Click here to monitor SSC
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in
Home       Members    Calendar    Who's On

Add to briefcase ««12

SQL to list the headers in a table? Expand / Collapse
Posted Tuesday, October 1, 2013 7:21 AM



Group: General Forum Members
Last Login: Tuesday, December 6, 2016 8:08 PM
Points: 16,145, Visits: 16,850
jpnasab 81740 (9/30/2013)
Create function [dbo].[AF_TableColumns](@table_name nvarchar(55))
returns nvarchar(4000) as
declare @str nvarchar(4000)
select @str = cast(rtrim(ltrim(column_name)) as nvarchar(500)) + coalesce(' ' + @str , ' ')
from information_schema.columns
where table_name = @table_name
group by table_name, column_name, ordinal_position
order by ordinal_position DESC
return @str

--select dbo.AF_TableColumns('YourTable') Select * from YourTable

Gosh that is a pretty limited function that doesn't help for this post at all. The OP is looking for the column names from a view.

And btw, you should not use the information_schema dmv's to view the schema. You should sys.objects.


Need help? Help us help you.

Read the article at for best practices on asking questions.

Need to split a string? Try Jeff Moden's splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Post #1500394
« Prev Topic | Next Topic »

Add to briefcase ««12

Permissions Expand / Collapse