0

我有“员工登记”表格(Windows 应用程序)。员工注册完成后,同时我想创建一个用户。从“员工注册”中获取“EmpCode”值并将其设为“用户名”的值。请帮我写一个触发器。

员工登记表列是

EMPCODE, 
EMPNAME, 
DESIGNATION, 
DEPARTMENT, 
DOJ,  
EMAILID, 
CATEGORY, 
EMPSTATUS, 
EMPMANAGER etc etc

UserCreation 表列是

USERNAME, 
USERPASSWORD, 
USERMAPPINGCODE, 
USERTYPE, 
USERACTIVE, 
USEREMAIL

当 'employee regstratn' 的插入发生时,同时在 'UserCreation' 表中发生以下 thngs ....,

EMPCODE的值应保存为USERNAME的值。 EMPNAME的值应保存为USERPASSWORD的值。 USERMAPPINGCODE' 值 optional CATEGORY' 值应保存为USERTYPE' 值。 USERACTIVE' 的值为 1 EMAILID的值应保存为USEREMAIL' 的值

请帮忙....

4

2 回答 2

2

干得好:

CREATE TRIGGER EmployeeTrigger
ON dbo.Employees
FOR INSERT
AS
BEGIN
    DECLARE @empCode BIGINT
    SET @empCode = (SELECT EMPCODE FROM INSERTED)
    DECLARE @empName VARCHAR(50)
    SET @empName = (SELECT EMPNAME FROM INSERTED)
    DECLARE @empCat VARCHAR(50)
    SET @empCat = (SELECT CATEGORY FROM INSERTED)
    DECLARE @empEMail VARCHAR(50)
    SET @empEMail = (SELECT EMAILID FROM INSERTED)
    INSERT INTO dbo.Users (USERNAME, USERPASSWORD, USERMAPPINGCODE, USERTYPE, USERACTIVE, USEREMAIL)
    VALUES  (@empCode, @empName, NULL, @empCat, 1, @empEMail)
END

它可能不会按原样执行,您可能需要调整一些变量名,以防我们中的任何一个出现拼写错误。这是使用包含新插入值的 INSERTED 逻辑表。

于 2012-10-29T05:06:26.517 回答
1
CREATE TRIGGER EmployeeTrigger
ON dbo.Employees
FOR INSERT
AS
BEGIN
  INSERT dbo.Users (USERNAME, USERPASSWORD, USERMAPPINGCODE, USERTYPE, USERACTIVE, USEREMAIL)
  SELECT EMPCODE, EMPNAME, NULL, CATEGORY, 1, EMAILID
  FROM inserted
END  
于 2012-10-29T07:36:01.960 回答