Home > Database > Mysql Tutorial > How Can I Efficiently Concatenate Strings from Multiple Rows in SQL Server Without Using FOR XML or CLR Functions?

How Can I Efficiently Concatenate Strings from Multiple Rows in SQL Server Without Using FOR XML or CLR Functions?

DDD
Release: 2025-01-20 09:12:11
Original
919 people have browsed it

How Can I Efficiently Concatenate Strings from Multiple Rows in SQL Server Without Using FOR XML or CLR Functions?

SQL Server string concatenation method

Problem description:

You need to aggregate strings from multiple lines into a single line. COALESCE and FOR XML won't do what you need, and Azure doesn't support CLR-defined aggregate functions. You need to look for the best alternative.

Solution:

Using standard Transact SQL, you can concatenate strings efficiently by following these steps:

  1. Numbered rows: Partition the data based on grouping criteria and assign row numbers within each partition.
  2. Recursive join: Create a recursive CTE that iterates over the join string based on line numbers.
  3. Filter results: Only results with the highest line number are selected, ensuring complete connections.

Instructions:

This method uses three CTEs:

  • Partitioned: Assign row numbers to rows within each partition.
  • Concatenated: Recursively builds concatenated strings.
  • Final Result: Filters the concatenated rows to include only complete results.

To use this solution, you must specify grouping and sorting criteria. For example, in your case, rows with the same ID are concatenated and sorted alphabetically.

Example:

Consider the following data:

<code>ID  Name
-- --
1   Matt
1   Rocks
2   Stylus
3   Foo
3   Bar
3   Baz</code>
Copy after login

The query and its output are as follows:

<code class="language-sql">WITH Partitioned AS
(
    SELECT 
        ID,
        Name,
        ROW_NUMBER() OVER (PARTITION BY ID ORDER BY Name) AS NameNumber,
        COUNT(*) OVER (PARTITION BY ID) AS NameCount
    FROM dbo.SourceTable
),
Concatenated AS
(
    SELECT 
        ID, 
        CAST(Name AS nvarchar(max)) AS FullName, 
        Name, 
        NameNumber, 
        NameCount 
    FROM Partitioned 
    WHERE NameNumber = 1

    UNION ALL

    SELECT 
        P.ID, 
        CAST(C.FullName + ', ' + P.Name AS nvarchar(max)), 
        P.Name, 
        P.NameNumber, 
        P.NameCount
    FROM Partitioned AS P
        INNER JOIN Concatenated AS C 
                ON P.ID = C.ID 
                AND P.NameNumber = C.NameNumber + 1
)
SELECT 
    ID,
    FullName
FROM Concatenated
WHERE NameNumber = NameCount;</code>
Copy after login
<code>ID  FullName
-- ------------------------------
2   Stylus
3   Bar, Baz, Foo
1   Matt, Rocks</code>
Copy after login

Improvement description: Change the nvarchar type to nvarchar(max) to support longer string concatenation results. This avoids potential string length overflow errors.

The above is the detailed content of How Can I Efficiently Concatenate Strings from Multiple Rows in SQL Server Without Using FOR XML or CLR Functions?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template