SQL Server 2012连接多行的字符串

时间:2014-02-06 12:14:50

标签: sql-server sql-server-2012 common-table-expression string-concatenation

[编辑]由于时间限制,我放弃了使用CTE并创建了一个返回连接字符串的函数:

CREATE FUNCTION fn_GetCategoryNamesAsString
(
    @lawID INT
)
RETURNS NVARCHAR(MAX)
AS
BEGIN

    DECLARE @categoryNames NVARCHAR(MAX)
    SET @categoryNames = ''

    DECLARE @categoryID INT

    DECLARE CUR CURSOR LOCAL FORWARD_ONLY READ_ONLY FOR 
        SELECT t1.LawCategoryID FROM [GWS].[dbo].[GWSMasterLawsLawCategories] t1 WHERE t1.LawID = @lawID
    OPEN CUR

    FETCH FROM CUR INTO @categoryID
    WHILE @@FETCH_STATUS = 0
    BEGIN
        SET @categoryNames = @categoryNames + (SELECT t2.Name
                                                        FROM [GWS].[dbo].GWSMasterLawCategories t2
                                                        WHERE t2.LawCategoryID = @categoryID) + ', '
        FETCH NEXT FROM CUR INTO @categoryID
    END
    CLOSE CUR
    DEALLOCATE CUR

    RETURN @categoryNames

END
GO

这可以胜任,但我真的不喜欢它。如果有人有更好的解决方案,我很想知道。

[结束编辑]

我已经看到几个问题,该交易大致相同的主题,但没有涵盖包含空值。

我正在编写一个查询,该查询应该返回一个表的全部内容,其中有几列添加了来自其他表的相关数据。这些列可以包含0 - n值。

空值必须存储为空字符串,并且具有额外数​​据的集合应以逗号分隔显示。

有些方法传递了所有串联的名称,有些只是单独返回值,有些根本没有值,最常见的是递归深入(这意味着我因为数据集很小而犯规)。

这是我目前的做法:

DECLARE @categoryNames NVARCHAR(MAX);
SET @categoryNames = '';

WITH sources (sourcesLawSourceID, sourcesName) AS (
    SELECT DISTINCT [LawSourceID], [name]
    FROM [GWS].[dbo].[GWSMasterLawSources]
),
categories AS(
    SELECT GWSCategories.LawCategoryID AS categoryID, GWSLawCategories.LawID AS lawID,
    categoryNames = @categoryNames 
    --CAST(LEFT( GWSCategories.name, CHARINDEX(',', GWSCategories.name + ',') -1)  AS NVARCHAR(MAX)) categoryName,
    --STUFF(GWSCategories.name, 1, CHARINDEX(',', GWSCategories.name + ','), '') categoryNames
    FROM [GWS].[dbo].[GWSMasterLawCategories] GWSCategories
    JOIN [GWS].[dbo].[GWSMasterLawsLawCategories] GWSLawCategories
    ON GWSCategories.LawCategoryID = GWSLawCategories.LawCategoryID

    UNION ALL

    SELECT categories.categoryID, categories.lawID, 
    CAST(LEFT( @categoryNames, CHARINDEX(',', @categoryNames + ',') -1)  AS NVARCHAR(MAX)) + GWSCategories.Name
    FROM categories
    JOIN [GWS].[dbo].[GWSMasterLawCategories] GWSCategories
    ON categories.categoryID = GWSCategories.LawCategoryID
    WHERE @categoryNames > ''
)
SELECT DISTINCT GWSMaster.[LawID]
      ,[Name]

      ,sources.sourcesName LawSourceName
      ,(SELECT STUFF((SELECT DISTINCT ', ' + RTRIM(LTRIM(categories.CategoryNames))
      FROM categories
      FOR XML PATH ('')), 1, 1, '')) Categories

  FROM [GWS].[dbo].[GWSMasterLaws] GWSMaster
  JOIN sources
  ON sources.sourcesLawSourceID = GWSMaster.LawSourceID
  JOIN categories
  ON categories.lawID = GWSMaster.LawID

这使类别名称字段完全为空。

如果我可以提供更多信息或者我错过了一个可以解决我问题的问题,请告诉我。

0 个答案:

没有答案