Caveman's Blog

My commitment to learning.

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

Posted by cavemansblog on March 31, 2008

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(@EmployeeList + ', ', '') +
CAST(account_num AS varchar(5))
FROM cl_accountSELECT @SingleLine

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

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>