连接数字的存储过程

时间:2014-09-11 20:03:30

标签: sql sql-server stored-procedures

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

下面的存储过程可以正常工作

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

2 个答案:

答案 0 :(得分:1)

我会从2个表中合并结果然后我个人喜欢使用FOR XML PATH技巧从多行获取逗号分隔的字符串。

How to collate rows to a delimited string in SQL2008R2

答案 1 :(得分:1)

试试这个......

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