sql – 连接数字的存储过程

我有一个存储过程将多个记录中的跟踪号码连接成一个.

下面的存储过程工作正常

ALTER PROCEDURE [dbo].[Proc1]
   @HWOrderID NVARCHAR(20)
AS
   DECLARE @concatn NVARCHAR(MAX)

   SELECT 
      @concatn = COALESCE(@concatn + ' ','') +
                 COALESCE(IT.TrackingNum + ', ','')  
   FROM 
      HWEvent as hwe
   INNER JOIN 
      HWOrderID AS hwo ON hwo.HWEventpk = hwe.HWEventpk
   LEFT OUTER JOIN  
      InventoryTransaction IT ON hwo.HWEventpk = IT.HWEventpk
   WHERE
       hwo.HWOrderID = 21000
       AND IT.FromPartypk = '13EF4AF5-0957-465C-BE20-C057EC970B5B'

   SELECT 
      @concatn AS 'FromSMHS'

   SET @concatn = null

我的结果:

FromSMHS
5560,  5561,  5562,  5563,  5564,  5565

但是……现在我需要从第二个表中添加额外的跟踪号码.当我加入其他表时,我得到重复的跟踪号码.

ALTER PROCEDURE [dbo].[Proc2]
    @HWOrderID NVARCHAR(20)
AS
   DECLARE @concatn NVARCHAR(MAX)

   SELECT 
      @concatn = COALESCE(@concatn + ' ','') +
                 COALESCE(IT.TrackingNum + ', ','') +
                 COALESCE(fexd.TrackingNumber + ', ','')  
   FROM
      HWEvent as hwe
   INNER JOIN 
      HWOrderID AS hwo ON hwo.HWEventpk = hwe.HWEventpk
   LEFT OUTER JOIN  
      InventoryTransaction IT ON hwo.HWEventpk = IT.HWEventpk
   LEFT OUTER JOIN   
      FedexTrackingReference fexr ON hwe.HWEventID = fexr.HWEventID
   LEFT OUTER JOIN  
      FedExTrackingDetail fexd ON fexr.FedexTrackingDetailpk =  fexd.FedExTrackingDetailpk
   WHERE
      hwo.HWOrderID = 21000
      AND IT.FromPartypk = '13EF4AF5-0957-465C-BE20-C057EC970B5B'

   SELECT @concatn AS 'FromSMHS'
   SET @concatn = null

我的结果:

FromSMHS
5560, 5561  5560, 8500,5560, 8501,  5560, 8502,  5562, 5561  5562, 8500,5562, 8501,  5562, 8502,  5563, 5561  5563, 8500,5563, 8501,  5563, 8502,  5564, 5561  5564, 8503, 8501,  5564, 8502,  5564, 5561  5564, 8503, 8501,  5564, 8502,  5565, 5561  5565, 8500,5565, 8501,  5565, 8502

我想要的结果应该是这样的:

FromSMHS
5560, 5561, 5562, 5563, 5564,  5565, 8500, 8501, 8502,8503

最佳答案 试试这个….

ALTER PROCEDURE [dbo].[Proc2]
@HWOrderID NVARCHAR(20)
AS
BEGIN
    SET NOCOUNT ON;

SELECT DISTINCT
       STUFF((SELECT ', ' +  CAST(TrackingNum AS VARCHAR(100))
              FROM InventoryTransaction
              WHERE HWEventpk = IT.HWEventpk
              FOR XML PATH(''),TYPE)
              .value('.','NVARCHAR(MAX)'),1,2,'')

 + ' ' +STUFF((SELECT ', ' +  CAST(TrackingNumber AS VARCHAR(100))
              FROM FedExTrackingDetail
              WHERE fexr.FedexTrackingDetailpk = FedexTrackingDetailpk
              FOR XML PATH(''),TYPE)
              .value('.','NVARCHAR(MAX)'),1,2,'')  AS Result_List             

FROM  HWEvent as hwe
INNER JOIN  HWOrderID AS hwo            ON hwo.HWEventpk = hwe.HWEventpk  AND hwo.HWOrderID = @HWOrderID
LEFT  JOIN  InventoryTransaction IT     ON hwo.HWEventpk = IT.HWEventpk
                                       AND IT.FromPartypk = '13EF4AF5-0957-465C-BE20-C057EC970B5B'
LEFT  JOIN  FedexTrackingReference fexr ON hwe.HWEventID = fexr.HWEventID
LEFT  JOIN  FedExTrackingDetail fexd    ON fexr.FedexTrackingDetailpk =  fexd.FedExTrackingDetailpk

END
点赞