Friday, 21 November 2014

Cet column name by comma separated sql server

Get column name by comma separated sql server :
To get all column name by comma seperated in sql ser execute the query:

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

 --------------------------------------------------------------------------
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)