SQL Server SQL query to get columns of a composite index [duplicate]

pu82cl6c  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(98)

This question already has answers here:

Simulating group_concat MySQL function in Microsoft SQL Server 2005? (12 answers)
How to concatenate text from multiple rows into a single text string in SQL Server (48 answers)

Closed last month.

The following query returns the columns of a composite index in multiple rows.

Question: how can we write a query that returns all columns of a composite index in one row?

Example:

create table #t
(
    c int, 
    d varchar(5), 
    e varchar(10)
)

create index idx_test on #t (d, e)

Running the following query on tempdb returns two rows for the same index. I would like it to return one row under header column_names containing the columns d and e separated by , such as e,d

SELECT 
    i.name AS index_name,
    COL_NAME(ic.object_id, ic.column_id) AS column_names
FROM 
    sys.indexes AS i
INNER JOIN 
    sys.index_columns AS ic ON i.object_id = ic.object_id 
                            AND i.index_id = ic.index_id
WHERE 
    i.object_id = OBJECT_ID('#t');

Output:

index_namecolumn_names
idx_testd
idx_teste

Desired output:

|      index_name     |     column_names |
|---------------------|------------------|
|          idx_test   |         d,e      |
huwehgph

huwehgph1#

You just need to use STRING_AGG in your query and group by the index name - try this code:

SELECT 
    i.name AS index_name,
    STRING_AGG(COL_NAME(ic.object_id, ic.column_id), ', ') AS column_names
FROM 
    sys.indexes AS i
INNER JOIN 
    sys.index_columns AS ic ON i.object_id = ic.object_id 
                            AND i.index_id = ic.index_id
WHERE 
    -- updated to keep siggemannen happy ;-) 
    i.object_id = OBJECT_ID('tempdb..#t')
    -- or alternatively: use the known index name; there could be more than 1 index on that table ...
    -- i.name = 'idx_test'
GROUP BY
    i.name;

相关问题