日期:2014-05-19  浏览次数:20418 次

这个触发器是否正确(立即结贴)
表members有一个字段email
表accounts有一个字段userEmail
现在我想在表accounts中建立一个触发器,当表accounts里的email发生改变时,表members同步更新,下面的触发器代码不知是否正确?

CREATE   TRIGGER   tr_emailChange   ON   accounts   for   update  
AS
    UPDATE   members   SET   members.email=inserted.userEmail   FROM   inserted   WHERE   inserted.userName=members.userName

------解决方案--------------------
只要没语法问题就没问题

------解决方案--------------------
CREATE TRIGGER tr_emailChange ON accounts
AFTER update
AS
DECLARE @userName nvarchar(100)
DECLARE @userEmail nvarchar(100)
IF @@ROWCOUNT > 0
BEGIN
SELECT @userName=userName,@userEmail=userEmail FROM Inserted

UPDATE members SET email = @email where userEmail = @userEmail

END
------解决方案--------------------
对,可以多加if update(email)

CREATE TRIGGER tr_emailChange ON accounts for update
AS
if update(email)
UPDATE members SET members.email=inserted.userEmail FROM inserted WHERE inserted.userName=members.userName
------解决方案--------------------
LZ 的看起来没问题