读书人

怎么利用SQL创建触发器使表1删除相应记

发布时间: 2012-01-22 22:38:43 作者: rapoo

如何利用SQL创建触发器使表1删除相应记录时,同时删除表2的相关记录?
在线急求。谢谢。

[解决办法]
--建立境
Create Table A
(IDInt,
NameVarchar(10))
Insert A Select 1, 'AA '
Union All Select 2, 'BB '
Create Table B
(IDInt,
ScoreInt)
Insert B Select 1, 23
Union All Select 1, 57
Union All Select 1, 98
Union All Select 2, 33
Union All Select 2, 23
GO
--建立器
Create Trigger Delete_B On A
For Delete
As
Delete B From B Inner Join Deleted A On A.ID = B.ID
GO
--
Delete From A Where ID = 1

Select * From B
GO
--除境
Drop Table A, B
--果
/*
IDScore
233
223
*/

读书人网 >SQL Server

热点推荐