日期:2014-05-18  浏览次数:20661 次

高手请进来看看,有关于触发器的问题
譬如说,我有一员工表,和用户表(登陆用),用户表的信息是从员工表添加而来,现在我更改员工表的员工状态(在职,离职,锁定),当我更新员工表的员工状态为‘离职’时,同时删除在用户表对应的员工号,我的触发器代码如下:

CREATE   trigger   UserInfo_upate   on   UserInfo
after   update
as
declare   @userstate   varchar(10)
set   @userstate= '离职 '
select   @userstate=Userstate   from   inserted
if   update(UserState)
begin
Delete   from   UserLogin
where   userID   IN   (select   WorkNo   from   inserted)
end

但我只要一更改员工表的员工状态(不论是‘离职’或者‘锁定’),用户表里的信息都删除了,请问,怎么才能删除指定已经‘离职’的用户表信息



------解决方案--------------------
CREATE trigger UserInfo_upate on UserInfo
after update
as
if update(UserState)
begin
Delete from UserLogin
where userID IN (select WorkNo from inserted where Userstate= '离职 ')
end
------解决方案--------------------
CREATE trigger UserInfo_upate on UserInfo
for update
as
if update(UserState)
begin
Delete u from UserLogin u join inserted i on u.userID =i.WorkNo
where i.Userstate= '离职 '
end