sqlserver 触发器

发布网友 发布时间:2022-04-22 07:56

我来回答

4个回答

热心网友 时间:2022-04-08 09:47

删除的时候,记录已经不存在,向另一张表中写什么?是写入准备删除的记录的某些字段吗?
暂不管删除,就insert和update触发。

create trigger TR_MyTrigger
on 表名
for insert, update
as
begin
insert 另一张表( ...... )
select ...... from inserted
end

热心网友 时间:2022-04-08 11:05

create trigger 触发器名
ON 表
FOR INSERT,UPDATE,DELETE
AS
BEGIN
set nocount on
declare @Ins int,@Del int
select @Ins=count(*) from Inserted
select @Del=count(*) from Deleted
if @Ins>0 and @Del=0
begin
insert into Stu_Log(id, name, sex, major, birdate, FType, FOpTime)
select id, name, sex, major, birdate, 'ADD',getdate() from Inserted
end
else if @Ins>0 and @Del>0
begin
insert into Stu_Log(id, name, sex, major, birdate, FType, FOpTime)
select id, name, sex, major, birdate, 'UPDATE1',getdate() from Deleted

insert into Stu_Log(id, name, sex, major, birdate, FType, FOpTime)
select id, name, sex, major, birdate, 'UPDATE2',getdate() from Inserted
end
else if @Ins=0 and @Del>0
begin
insert into Stu_Log(id, name, sex, major, birdate, FType, FOpTime)
select id, name, sex, major, birdate, 'DEL',getdate() from Deleted
end

END

热心网友 时间:2022-04-08 12:40

create trigger ALL_Stu
ON dbo.stu
FOR INSERT,UPDATE,DELETE
AS
BEGIN
set nocount on
declare @Ins int,@Del int
select @Ins=count(*) from Inserted
select @Del=count(*) from Deleted
if @Ins>0 and @Del=0
begin
insert into Stu_Log(id, name, sex, major, birdate, FType, FOpTime)
select id, name, sex, major, birdate, 'ADD',getdate() from Inserted
end
else if @Ins>0 and @Del>0
begin
insert into Stu_Log(id, name, sex, major, birdate, FType, FOpTime)
select id, name, sex, major, birdate, 'UPDATE1',getdate() from Deleted

insert into Stu_Log(id, name, sex, major, birdate, FType, FOpTime)
select id, name, sex, major, birdate, 'UPDATE2',getdate() from Inserted
end
else if @Ins=0 and @Del>0
begin
insert into Stu_Log(id, name, sex, major, birdate, FType, FOpTime)
select id, name, sex, major, birdate, 'DEL',getdate() from Deleted
end

END

热心网友 时间:2022-04-08 14:31

路过。。。学习下!

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com