当前位置: 代码迷 >> Sql Server >> 怎么利用SQL创建触发器使表1删除相应记录时,同时删除表2的相关记录
  详细解决方案

怎么利用SQL创建触发器使表1删除相应记录时,同时删除表2的相关记录

热度:27   发布时间:2016-04-27 17:55:09.0
如何利用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
*/

  相关解决方案