码迷,mamicode.com
首页 > 数据库 > 详细

MSSQL - 存储过程事物

时间:2015-07-05 16:26:09      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		HF_Ultrastrong
-- Create date: 2015年7月5日14:39:00
-- Description:	删除类别(连同其下的新闻及新闻评论一起删除)
-- =============================================
CREATE PROCEDURE DeleteCategoryAndNewsAndComment
	@Caid int
AS
BEGIN

	begin transaction

	--定义局部变量 
	declare @errorSum int  
	    
	--初始化临时变量   
	set @errorSum=0  

	--执行SQL语句(根据分类,查询出此分类下创建的所有新闻,然后根据新闻编号删除其下的所有评论)
	delete from Tb_Comment where NewsId in (select ID from Tb_News where CaId = @Caid)
	--累计是否有错误 
	set @errorSum=@errorSum+@@error    

	--执行SQL语句
	delete from Tb_News where CaId = @Caid 
	--累计是否有错误  
	set @errorSum=@errorSum+@@error    

	--执行SQL语句
	delete from Tb_Category where ID = @Caid      --Tb_Category--Tb_Categorsy调试
	--累计是否有错误  
	set @errorSum=@errorSum+@@error   
	
	--如果有错误 
	if @errorSum<>0     
	 
	--执行回滚 
	begin  
	 
	rollback transaction 
	  
	end
	
	--否则提交事物   
	else
	   
	begin  
	 
	commit  transaction  
	 
	end
END
GO

 

MSSQL - 存储过程事物

标签:

原文地址:http://www.cnblogs.com/KTblog/p/4622199.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!