-- ================================================ -- Template generated from Template Explorer using: -- Create Procedure (New Menu).SQL -- -- Use the Specify Values for Template Parameters -- command (Ctrl-Shift-M) to fill in the parameter -- values below. -- -- This block of comments will not be included in -- the definition of the procedure. -- ================================================ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO -- ============================================= -- Author: 曹嘉杰 -- Create date: 2022/8/11 -- Description: 检查分包是否齐全 -- ============================================= IF (EXISTS (SELECT * FROM SYS.OBJECTS WHERE NAME = 'SP_Cust_PDA_CheckPackageComplete')) DROP PROC SP_Cust_PDA_CheckPackageComplete GO CREATE PROCEDURE SP_Cust_PDA_CheckPackageComplete -- Add the parameters for the stored procedure here @BarCodes AS NVARCHAR(MAX), --@PackageCodes AS NVARCHAR(MAX), @OrgCode AS NVARCHAR(50) AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON; -- Insert statements for procedure here DECLARE @SQL NVARCHAR(MAX) DECLARE @OrgID BIGINT SELECT @OrgID = ID FROM Base_Organization WHERE Code = @OrgCode IF OBJECT_ID('tempdb..#TempBarCodeTable') IS NOT NULL BEGIN DROP TABLE #TempBarCodeTable END CREATE TABLE #TempBarCodeTable ( BarCode NVARCHAR(500) ) SET @SQL = 'INSERT INTO #TempBarCodeTable SELECT ''' + REPLACE (@BarCodes,',',''' AS BarCode UNION SELECT ''') + '''' EXEC(@SQL) IF OBJECT_ID('tempdb..#TempPackageCodeTable') IS NOT NULL BEGIN DROP TABLE #TempPackageCodeTable END CREATE TABLE #TempPackageCodeTable ( PackageCode NVARCHAR(500), Segment77 NVARCHAR(50) ) INSERT INTO #TempPackageCodeTable SELECT DISTINCT CASE WHEN (SUBSTRING(Code, 1, 1) = 'C' AND LEN(Code) = 16) THEN SUBSTRING(Code, 1, 9) + '_' + SUBSTRING(Code, 12, 5) ELSE SUBSTRING(Code, 1, LEN(Code) - 5) END AS PackageCode, Segment77 AS Segment77 FROM BC_BarCode WHERE Code IN (SELECT BarCode FROM #TempBarCodeTable) -- AND Org = @OrgID AND BarCodeType = 3 AND ISNULL(Segment78, 1) <> 1 SELECT T_BarCode.Code, T_PackageCode.PackageCode FROM BC_BarCode T_BarCode INNER JOIN #TempPackageCodeTable T_PackageCode ON CASE WHEN (SUBSTRING(Code, 1, 1) = 'C' AND LEN(Code) = 16) THEN SUBSTRING(Code, 1, 9) + '_' + SUBSTRING(Code, 12, 5) ELSE CASE WHEN LEN(Code) > 5 THEN SUBSTRING(Code, 1, LEN(Code) - 5) ELSE '' END END = T_PackageCode.PackageCode AND T_BarCode.Segment77 = T_PackageCode.Segment77 WHERE T_BarCode.Code NOT IN (SELECT BarCode FROM #TempBarCodeTable) -- AND T_BarCode.Org = @OrgID AND ISNULL(T_BarCode.Segment78, 1) <> 1 END GO -- SELECT LEN('C220830030100007'), SUBSTRING('C220830030100007', 1, 1) --EXEC SP_Cust_PDA_CheckPackageComplete @BarCodes = 'C220829020100005,C220829020200005,C220825020200001,C220825020100001,11019999-00012-110-PO1102208300002-10-00001,11019999-00012-110-PO1102208300002-10-00002,11019999-00012-110-PO1102208300002-10-00003,11019999-00012-110-PO1102208300002-10-00004,11019999-00012-110-PO1102208300002-10-00005,M2120201-00030-110-PO1102208300002-20-00001', @OrgCode = '110'