Caveman's Blog

My commitment to learning.

SQL Server: how to print all the column names of a query output

leave a comment »


How to print all the column names of a query output ?

DECLARE @Fields as NVARCHAR(3000)
set @Fields = ''SELECT @Fields = @Fields + ', ' + COLUMN_NAME FROM .INFORMATION_SCHEMA.COLUMNS
where Table_Name = 'Employee'

print SUBSTRING(@Fields, 3, len(@Fields))

–OutPut
–emp_id, emp_name, emp_age, emp_salary

how to return multiple rows of a query output as one string?

DECLARE @SingleLine varchar(100)
SELECT top 10 @SingleLine = COALESCE(@SingleLine + ', ', '') +
CAST(account_num AS varchar(5))
FROM cl_account
SELECT @SingleLine

–OutPut
–*, 70, 71, 72, 73, 74, 75, 76, 77, 78

Advertisements

Written by cavemansblog

March 31, 2008 at 6:59 pm

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: