sql触发器delete
‘壹’ sql delete触发器
你要让所有的user_id都-1?还是+1?还有你的user_id是否是自增列
如果是自增列是不可以用update修改的
‘贰’ SQL 2008 触发器中UPDATE、DELETE
1.CREATE TRIGGER TEST1
ON A FOR UPDATE
AS
DECLARE @A,@B,@C,@D,@E,@F,@G
SELECT @B=B,....@G=G FROM INSERTED WHERE A=@A
UPDATE B SET A=@A,,,,G=@G,[KEY] = 2 WHERE @A=A
GO
2.CREATE TRIGGER TEST2
ON A FOR DELETE
AS
declare @A
SELECT @A = A FROM DELETED
UPDATE B SET [KEY] = 3
GO
第一个我不知道能不能完成你的功能 没有测试
‘叁’ 在Sql Server触发器中判断操作是Insert还是Update还是Delete
DECLARE
@IsInsert bit,
@IsUpdate bit,
@IsDelete bit
IF EXISTS(SELECT 1 FROM inserted) AND NOT EXISTS(SELECT 1 FROM deleted)
SET @IsInsert = 1
ELSE
SET @IsInsert = 0
IF EXISTS(SELECT 1 FROM inserted) AND EXISTS(SELECT 1 FROM deleted)
SET @IsUpdate = 1
ELSE
SET @IsUpdate = 0
IF NOT EXISTS(SELECT 1 FROM inserted) AND EXISTS(SELECT 1 FROM deleted)
SET @IsDelete = 1
ELSE
SET @IsDelete = 0
create trigger Update_Del on Table
for update,delete
as
if not exists(select 1 from inserted)
begin /*inserted表无记录,是删除*/
end
else
begin /*是更新*/ end
go
关键在于Inserted表
触发器语句中使用了两种特殊的表:deleted
表和 inserted 表。
Deleted
表用于存储 DELETE 和 UPDATE 语句所影响的行的复本。在执行 DELETE 或 UPDATE 语句时,行从触发器表中删除,并传输到 deleted
表中。Deleted 表和触发器表通常没有相同的行。
Inserted
表用于存储 INSERT 和 UPDATE 语句所影响的行的副本。在一个插入或更新事务处理中,新建行被同时添加到 inserted
表和触发器表中。Inserted 表中的行是触发器表中新行的副本。
1.插入操作(Insert)
Inserted表有数据,Deleted表无数据
2.删除操作(Delete)
Inserted表无数据,Deleted表有数据
3.更新操作(Update)
Inserted表有数据(新数据),Deleted表有数据(旧数据)
‘肆’ sql触发器for delete触发器问题
---在表A上单独为Delete创建一个触发器
CREATETRIGGER[dbo].[trigger_A]
ON[dbo].A
AFTERDELETE
AS
BEGIN
……
END
或者
CREATETRIGGER[dbo].[trigger_A]
ON[dbo].A
FORDELETE
AS
BEGIN
……
END
‘伍’ SQL触发器同步删除数据要怎么写
使用delete 触发器格式:
CREATE trigger tri_update
on tablename
for delete
as
begin
--sql code
end
例:
CREATE trigger tri_update
on tablename1 --触发的表名
for delete
as
begin
declare @del_id varchar(40)
select @del_id = id from deleted --tablename1 中删除的数据保存在 deleted 中
delete tablename2 where id = @del_id --同步删除tablename2中数据
end
‘陆’ sql 触发器 阻止多条delete
instead of 触发器(针对一次删除多条)
create trigger club_messages_delete
on club_messages
instead of delete
as
begin
delete from club_Reply where messagesID in (select messagesID from deleted)
delete from club_messages where messagesID in (select messagesID from deleted)
end
after 触发器(针对多条)
create trigger club_messages_delete
on club_messages
after delete
as
begin
delete from club_Reply where messagesID in (select messagesID from deleted)
end
其实根据你的设计方案,我想根本就可以不用触发器,一条留言只对应一条回复,这样的话可以把留言ID作为留言回复的外键,把删除规则设为层叠就可以了
‘柒’ 请问SQL触发器使用Deleted为什么没有执行
把触发器改一下,做成如下测试:
ALTER TRIGGER zkemp_udpate on zlemployee
after update
as
declare @state int,@i int
set @state=(select top 1 state from inserted)
if exists (select 1 from USERINFO where badgenumber=name
or empid in (select top 1 id from Deleted))
begin
print('有记录')
end
-----------------------
然后执行update表,看看是否输出 '有记录'问题,没有的话,说明没有找到可以删除的记录,自然是不会执行了
‘捌’ 求解答sql中的delete触发器赋值操作。。
不是这样写的,应该是
create trigger tri_还书
after delete on loan
REFERENCING OLD AS OLD NEW AS NEW FOR EACH ROW
begin
insert into loanhist values(:OLD.借阅证号,:OLD.借阅书号,:OLD.借阅日期,getdate());
end
大概是这样的,如果不对,你可以改改试试
‘玖’ 关于sql delete和update的触发器
因为你引入了inserted。在delete触发器里,inserted永远是空集。因此,三表连接后,也永远是空集,即update语句永远不会执行。
删掉inserted,像这样就可以了:
createtriggerp7
onp_form0000000006_mafterdelete
as
updatepsetp.amt_2-=d.amt_1from
p_form0000000006_mp,deleteddwherep.code>d.code;
‘拾’ sql2000里面,两张表上的delete触发器怎么实现
建议使用存储过程实现
A表,B表给你一个示例,具体情况你修改一下
两个表必须有联系,比如外链
create proc pro_delete
@ID int --要删除信息的ID,A表的ID传进来
as
begin
--该信息已经存在
if exists(select * from B表 where ID= @ID)
begin
--A表不做任何操作
urollback
end
--B表中没有相关信息时 A表中的信息删除
else
begin
delete * from A where ID=@ID
end
end
go
应该能解决你的问题了