sqlserver 触发器增删改

create TRIGGER [dbo].[updUser]
ON [dbo].[user]
-- 触发时机、条件
after update AS 
declare @insertedId int
declare @oldPassWord nvarchar(255)
declare @nwePassWord nvarchar(255)
BEGIN 
     select @oldPassWord = passWord from deleted;
     SELECT @insertedId = id, @nwePassWord = passWord FROM inserted
     if (@nwePassWord = @oldPassWord)
     begin 
        update [user] set userName = 'cjxxxxx' where id = @insertedId
     end
END 


 
create TRIGGER addUser
ON [user]
-- 触发时机、条件
AFTER insert AS 
BEGIN 
    --SET NOCOUNT ON;
    UPDATE [user]
    SET passWord='add'
    WHERE id IN (SELECT id FROM inserted)
END 

insert into [user] values('cj4','cj4');

create trigger delUser
on [user]
after DELETE as
begin
    select * from deleted
end

select * from [user]

delete [user] where id = 3;

你可能感兴趣的:(sql)