SQL事务示例2个

示例1:

ALTER PROCEDURE dbo.newstapein 

@program nvarchar(50), 
@tapecode nvarchar(50), 
@department nvarchar(50), 

AS 
/* SET NOCOUNT ON */  
if Exists(select * from ttape where program = @program and tapecode = @tapecode) 
begin 
  return -100 
end 
else
begin
  begin transaction 
  insert into ttape(program,tapecode,department) values(@program,@tapecode,@department)
  if @@error=0  
  begin 
  declare @tapeid int ,@pubdate datetime
  select top 1 @tapeid=tapeid ,@pubdate=pubdate from ttape order by tapeid desc
  if @@row_count<>0
  insert into tnewstapedetail(tapeid,pubdate) values(@tapeid,@pubdate)
  commit transaction  
  end
  else  
  begin  
  rollback transaction  
  return -1 
  end
end

示例2:

ALTER PROCEDURE [dbo].[AP_USER_UpdateCompanyList]
AS
BEGIN
 BEGIN TRANSACTION
 update AP_USER set CompanyList=dbo.GetDeptList_DY(DEPT_CODE)
 from DEPTCODE
 where WhichCompany=CONTENT
 and CHARINDEX('Z001',AuthList)>0
 and WhichCompany<>'集团本部'
 and UserName<>'admin'

 update AP_USER set CompanyList=dbo.GetDeptList_DY('01')
 from DEPTCODE
 where CHARINDEX('Z001',AuthList)>0
 and (WhichCompany='集团本部' or UserName='admin')
 if @@ERROR = 0 
  commit transaction
 else
  rollback transaction
END

posted @ 2011-10-21 11:17  流失的痕迹  阅读(349)  评论(0编辑  收藏  举报