CREATE PROCEDURE AddUser
(
    @UserSum int,    
    @Code varchar(16),
    @BeginCode int
)
asDeclare @userCode varchar(16)--存储用户code
Declare @UserName varchar(20)--存储用户name
Declare @UserGUID uniqueidentifier--存储用户的GUID
Declare @i int--增加用户个数
Declare @Number int--用户code生成格式
Declare @NUM int --存储判断输入的用户是否存在set @Number = @BeginCode --从多少号开始递增
set @i = 0
set @i = @UserSum -- 控制新增多少条记录
set @UserCode = @code + cast(@Number as varchar(16))
print '@code='+@Code
print '@UserCode='+@UserCode--对查询不到输入的用户和存在需要新增的用户的判断
select @NUM = count(*) from myuser where UserCode = @Code
--print '@NUM1='+cast(@NUM as varchar(16))
if @NUM = 0 
   begin
   print '需要拷贝的用户不存在!'
   return 0
   end
select @NUM = count(*) from myuser where UserCode = @UserCode
--print '@NUM2='+cast(@NUM as varchar(16))
if @NUM > 0 
   begin
   print '需要新增的用户已经存在!'
   return 0 
   end--找出要拷贝的记录,放到临时表中。select * into #myuser from myuser where UserCode = @Codeselect * into #myStationUser from myStationUser where UserGUID in(select UserGUID from Myuser where UserCode = @Code)select * into #myUserRights from myUserRights where UserGUID in(select UserGUID from Myuser where UserCode = @Code)select * into #myUserObject from myUserObject where UserGUID in(select UserGUID from Myuser where UserCode = @Code)
begin tran--开始事务
while(@i > 0)
begin
set @Number = convert(decimal(3,0),@Number)
set @UserCode = @code + cast(@Number as varchar(16))
set @UserName = @code + cast(@Number as varchar(16))
Set @UserGUID = newid()
--将表变量中的记录插入到基表中
insert into myuser 
select @UserGUID,@UserCode,@UserName,BUGUID,PhotoUrl,JobTitle,Email,OfficePhone,MobilePhone,HomePhone,ADAccount,CreatedOn,ModifiedOn,CreatedBy,ModifiedBy,IsDisabeld,DisabledReason,Comments,ParentGUID,Password,IsAdmin,IsSaler,JobNumber,DepartmentGUID,UserProject,IsAdvanceUser,DefaultStation from #myuser 
if @@error <> 0
begin
   print '回滚事务!'
   rollback tran
   return 1
end
insert into myStationUser(StationUserGUID,StationGUID,UserGUID) select newid(),StationGUID,@UserGUID from #myStationUser
if @@error <> 0
begin
   print '回滚事务!'
   rollback tran
   return 1
end
insert into myUserRights select newid(),ActionCode,ObjectType,Application,@UserGUID,StationGUID from #myuserRights
if @@error <> 0
begin
   print '回滚事务!'
   rollback tran
   return 1
end
insert into myUserObject select newid(),@UserGUID,StationGUID,ObjectType,ObjectGUID,EffectScope,TableName,BUGUID  from #myuserObject
if @@error <> 0
begin
   print '回滚事务!'
   rollback tran
   return 1
end
set @i=@i-1
set @Number=@Number+1
end
commit tran--提交事务
GO--drop proc adduser我将其中一段修改错误。使SQL抛异常。
select * into #myUserRights from myUserRights where UserGUID in(select UserGUID from Myuser where UserCode = @Code)select * into #myUserObject from myUserRights  where UserGUID in(select UserGUID from Myuser where UserCode = @Code)
在执行此存储过程时,sql抛出了错误,但是事务却没有回滚?提示:
@code=zhao
@UserCode=zhao12(1 行受影响)(1 行受影响)(154 行受影响)(154 行受影响)(1 行受影响)(1 行受影响)(154 行受影响)
消息 207,级别 16,状态 1,过程 AddUser,第 80 行
列名 'ObjectGUID' 无效。
消息 207,级别 16,状态 1,过程 AddUser,第 80 行
列名 'EffectScope' 无效。
消息 207,级别 16,状态 1,过程 AddUser,第 80 行
列名 'TableName' 无效。
消息 207,级别 16,状态 1,过程 AddUser,第 80 行
列名 'BUGUID' 无效。
消息 266,级别 16,状态 2,过程 AddUser,第 80 行
EXECUTE 后的事务计数指示缺少了 COMMIT 或 ROLLBACK TRANSACTION 语句。上一计数 = 2,当前计数 = 3。