Sqlserver
 sql >> Base de données >  >> RDS >> Sqlserver

Liste séparée par des virgules de toutes les colonnes de la base de données (tablename | Column_names...)

Select TABLE_SCHEMA, TABLE_NAME
    , Stuff(
        (
        Select ', ' + C.COLUMN_NAME
        From INFORMATION_SCHEMA.COLUMNS As C
        Where C.TABLE_SCHEMA = T.TABLE_SCHEMA
            And C.TABLE_NAME = T.TABLE_NAME
        Order By C.ORDINAL_POSITION
        For Xml Path('')
        ), 1, 2, '') As Columns
From INFORMATION_SCHEMA.TABLES As T

Comme mentionné dans les commentaires, ce qui précède inclura des vues. Si vous souhaitez exclure des vues, vous pouvez procéder comme suit :

Select T.TABLE_SCHEMA, T.TABLE_NAME
    , Stuff(
        (
        Select ', ' + C.COLUMN_NAME
        From INFORMATION_SCHEMA.COLUMNS As C
        Where C.TABLE_SCHEMA = T.TABLE_SCHEMA
            And C.TABLE_NAME = T.TABLE_NAME
        Order By C.ORDINAL_POSITION
        For Xml Path('')
        ), 1, 2, '') As Columns
From INFORMATION_SCHEMA.TABLES As T
    Left Join INFORMATION_SCHEMA.VIEWS As V
        On V.TABLE_SCHEMA = T.TABLE_SCHEMA
            And V.TABLE_NAME = T.TABLE_NAME
Where V.TABLE_NAME Is Null