我的第一个触发器练习
来源:互联网 发布:腾讯炒股软件 编辑:程序博客网 时间:2024/04/29 17:31
表ExA如下:
CREATE TABLE ExA (
id int IDENTITY (1, 1) NOT NULL ,
status bit NULL ,
primary key (id)
)
select * from ExA
id status
----------- ------
4 1
6 1
表ExB如下:
CREATE TABLE ExB (
id int IDENTITY (1, 1) NOT NULL ,
Aid int NULL ,
mark varchar (50) NULL ,
primary key (id),
foreign key (Aid) references ExA On Delete Cascade On Update Cascade
)
select * from ExB
id Aid mark
----------- ----------- --------------------------------------------------
76 4 test1
77 4 test2
78 4 test3
79 4 test4
84 6 test1
85 6 test2
86 6 test3
87 6 test4
ExA.id与ExB.Aid是一对多的关系。
现在要求,当ExB中删除记录时,检查表,若ExA的数据在ExB中没有找到对应的数据,则将ExA.Status设置为0。
触发器如下:
Alter Trigger trTestEx
On ExB
For Delete
As
Declare curDelete cursor for
select Aid from Deleted group by Aid
Declare @intAid int
Open curDelete
Fetch Next from curDelete Into @intAid
while @@Fetch_Status=0
Begin
if exists(select * from ExA where id=@intAid)
Begin
if not exists(select * from ExB where Aid=@intAid)
Update ExA set status=0 where id=@intAid
End
Fetch Next from curDelete Into @intAid
End
Close curDelete
Deallocate curDelete
Go
Delete From ExB where Aid=4 --支持Delete From ExB where Aid in (4,6)
结果如下:
select * from ExA
id status
----------- ------
4 0
6 1
select * from ExB
id Aid mark
----------- ----------- --------------------------------------------------
84 6 test1
85 6 test2
86 6 test3
87 6 test4
- 我的第一个触发器练习
- 我的第一个mysql触发器
- 触发器的学习和我的第一个触发器的例子
- 分享:菜鸟的练习--我的第一个小板凳
- 我的第一个的JAVA反射练习代码
- 我的第一个
- 我的第一个
- 自己写的第一个SQLSERVER2005触发器
- 我的第一个extjs desktop + jsp +mysql实战练习项目
- Hello World!(printf练习)我的第一个C程序:-)
- 我的第一个Blog!
- 我的第一个blog
- 我的第一个Robocode
- 我的第一个GMaiL
- 我的第一个BLOG
- 我的第一个HelloWorld
- 我的第一个文章
- 我的第一个BLOG
- MTL 矩阵逆阵 解线性方程
- 沙僧传
- 源码
- 用asp.net画饼图
- Hibernate查询语言:HQL (选择自 Jplateau 的 Blog )
- 我的第一个触发器练习
- 《深入浅出MFC读书笔记一》
- 插件框架
- 大二下学期课表 2005.02~2005.07
- Blog on Blog 或 影迷
- [转贴]EXP、IMP 命令详解
- 昨日关注:BT之父下海从商 免费BT网络带来无限商机
- 今天被人ddos网站