sql >> Databasteknik >  >> RDS >> Sqlserver

Simulera group_concat MySQL-funktion i Microsoft SQL Server 2005?

Inget RIKTIGT enkelt sätt att göra detta på. Men många idéer där ute.

Bästa jag har hittat:

SELECT table_name, LEFT(column_names , LEN(column_names )-1) AS column_names
FROM information_schema.columns AS extern
CROSS APPLY
(
    SELECT column_name + ','
    FROM information_schema.columns AS intern
    WHERE extern.table_name = intern.table_name
    FOR XML PATH('')
) pre_trimmed (column_names)
GROUP BY table_name, column_names;

Eller en version som fungerar korrekt om data kan innehålla tecken som <

WITH extern
     AS (SELECT DISTINCT table_name
         FROM   INFORMATION_SCHEMA.COLUMNS)
SELECT table_name,
       LEFT(y.column_names, LEN(y.column_names) - 1) AS column_names
FROM   extern
       CROSS APPLY (SELECT column_name + ','
                    FROM   INFORMATION_SCHEMA.COLUMNS AS intern
                    WHERE  extern.table_name = intern.table_name
                    FOR XML PATH(''), TYPE) x (column_names)
       CROSS APPLY (SELECT x.column_names.value('.', 'NVARCHAR(MAX)')) y(column_names) 


  1. SQLite-tabellbegränsning unik och ON CONFLICT REPLACE-användning

  2. Hur man hittar den optimala unika identifieraren i en tabell i SQL Server:sp_special_columns

  3. Hur tar jag reda på vad som låser mina bord?

  4. MySQL SOUNDEX() Exempel