sqlserver 用觸發器記錄操作
阿新 • • 發佈:2019-01-04
create trigger tri_code on t_jc_code after insert,delete,update
as
begin
declare @id nvarchar(50)
declare @typeid nvarchar(50)
declare @name nvarchar(50)
declare @code nvarchar(50)
if exists(select 1 from inserted) and not exists(select 1 from deleted)
begin
select @id=id,@typeid=typeid,@name=name,@code=cod from inserted
insert into t_tri_code(id,typeid,name,cod,atype) values(@id,@typeid,@name,@code,1)
end
if exists(select 1 from inserted) and exists(select 1 from deleted)
begin
select @id=id,@typeid=typeid,@name=name,@code=cod from inserted
insert into t_tri_code(id,typeid,name,cod,atype) values (@id,@typeid,@name,@code,2)
end
if not exists(select 1 from inserted) and exists(select 1 from deleted)
begin
select @id=id,@typeid=typeid,@name=name,@code=cod from deleted
insert into t_tri_code(id,typeid,name,cod,atype) values(@id,@typeid,@name,@code,3)
end
end
使用 select 1 from inserted 和 select 1 from deleted 是否有值來判斷是insert,update,delete