[size=medium]
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Dylan
-- Create date: 2010年
-- Description: 删除外键关联的三张表(从下往上删除)
-- =============================================
CREATE PROCEDURE 表名
@table1_Id int -- 第一张表的ID值
AS
BEGIN
begin tran TranStart -- 事务开始
save tran FirstPoint
-- 先删除第三张表相关联的信息
delete from table3 where table2_foreignId in
(
select table2_Id from ConfigSecondBranch where table2_foreignId in
(
select table1_Id from ConfigFirstBranch where table1_Id = @table1_Id
)
)
-- 再先删除第二张表相关联的信息
delete from table2 where table2_foreignId in
(
select table1_Id from ConfigFirstBranch where table1_Id = @table1_Id
)
-- 最后先删除第一张表相关联的信息
delete from table1 where table1_Id = @table1_Id
-- 如果出错
if @@ERROR <> 0
begin
rollback tran FirstPoint -- 回滚
end
commit tran TranStart
END
[/size]