日期:2014-05-18  浏览次数:20785 次

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

------解决方案--------------------
--建立測試環境
Create Table A
(ID Int,
Name Varchar(10))
Insert A Select 1, 'AA '
Union All Select 2, 'BB '
Create Table B
(ID Int,
Score Int)
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
--結果
/*
ID Score
2 33
2 23
*/