-- ============================================= -- Author: Evan -- Create date: 2018年6月14日 16点27分 -- Description: 冻结BOL不允许GI,或取消冻结 -- ============================================= ALTER PROCEDURE [dbo].[SP_FRU_FreezeGIByBOLorCancle] ( @BOL NVARCHAR(50) , @UpdateBy NVARCHAR(50) , @Flag INT --0表示冻结,1表示取消,2查询未GI的BOL的冻结信息 ) AS BEGIN --开始事务 BEGIN TRANSACTION --设置一个存储报错代码的变量 DECLARE @errorSum INT SET @errorSum = 0 --在每一条涉及到数据变动的语句后都累加一次报错代码 IF ( @Flag = 0 ) BEGIN UPDATE ddd WITH ( ROWLOCK ) SET ddd.STATE = 0 , ddd.UpdateBy = @UpdateBy , ddd.UpdateDate = GETDATE() FROM dbo.Fn_Split(@BOL, '/') fn LEFT JOIN dbo.D_ControlGIByBOL ddd ON fn.value = ddd.BOL WHERE ddd.ID IS NOT NULL; SET @errorSum += @@ERROR; INSERT INTO dbo.D_ControlGIByBOL ( ID , BOL , State , CreateBy , CreateDate ) SELECT NEWID() id , fn.value , 0 state , @UpdateBy createby , GETDATE() createdate FROM dbo.Fn_Split(@BOL, '/') fn LEFT JOIN dbo.D_ControlGIByBOL ddd ON fn.value = ddd.BOL WHERE ddd.ID IS NULL; SET @errorSum += @@ERROR; END IF ( @Flag = 1 ) BEGIN --------------------------------------------------取消冻结 UPDATE D_ControlGIByBOL WITH ( ROWLOCK ) SET State = 1 , UpdateBy = @UpdateBy , UpdateDate = GETDATE() WHERE BOL IN ( SELECT DISTINCT fn.value FROM dbo.Fn_Split(@BOL, '/') fn ) SET @errorSum += @@ERROR; END --报错代码不为0表示在执行上述sql时出现错误,回滚事务,撤销所有更改 IF ( @errorSum <> 0 ) BEGIN SELECT '执行错误:' + CAST(@errorSum AS VARCHAR(50)); ROLLBACK TRANSACTION END ELSE --报错代码为0表示无报错信息,可以执行更改操作 BEGIN IF ( @Flag = 0 ) BEGIN SELECT '批量冻结成功'; END IF ( @Flag = 1 ) BEGIN SELECT '批量取消冻结成功'; END IF ( @Flag = 2 ) BEGIN SELECT ddd.BOL,ddd.CreateBy,ddd.CreateDate,ddd.UpdateBy,ddd.UpdateDate, CASE WHEN ddd.State=0 THEN '冻结' WHEN ddd.State=1 THEN '未冻结' ELSE '异常' END as State FROM dbo.D_ControlGIByBOL ddd WHERE ddd.State = 0 ORDER BY CreateDate DESC END COMMIT TRANSACTION END END