其他分享
首页 > 其他分享> > 递归、创建临时表

递归、创建临时表

作者:互联网


GO
/****** Object: StoredProcedure [dbo].[Proc_Drug_ScanTrackCodeTempInStock] Script Date: 2022/6/28 12:30:17 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: zhengwei
-- Create date: 20220610
-- Description: 扫码追溯码入库
-- =============================================
ALTER PROCEDURE [dbo].[Proc_Drug_ScanTrackCodeTempInStock]
@trackCode nvarchar(30),
@deptCode varchar(36),
@SupplierCode varchar(36),
@userName varchar(20),
@voucherCode varchar(55)
AS
BEGIN
SET NOCOUNT ON;

BEGIN TRY
BEGIN TRANSACTION;
DECLARE @trackCodeCount decimal(10,2),@goodsCode varchar(36),@goodsBath varchar(50),@ExpiryDate varchar(10),@InStockId varchar(55),@Msg varchar(500)
-- IF not exists(select * from T_Base_TrackCode where Code=@trackCode)
--BEGIN
--set @Msg = '追溯码['+@trackCode+']不存在,请检查!'
-- RAISERROR (@Msg, 16, 1) WITH NOWAIT
--RETURN 0;
--END
IF exists(select * from T_Base_TrackCode where Code=@trackCode and IsInStockScan=1)
BEGIN
set @Msg = '追溯码['+@trackCode+']已进行入库扫码,不能重复扫码!'
RAISERROR (@Msg, 16, 1) WITH NOWAIT
RETURN 0;
END

--获取所有子码,包括自己

CREATE TABLE #TrackCodeNew
(
Code VARCHAR(50) COLLATE database_default null,
ParentCode VARCHAR(50) COLLATE database_default null,
CodeLevel INT
);
WITH CodeInfo AS(
SELECT Code,ParentCode,CodeLevel FROM dbo.T_Base_TrackCode WHERE Code=@trackCode
UNION ALL
SELECT a.Code,a.ParentCode,a.CodeLevel FROM dbo.T_Base_TrackCode AS a,CodeInfo AS b WHERE a.ParentCode = b.Code
)
insert into #TrackCodeNew select Code,ParentCode,CodeLevel from CodeInfo

--#码为一级码数量就是要入库的数量
SELECT @trackCodeCount = count(*) FROM #TrackCodeNew WHERE CodeLevel=1

SELECT @goodsCode=GoodsCode,@goodsBath=GoodsBatch,@ExpiryDate=ExpiryDate from T_Base_TrackCode a inner join T_Base_TrackCodeProduct b on a.productId = b.Id where code = @trackCode;
IF exists(select * from Oper_InStockVoucherTemp where GoodsID=@goodsCode and GoodsBatch=@goodsBath and YXQ=@ExpiryDate)
BEGIN
PRINT '存在:'+@goodsCode+'----'+@goodsBath+'------'+@ExpiryDate
UPDATE Oper_InStockVoucherTemp set InStockCount = InStockCount+@trackCodeCount,ModifyOn=GETDATE() WHERE GoodsID=@goodsCode and GoodsBatch=@goodsBath and YXQ=@ExpiryDate
SELECT @InStockId =ID from Oper_InStockVoucherTemp WHERE GoodsID=@goodsCode and GoodsBatch=@goodsBath and YXQ=@ExpiryDate
END
ELSE
BEGIN
PRINT '不存在:'+@goodsCode+'----'+@goodsBath+'------'+@ExpiryDate

SET @InStockId = dbo.GetTicks() + '-' +LOWER(NEWID())
INSERT INTO dbo.Oper_InStockVoucherTemp (ID,VoucherCode, Warehouse,GoodsID ,GoodsBatch,YXQ ,InStockCount,Price,Supplier,SuppVoucherCode, Status,IsDeleted,CreatedOn, ModifyOn,CreatedBy,ModifyBy, VoucherLine,VoucherType,Unit,StorageType)
SELECT @InStockId,@voucherCode,@deptCode,b.GoodsCode,b.GoodsBatch,b.ExpiryDate,@trackCodeCount,isnull(b.Price,0),@SupplierCode,b.VoucherCode, 0,0,getdate(),GETDATE(),@userName,@userName, '1','3',b.Unit,'1'
FROM T_Base_TrackCode a
inner join T_Base_TrackCodeProduct b on a.productId = b.Id
inner join BaseGoodsInfo c on b.GoodsCode=c.GoodsCode
WHERE code = @trackCode;
END

--更新追溯码扫描信息、插入临时单据表

UPDATE T_Base_TrackCode SET IsInStockScan=1,inStockScanTime=getdate() WHERE code in (select Code from #TrackCodeNew);

INSERT INTO T_Base_VoucherTrackCodeTemp(VoucherId,TrackCode,isDelete,VoucherType) select @InStockId, Code,'0','3' from #TrackCodeNew

DROP TABLE #TrackCodeNew

COMMIT TRANSACTION;
END TRY
BEGIN CATCH
IF @@TRANCOUNT > 0
BEGIN
ROLLBACK TRANSACTION;
END;
DECLARE @ErrMsg NVARCHAR(4000) ,
@ErrSeverity INT;
SELECT @ErrMsg = ERROR_MESSAGE() ,
@ErrSeverity = ERROR_SEVERITY();
RAISERROR(@ErrMsg, @ErrSeverity, 1);
RETURN 0;
END CATCH;
RETURN 1;
END

 

标签:Code,varchar,递归,临时,创建,TrackCode,ExpiryDate,--,Base
来源: https://www.cnblogs.com/zhengwei-cq/p/16419325.html