关于sql server触发器和存储过程一些问题
求大牛解救啊,如果说我想写一个触发器,这个触发器能完成同步删除的效果,比如说A表中有b1和c1两个属性,分别是B表和C表的主属性,如果我想写个触发器把B表里的b1或者C表里的c1删除一条记录,同时在A表里删除和在刚才B表中删除的b1值相同的那一条记录。还有,这个能用存储过程来完成么?求大牛解救啊~~~
2、在delete,选择级联
不好意思哥哥,我们这个是老师留的作业里的内容,要求写几个触发器,不知道在哪儿加而且不知道怎么加。。。并不是为了更简单完成这项功能。。。你懂的。。。
用触发器来实现:
1.建表
create table b(b1 int primary key)
insert into b
select 1 union all
select 2
create table c(c1 int primary key)
insert into c
select 1 union all
select 2
create table a(
b1 int foreign key references b(b1),
c1 int foreign key references c(c1)
)
insert into a
select 1,1 union all
select 1,2 union all
select 2,1
go
create trigger dbo.trigger_b_delete
on dbo.b
for delete
as
delete b
from deleted d
where b.b1 = d.b1
go
create trigger dbo.trigger_c_delete
on dbo.c
for delete
as
delete c
from deleted d
where c.c1 = d.c1
go
delete from b where b1 = 1
--自动删除了b1 = 1的记录
select * from a
/*
b1c1
21
*/
delete from c where c1 = 1
--自动删除了c1 = 1的记录
select * from a
/*
*/