关于SQL Server触发器instead of delete 使用问题
表student(字段:stu_id,stu_name)主键:stu_id表score(字段:stu_id,stu_sc)外键:stu_id实现删除表student一条信...
表student(字段:stu_id,stu_name ) 主键:stu_id
表score(字段:stu_id,stu_sc)外键:stu_id
实现删除表student一条信息时,同时删除表score中对应记录
我建立了instead of 型删除触发器,如下:
-------------------------------------------
create trigger tri_instead_del
on student
instead of delete
as
delete from score
where stu_id=(select stu_id from deleted)
-------------------------------------------
delete from student where stu_id='1001'
执行delete命令删除表student中记录,结果是表score中“stu_id=1001”的记录删除了,但表student中的记录没删除。是不是应该都删除,问题出在哪? 展开
表score(字段:stu_id,stu_sc)外键:stu_id
实现删除表student一条信息时,同时删除表score中对应记录
我建立了instead of 型删除触发器,如下:
-------------------------------------------
create trigger tri_instead_del
on student
instead of delete
as
delete from score
where stu_id=(select stu_id from deleted)
-------------------------------------------
delete from student where stu_id='1001'
执行delete命令删除表student中记录,结果是表score中“stu_id=1001”的记录删除了,但表student中的记录没删除。是不是应该都删除,问题出在哪? 展开
2个回答
展开全部
create trigger tri_instead_del
on student
instead of delete
as
begin
declare @courseId varchar(50) --定义编号变量。
--在deleted表中查询要删除的编号
select @courseId=stu_idfrom deleted
delete from score
where stu_id=@courseId
delete from student where stu_id=@courseId
--其中就要通过deleted表来查询要删除的编号
end
INSTEAD OF触发器:它在执行INSERT、UPDATE、DELETE语句操作之前执行触发器本身所定义的操作。而INSTEAD OF触发器是可以定义在视图上的
追问
非常感谢您的解答,通过定义变量可以实现。我想追问下,不是变量,按照我做的方式,问题出在哪里?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询