Sqlserver 存储过程中结合事务的代码
程序员文章站
2023-12-11 16:56:04
复制代码 代码如下: --方式一 if exists (select * from dbo.sysobjects where id = object_id(n'[dbo]....
复制代码 代码如下:
--方式一
if exists (select * from dbo.sysobjects where id = object_id(n'[dbo].[usp_procedurewithtransaction_demo]') and objectproperty(id, n'isprocedure') = 1)
drop procedure [dbo].[usp_procedurewithtransaction_demo]
go
-- =============================================
-- author: <chengxiaoming>
-- create date: <2010-06-11>
-- description: <demo:存储过程中使用事务>
-- =============================================
create procedure [dbo].[usp_procedurewithtransaction_demo]
as
begin
set xact_abort on
begin transaction
insert into lock(locktypeid) values('a')--此语句将出错,locktypeid为int类型
update lock set locktypeid = 2 where lockid = 32
commit transaction
set xact_abort off
end
go
--方式二
if exists (select * from dbo.sysobjects where id = object_id(n'[dbo].[usp_procedurewithtransaction_demo]') and objectproperty(id, n'isprocedure') = 1)
drop procedure [dbo].[usp_procedurewithtransaction_demo]
go
-- =============================================
-- author: <chengxiaoming>
-- create date: <2010-06-11>
-- description: <demo:存储过程中使用事务>
-- =============================================
create procedure [dbo].[usp_procedurewithtransaction_demo]
as
begin
begin transaction
insert into lock(locktypeid) values('a')--此语句将出错,locktypeid为int类型
update lock set locktypeid = 1 where lockid = 32
commit transaction
if(@@error <> 0)
rollback transaction
end
go
--方式三
if exists (select * from dbo.sysobjects where id = object_id(n'[dbo].[usp_procedurewithtransaction_demo]') and objectproperty(id, n'isprocedure') = 1)
drop procedure [dbo].[usp_procedurewithtransaction_demo]
go
-- =============================================
-- author: <chengxiaoming>
-- create date: <2010-06-11>
-- description: <demo:存储过程中使用事务>
-- =============================================
create procedure [dbo].[usp_procedurewithtransaction_demo]
as
begin
begin try
begin transaction
update lock set locktypeid = 1 where lockid = 32--此语句将出错,locktypeid为int类型
insert into lock(locktypeid) values('a')
commit transaction
end try
begin catch
rollback transaction
end catch
end
go
exec [usp_procedurewithtransaction_demo]
推荐阅读
-
Sqlserver 存储过程中结合事务的代码
-
sqlserver存储过程中SELECT 与 SET 对变量赋值的区别
-
sqlserver存储过程中SELECT 与 SET 对变量赋值的区别
-
Sqlserver 存储过程中结合事务的代码
-
sqlserver 存储过程中If Else的用法实例
-
sqlserver 存储过程中If Else的用法实例
-
SQLServer 2008中的代码安全(一) 存储过程加密与安全上下文
-
MYSQL存储过程中事务和DECLARE EXIT/CONTINUE HANDLER的使用
-
SQL Server存储过程中编写事务处理的方法小结
-
Sqlserver事务备份和还原的实例代码(必看)