Comma Separated List of all columns in the Database (Tablename | Column_names…)

后端 未结 3 1554
深忆病人
深忆病人 2021-02-03 10:30

In SQL Server, I would like see Table_Name and all the Columns associated with that Table_Name in a database. So the Output should look like this:

      TABLE_N         


        
3条回答
  •  北荒
    北荒 (楼主)
    2021-02-03 11:07

    select name as TABLE_NAME,
           STUFF(COLUMN_NAME, 1, 1, '') AS COLUMN_NAME
    from sys.tables t
    CROSS APPLY 
    ( 
            SELECT 
                ',' + name AS [text()] 
            FROM 
                sys.columns c
            WHERE 
                c.object_id = t.object_id
            FOR XML PATH('') 
        ) o (COLUMN_NAME)
    

提交回复
热议问题