存储过程使用事物的几种方式 转载自--http://www.cnblogs.com/chengxiaoming/archive/2010/06/11/1756163.html
2012-08-16 09:11
856 查看
--方式一
ifexists (select*from dbo.sysobjects where id =object_id(N'[dbo].[USP_ProcedureWithTransaction_Demo]') andOBJECTPROPERTY(id, N'IsProcedure') =1)
dropprocedure[dbo].[USP_ProcedureWithTransaction_Demo]
GO
-- =============================================
-- Author: <ChengXiaoming>
-- Create date: <2010-06-11>
-- Description: <Demo:存储过程中使用事务>
-- =============================================
CreatePROCEDURE[dbo].[USP_ProcedureWithTransaction_Demo]
As
Begin
SET XACT_ABORT ON
BeginTransaction
InsertInto Lock(LockTypeID) Values('A')--此语句将出错,LockTypeID为Int类型
Update Lock Set LockTypeID =2Where LockID =32
CommitTransaction
SET XACT_ABORT OFF
End
GO
--方式二
ifexists (select*from dbo.sysobjects where id =object_id(N'[dbo].[USP_ProcedureWithTransaction_Demo]') andOBJECTPROPERTY(id, N'IsProcedure') =1)
dropprocedure[dbo].[USP_ProcedureWithTransaction_Demo]
GO
-- =============================================
-- Author: <ChengXiaoming>
-- Create date: <2010-06-11>
-- Description: <Demo:存储过程中使用事务>
-- =============================================
CreatePROCEDURE[dbo].[USP_ProcedureWithTransaction_Demo]
As
Begin
BeginTransaction
InsertInto Lock(LockTypeID) Values('A')--此语句将出错,LockTypeID为Int类型
Update Lock Set LockTypeID =1Where LockID =32
CommitTransaction
If(@@ERROR<>0)
RollbackTransaction
End
GO
--方式三
ifexists (select*from dbo.sysobjects where id =object_id(N'[dbo].[USP_ProcedureWithTransaction_Demo]') andOBJECTPROPERTY(id, N'IsProcedure') =1)
dropprocedure[dbo].[USP_ProcedureWithTransaction_Demo]
GO
-- =============================================
-- Author: <ChengXiaoming>
-- Create date: <2010-06-11>
-- Description: <Demo:存储过程中使用事务>
-- =============================================
CreatePROCEDURE[dbo].[USP_ProcedureWithTransaction_Demo]
As
Begin
Begin Try
BeginTransaction
Update Lock Set LockTypeID =1Where LockID =32--此语句将出错,LockTypeID为Int类型
InsertInto Lock(LockTypeID) Values('A')
CommitTransaction
End Try
Begin Catch
RollbackTransaction
End Catch
End
GO
Exec[USP_ProcedureWithTransaction_Demo]
ifexists (select*from dbo.sysobjects where id =object_id(N'[dbo].[USP_ProcedureWithTransaction_Demo]') andOBJECTPROPERTY(id, N'IsProcedure') =1)
dropprocedure[dbo].[USP_ProcedureWithTransaction_Demo]
GO
-- =============================================
-- Author: <ChengXiaoming>
-- Create date: <2010-06-11>
-- Description: <Demo:存储过程中使用事务>
-- =============================================
CreatePROCEDURE[dbo].[USP_ProcedureWithTransaction_Demo]
As
Begin
SET XACT_ABORT ON
BeginTransaction
InsertInto Lock(LockTypeID) Values('A')--此语句将出错,LockTypeID为Int类型
Update Lock Set LockTypeID =2Where LockID =32
CommitTransaction
SET XACT_ABORT OFF
End
GO
--方式二
ifexists (select*from dbo.sysobjects where id =object_id(N'[dbo].[USP_ProcedureWithTransaction_Demo]') andOBJECTPROPERTY(id, N'IsProcedure') =1)
dropprocedure[dbo].[USP_ProcedureWithTransaction_Demo]
GO
-- =============================================
-- Author: <ChengXiaoming>
-- Create date: <2010-06-11>
-- Description: <Demo:存储过程中使用事务>
-- =============================================
CreatePROCEDURE[dbo].[USP_ProcedureWithTransaction_Demo]
As
Begin
BeginTransaction
InsertInto Lock(LockTypeID) Values('A')--此语句将出错,LockTypeID为Int类型
Update Lock Set LockTypeID =1Where LockID =32
CommitTransaction
If(@@ERROR<>0)
RollbackTransaction
End
GO
--方式三
ifexists (select*from dbo.sysobjects where id =object_id(N'[dbo].[USP_ProcedureWithTransaction_Demo]') andOBJECTPROPERTY(id, N'IsProcedure') =1)
dropprocedure[dbo].[USP_ProcedureWithTransaction_Demo]
GO
-- =============================================
-- Author: <ChengXiaoming>
-- Create date: <2010-06-11>
-- Description: <Demo:存储过程中使用事务>
-- =============================================
CreatePROCEDURE[dbo].[USP_ProcedureWithTransaction_Demo]
As
Begin
Begin Try
BeginTransaction
Update Lock Set LockTypeID =1Where LockID =32--此语句将出错,LockTypeID为Int类型
InsertInto Lock(LockTypeID) Values('A')
CommitTransaction
End Try
Begin Catch
RollbackTransaction
End Catch
End
GO
Exec[USP_ProcedureWithTransaction_Demo]
相关文章推荐
- C#使用命令行方式实现Ping简单功能 http://www.cnblogs.com/kevinton/archive/2007/06/28/798581.html
- linux下select使用--转载http://www.cnblogs.com/moonvan/archive/2012/05/26/2518881.html
- 转载TortoiseSVN的使用详解1(http://www.cnblogs.com/xiaobaihome/archive/2012/03/20/2407610.html)
- 转载TortoiseSVN的使用详解1(http://www.cnblogs.com/xiaobaihome/archive/2012/03/20/2407610.html)
- MyEclipse设置编码方式 转载【http://www.cnblogs.com/susuyu/archive/2012/06/27/2566062.html】
- Android实现推送方式解决方案【转载地址:http://www.cnblogs.com/hanyonglu/archive/2012/03/04/2378971.html】
- 转载TortoiseSVN的使用详解2(http://www.cnblogs.com/xiaobaihome/archive/2012/03/20/2407610.html)
- 转载TortoiseSVN的使用详解2(http://www.cnblogs.com/xiaobaihome/archive/2012/03/20/2407610.html)
- 转载TortoiseSVN的使用详解3(http://www.cnblogs.com/xiaobaihome/archive/2012/03/20/2407610.html)
- 转载TortoiseSVN的使用详解3(http://www.cnblogs.com/xiaobaihome/archive/2012/03/20/2407610.html)
- hadoop使用javaAPI实现hdfs常见操作(转载于http://www.cnblogs.com/xuqiang/archive/2011/06/03/2042526.html)
- 使用反射和特性构造自己的ORM框架 转载自:http://www.cnblogs.com/xdesigner/archive/2008/06/24/1228702.html
- 在Asp.net MVC中使用Asp.net Ajax --转载http://www.cnblogs.com/Nimeux/archive/2008/08/17/1269907.html
- SQL优化--使用 EXISTS 代替 IN 和 关联查询(inner join) (转载)http://www.cnblogs.com/zping/archive/2008/08/05/1260959.html
- jquery ui 1.7 ui.tabs 动态添加与关闭(按钮关闭+双击关闭) 【转载】(http://www.cnblogs.com/lijia7436/archive/2010/03/31/1701193.html)
- 事件转载自(http://www.cnblogs.com/zengshunyou/archive/2009/06/16/1504475.html)
- linux的硬链接与软连接(转载,原文地址为http://www.cnblogs.com/sonic4x/archive/2011/08/05/2128543.html)
- SQLiteDatabase类——转载(http://www.cnblogs.com/tt_mc/archive/2010/03/09/1681540.html)
- Asp.Net页面生命周期【转载,地址:http://www.cnblogs.com/xhwy/archive/2012/05/20/2510178.html】
- 浮点数在计算机中存储方式 转自:http://www.cnblogs.com/jillzhang/archive/2007/06/24/793901.html