存储过程使用回滚
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
CREATE PROCEDURE [dbo].[cunchuguocheng]
AS
declare @num int---------变量----------------------
declare @rows int----------循环次数-----------------
declare @flag int-------标记操作是否成功:0是失败,1是成功-------
create table #prolist(
id int identity(1,1),---自动增长
proindex nvarchar(50)-----自定义临时表字段
)
BEGIN
select @num=1
insert #temp select distinct 字段1 from 表1 where 条件
select @rows=@@rowcount
BEGIN TRAN----------------------添加回滚机制----------------------------------------
while @num<=@rows
begin
--------------要循环的内容开始------------------
select @num=@num+1
--------------要循环的内容结束-------------------
end
if @@error<>0------------------------判断是否有错误出现如果有则回滚,如果没有则提交---------------------------------------
begin
rollback tran-------------------出现错误开始回滚---------------------------
select @flag=0 end
else
begin
COMMIT TRAN----------------------提交事务------------------------------
select @flag=1
end
select @flag as flag
END