插入后的SQL触发器更新

时间:2014-11-13 21:05:44

标签: tsql triggers

我正在尝试创建一个触发器,在插入后将计算字段添加到一个字段。我已经玩了好几天了,可以用另一双眼睛。基本上,如果字段PolicyNum为空并且满足其他一些参数,触发器将触发,插入我的函数以及一些内务处理项。

ALTER TRIGGER PolicyNumber ON Consolidated 

AFTER INSERT
AS
BEGIN
IF EXISTS (SELECT i.PolicyNum, i.PlanCode
    FROM Inserted AS i
    JOIN Consolidated as C
    ON c.ID = i.ID and c.SEQN = i.SEQN
    WHERE C.PolicyNum = '' AND
    C.PlanCode like '%NYL%'
    )

DECLARE @PolicyNumber nVarChar(255)
DECLARE @IMISID nVarChar(10)
DECLARE @SEQID Int

EXEC    @PolicyNumber = [dbo].[fn_GetNYLPolicyNumber]

--Updating Record
UPDATE Consolidated
SET PolicyNum = @PolicyNumber, 
UpdatedBy = 'Trigger', 
UpdatedDate = GETDATE()
WHERE (ID = @IMISID) AND (SEQN = @SEQID)
END

1 个答案:

答案 0 :(得分:0)

ALTER TRIGGER PolicyNumber ON Consolidated 

AFTER INSERT
AS
BEGIN
IF EXISTS (SELECT i.PolicyNum, i.PlanCode
    FROM Inserted AS i
    JOIN Consolidated as C
    ON c.ID = i.ID and c.SEQN = i.SEQN
    WHERE C.PolicyNum = '' AND
    C.PlanCode like '%NYL%'
    )
begin
DECLARE @PolicyNumber nVarChar(255)
DECLARE @IMISID nVarChar(10)
DECLARE @SEQID Int

EXEC    @PolicyNumber = [dbo].[fn_GetNYLPolicyNumber]

--Updating Record
UPDATE Consolidated
SET PolicyNum = @PolicyNumber, 
UpdatedBy = 'Trigger', 
UpdatedDate = GETDATE()
WHERE (ID = @IMISID) AND (SEQN = @SEQID)
END
end