如何更新此逻辑的触发器?

时间:2018-02-15 12:39:41

标签: sql sql-server triggers

表1:Userinfo_attarea

    employee_id area_id
          13    61
          14    61

表2:Userinfo

userid  badgenumber
   14   ST021
   15   ST013

表3:DevicePersonnelarea

areaid  areaname
  18    PROJECT119
  15    PROJECT108

表4:employee_attarea

emp_code    area_id
  ST021     119
  ST013     108

我正在使用以下触发器它工作正常但是当我更新多个时它只更改为单个值..如果值已经存在意味着它将更新,否则为同一employee_id插入多个。 mY tRIGGER查询:

CREATE TRIGGER [dbo].[TimeVision7]
   ON [dbo].[employee_attarea]
   FOR INSERT,UPDATE
AS 
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets FROM
    -- interfering with SELECT statements.
    SET NOCOUNT ON;
    DECLARE @areaid int;
    DECLARE @areaname nvarchar(50);
    DECLARE @count int;
    DECLARE @employee_code nvarchar(100);
    IF EXISTS(SELECT * FROM inserted) AND NOT EXISTS (SELECT * FROM deleted) BEGIN

        SELECT @areaname = i.area_id
        FROM  inserted i
        SELECT @areaid = areaid;

        FROM DevicePersonnelarea d
        WHERE d.id = @areaname
        SET @areaid = @areaid; --Why is this here? you're setting the value of a variable to the value fo the same variable.

        SELECT @employee_code = i.emp_code
        FROM inserted i;

        SELECT @employee_code = userid
        FROM BioTime_Production.dbo.userinfo u
        WHERE u.badgenumber=@employee_code;

        SET @employee_code = @employee_code; --Why is this here? you're setting the value of a variable to the value fo the same variable.

        SELECT @count = count(*)
        FROM BioTime_Production.dbo.userinfo_attarea u
        WHERE u.employee_id = @employee_code;

        IF (@count=1) BEGIN
            UPDATE  BioTime_Production.dbo.userinfo_attarea
            SET BioTime_Production.dbo.userinfo_attarea.area_id = @areai
            FROM inserted i
            WHERE BioTime_Production.dbo.userinfo_attarea.employee_id = @employee_code;

        END ELSE BEGIN

            INSERT INTO  BioTime_Production.dbo.userinfo_attarea (employee_id,area_id)
            VALUES (@employee_code,@areaid);

        END
    END
    IF EXISTS(SELECT * FROM inserted ) AND EXISTS(SELECT * FROM deleted ) BEGIN

        SELECT @areaname = i.area_id
        FROM  inserted i;

        SELECT @areaid = areaid
        FROM DevicePersonnelarea d
        WHERE d.id = @areaname;

        SET @areaid = @areaid; --Why is this here? you're seting the value of a variable to the value fo the same variable.

        SELECT @employee_code = i.emp_code
        FROM inserted i; 

        SELECT @employee_code = userid
        FROM  BioTime_Production.dbo.userinfo u
        WHERE u.badgenumber = @employee_code;

        SET @employee_code=@employee_code; --Why is this here? you're setting the value of a variable to the value fo the same variable.

        SELECT @count = count(*)
        FROM BioTime_Production.dbo.userinfo_attarea u
        WHERE u.employee_id = @employee_code;
        IF (@count=1) BEGIN

            UPDATE  BioTime_Production.dbo.userinfo_attarea 
            SET BioTime_Production.dbo.userinfo_attarea.area_id = @areaid
            FROM inserted i
            WHERE BioTime_Production.dbo.userinfo_attarea.employee_id = @employee_code;

        END ELSE BEGIN

            INSERT INTO  BioTime_Production.dbo.userinfo_attarea (employee_id,area_id)
            VALUES (@employee_code,@areaid);
        END
    END
END

预期输出:userinfo_attarea

employee_id area_id
  14           1
  14           2
  14          18

任何人都有帮助...

0 个答案:

没有答案