将每个Row列值连接到SQL中以逗号分隔的字符串

时间:2015-05-07 09:06:14

标签: sql sql-server sql-server-2008

我正在使用AdventureWorks2008R2示例数据库。

我需要一个输出,每行应该连接成字符串(列值将用逗号分隔)

这是我的代码:

    create proc sales5
    as begin 
    declare @SalesPersonID int 

    create table #table 
    (
        id int identity(1,1) primary key clustered,
        SalesPersonID int
    )

    create table #table1
    ( 
        SalesPersonID int null , 
        PostalCode nvarchar (15) null,City varchar (30) null 
    ) 

    insert into #table 
       select distinct  
          SA.BusinessEntityID 
       from 
          Sales.SalesPerson SA 
       join 
          Sales.SalesOrderHeader S on S.SalesPersonID = SA.BusinessEntityID   
                                   and S.SalesPersonID is not null

   ;WITH CTE
AS (
    SELECT DISTINCT s.SalesPersonID
        , CAST(A.PostalCode AS NVARCHAR(25)) AS PostalCode
        , A.City
        , CAST(s.SalesPersonID AS VARCHAR(MAX)) AS SortOrder    
    FROM Sales.SalesOrderHeader S(NOLOCK)
        , Person.Person P(NOLOCK)
        , Person.Address A(NOLOCK)
        , #table t
    WHERE s.SalesPersonID IS NOT NULL
        AND S.SalesPersonID = P.BusinessEntityID
        AND S.SalesPersonID = A.AddressID
        AND S.SalesPersonID IN ('274')

    UNION ALL

    SELECT a.SalesOrderID
        , a.SalesOrderNumber
        , NULL
        , C.SortOrder + CAST(a.SalesOrderID AS VARCHAR(MAX))   
    FROM CTE C
    INNER JOIN Sales.SalesOrderHeader A(NOLOCK)
        ON A.SalesPersonID = C.SalesPersonID
    )
SELECT *
FROM cte
union all
 select 
         cast ( P.BusinessEntityID as nvarchar(50)),
         cast (P.NationalIDNumber as nvarchar(30)),
         cast (P.Gender as  nvarchar(20)),
         cast (P.BusinessEntityID as  VARCHAR(MAX)) + cast ('1' as  VARCHAR(MAX))
    from   HumanResources.Employee P(nolock)
    where exists(
    select 1 from CTE C
        where P.BusinessEntityID=C.SalesPersonID  )
ORDER BY SortOrder

    set nocount off  
end 

我的输出如下:

SalesPersonID    PostalCode   City    SortOrder
274                98027    Issaquah    274
274              502097814    M         2741
43849              SO43849   NULL     27443849
44082              SO44082   NULL     27444082
44508              SO44508   NULL     27444508
44532              SO44532   NULL     27444532

我的预期输出:

274,98027,Issaquah   
274,502097814,M        
43849,SO43849
44082,SO44082   
44508,SO44508    
44532,SO44532   

注意:不需要sortorder Column,这就是我在输出中的选择。

任何人请帮忙解决这个问题.. 提前谢谢。

1 个答案:

答案 0 :(得分:2)

您可以使用+CONCAT这样连接您的结果。

SELECT LTRIM(SalesPersonID) + ',' + LTRIM(PostalCode) + ISNULL(',' +  LTRIM(City),'') 
FROM CTE2 
ORDER BY SortOrder

完成查询

   ;WITH CTE
AS (
    SELECT DISTINCT s.SalesPersonID
        , CAST(A.PostalCode AS NVARCHAR(25)) AS PostalCode
        , A.City
        , CAST(s.SalesPersonID AS VARCHAR(MAX)) AS SortOrder    
    FROM Sales.SalesOrderHeader S(NOLOCK)
        , Person.Person P(NOLOCK)
        , Person.Address A(NOLOCK)
        , #table t
    WHERE s.SalesPersonID IS NOT NULL
        AND S.SalesPersonID = P.BusinessEntityID
        AND S.SalesPersonID = A.AddressID
        AND S.SalesPersonID IN ('274')

    UNION ALL

    SELECT a.SalesOrderID
        , a.SalesOrderNumber
        , NULL
        , C.SortOrder + CAST(a.SalesOrderID AS VARCHAR(MAX))   
    FROM CTE C
    INNER JOIN Sales.SalesOrderHeader A(NOLOCK)
        ON A.SalesPersonID = C.SalesPersonID
    ), CTE2 as 
    (
SELECT *
FROM cte
union all
 select 
         cast ( P.BusinessEntityID as nvarchar(50)),
         cast (P.NationalIDNumber as nvarchar(30)),
         cast (P.Gender as  nvarchar(20)),
         cast (P.BusinessEntityID as  VARCHAR(MAX)) + cast ('1' as  VARCHAR(MAX))
    from   HumanResources.Employee P(nolock)
    where exists(
    select 1 from CTE C
        where P.BusinessEntityID=C.SalesPersonID  )
)
SELECT LTRIM(SalesPersonID) + ',' + LTRIM(PostalCode) + ISNULL(',' +  LTRIM(City),'') 
FROM CTE2 
ORDER BY SortOrder