sql server 存储过程、事务,增删改

CREATE procedure [dbo].[sp_TableSave]        
@TypeID tinyint,    --     0 新增,1 修改,2 删除        
@ID int,        
@Name nvarchar(32),        
@ProductIDs varchar(1024),        
@BackColor nvarchar(128),        
@UID int,        
@Creator nvarchar(32),        
@Error nvarchar(100)='1' output        
as        
set nocount on         
begin try        
begin transaction    --    开始事务
 declare @currDate SMALLDATETIME      
 SET @currDate=GETDATE()     
 if(@TypeID='1')  --修改        
 begin        
   update ProductActivity set Name=@Name,ProductIDs=@ProductIDs,Backcolor=@BackColor,IsPush=1 where ActivityID=@ID        
           
   -- 更新移除产品log        
   insert into OperateLog(UID,UserName,Title,Content,CreateTime)        
   select @UID,@Creator,'更新',CAST(ProductID as varchar(10))+' /Activity:0',@currDate       
   from Product AS p      
   WHERE ActivityID=@ID and NOT EXISTS(      
  select 1 from dbo.F_split(@ProductIDs,',') AS t where t.f=p.ProductID      
   )      
         
   -- 更新移除产品      
   update p set ActivityID=0,IsPush=(case when p.status=0 then 1 else 0 end)      
   from Product AS p      
   WHERE ActivityID=@ID and NOT EXISTS(      
  select 1 from dbo.F_split(@ProductIDs,',') AS t where t.f=p.ProductID      
   )       
         
   -- 更新新增产品log      
   insert into OperateLog(UID,UserName,Title,Content,CreateTime)        
   select @UID,@Creator,'更新',CAST(ProductID as varchar(10))+' /Activity:'+CAST(@ID as varchar(10)),@currDate       
   from Product AS p      
   WHERE ActivityID<>@ID and EXISTS(      
  select 1 from dbo.F_split(@ProductIDs,',') AS t where t.f=p.ProductID      
   )      
           
   -- 更新新增产品      
   update p set ActivityID=@ID,IsPush=(case when p.status=0 then 1 else 0 end)      
   from Product AS p      
   WHERE ActivityID<>@ID and EXISTS(      
  select 1 from dbo.F_split(@ProductIDs,',') AS t where t.f=p.ProductID      
   )       
 end        
 else if(@TypeID='2') --删除        
 begin        
   -- 删除 ProductActivity      
   update ProductActivity set Status=1,IsPush=1 where ActivityID=@ID        
           
   -- 写入product改动log        
   insert into OperateLog(UID,UserName,Title,Content,CreateTime)        
   select @UID,@Creator,'更新',cast(ProductID as varchar(10))+'/Activity:0',@currDate from Product where ActivityID=@ID        
           
   -- 更新product        
   update Product set ActivityID=0,IsPush=(case when Product.status=0 then 1 else 0 end) where ActivityID=@ID        
 end        
 else      --新增        
 begin        
   -- 更新ProductActivity表        
   declare @NewID int        
   insert into ProductActivity(Name,ProductIDs,Backcolor,Creator,CreateTime,IsPush)         
   values(@Name,@ProductIDs,@BackColor,@Creator,@currDate,1)        
   set @NewID = SCOPE_IDENTITY()        
                
   -- 更新product表      
   update p set ActivityID=@NewID, IsPush=(case when p.Status=0 then 1 else 0 end)       
   FROM Product AS p      
   INNER JOIN dbo.F_split(@ProductIDs,',') AS t ON t.f=p.ProductID      
           
   -- 写product改动log        
   insert into OperateLog(UID,UserName,Title,Content,CreateTime)        
   select @UID,@Creator,'更新', CAST(ProductID as varchar(10))+'/Activity:'+CAST(@NewID as varchar(10)),@currDate       
   from Product where ActivityID=@NewID        
 end        
 set @Error='0'        
commit transaction  --    提交事务
return;        
end try        
begin catch     --    异常
  set @Error='1-'+ERROR_MESSAGE()        
  rollback transaction  --    回滚事务
  return;        
end catch

 

转载于:https://www.cnblogs.com/flywing/p/5006509.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值