事务

    --获取一个新的ID
    declare @newId bigint
    select @newId=MAX(ID) from BdRdRecord01
    
    begin tran
      if @newId<1000001000 or @newId is null
      begin
         set @newId=1000001000
         insert into BdRdRecord01(ID,bRdFlag) values (@newId,1)
      end
      else
      begin
         set @newId=@newId+1
         insert into BdRdRecord01(ID,bRdFlag) values (@newId,1)
      end
      
      if @newId<1000001000 or @newId is null
      begin
         rollback tran  ---回滚事务
         set @newId=-1
      end
      else
      begin
         commit tran    --执行事务
      end

     select @newId

 

你可能感兴趣的:(事务)