SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Retrieve column headers for INFORMATION_SCHEMA


Retrieve column headers for INFORMATION_SCHEMA

Author
Message
at work
at work
SSC Journeyman
SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)

Group: General Forum Members
Points: 85 Visits: 20
Hi all,

I've been trying to do this for the past hour and I'm all googled out! Can anybody please point me in the right direction?

SQL SERVER 2005
I want to retrieve the schema for a particular table, say "table1" and display it in a web page and I can do that using:

SELECT * FROM
INFORMATION_SCHEMA.COLUMNS
WHERE table_name='table1'

But I also want the column headers of INFORMATION_SCHEMA.COLUMNS too. This is the closest I have got, but it is obviously throwing cast errors. There must be a simpler way without casting, but my schema skills aren't good enough to figure it out:

SELECT
'TABLE_CATALOG',
'TABLE_SCHEMA',
'TABLE_NAME',
'COLUMN_NAME',
'ORDINAL_POSITION',
'COLUMN_DEFAULT',
'IS_NULLABLE',
'DATA_TYPE',
'CHARACTER_MAXIMUM_LENGTH',
'CHARACTER_OCTET_LENGTH',
'ETC',
'ETC',
'ETC',
'ETC',
'ETC',
'ETC',
'ETC',
'ETC',
'ETC',
'ETC',
'ETC',
'ETC',
'ETC'
UNION ALL
SELECT * FROM
INFORMATION_SCHEMA.COLUMNS
WHERE table_name='table1'

Any help would be very much appreciated, thanks Smile
Lowell
Lowell
SSC Guru
SSC Guru (75K reputation)SSC Guru (75K reputation)SSC Guru (75K reputation)SSC Guru (75K reputation)SSC Guru (75K reputation)SSC Guru (75K reputation)SSC Guru (75K reputation)SSC Guru (75K reputation)

Group: General Forum Members
Points: 75745 Visits: 40993
{edit}ok, what are "column ?Headers...that's what i'm not understanding.

maybe I'm oversimplifying, but since the table_name exists in information_schema.columns, why not just select directly fromt here?

select * from
INFORMATION_SCHEMA.COLUMNS
where table_name ='mytable'

what did i miss on your question?

Lowell
--help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!
Lowell
Lowell
SSC Guru
SSC Guru (75K reputation)SSC Guru (75K reputation)SSC Guru (75K reputation)SSC Guru (75K reputation)SSC Guru (75K reputation)SSC Guru (75K reputation)SSC Guru (75K reputation)SSC Guru (75K reputation)

Group: General Forum Members
Points: 75745 Visits: 40993
Ok i think i know what you want;
in SSMS go to tools options.
check the shown checkbox, and when you copy/paste any results, the column names will be included:


Lowell
--help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!
at work
at work
SSC Journeyman
SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)SSC Journeyman (85 reputation)

Group: General Forum Members
Points: 85 Visits: 20
Hi Lowell,

Thanks for helping out.

Yes it is the column headers for the INFORMATION_SCHEMA that I am trying to return with the schema information e.g.

TABLE_CATALOG',
'TABLE_SCHEMA',
'TABLE_NAME',
'COLUMN_NAME',
'ORDINAL_POSITION'
etc etc etc

I saw the solution you have offered while I was google'ing, however, the results are being sent to a web page and the site is hosted in a shared environment and I don't have access to the options you are suggesting. I'm sure it can be achieved in a single sql query though, I just can't figure it out!

Any other ideas? Smile
Lowell
Lowell
SSC Guru
SSC Guru (75K reputation)SSC Guru (75K reputation)SSC Guru (75K reputation)SSC Guru (75K reputation)SSC Guru (75K reputation)SSC Guru (75K reputation)SSC Guru (75K reputation)SSC Guru (75K reputation)

Group: General Forum Members
Points: 75745 Visits: 40993
well, the web page itself would run the query, and stick the results in either an ADODB.Recordset, or a .NET DataTable or DataReader, right?
both of those have access to teh column names automatically... it's built in...
for example, in an asp page using ADODB recordset,
Response.Write " "
Do while not rs.EOF
For widgit = 0 to rs.Fields.Count -1
Response.Write rs(widgit).Name & ": " & rs(widgit).Value & "
"
Next 'widgit
rs.MoveNext
Loop


if it was .NET, it would be DataTable.Columns(0).ColumnName, iterating thru DataTable.Columns.Count -1

is that the question?

at work (2/11/2009)
Hi Lowell,

Thanks for helping out.

Yes it is the column headers for the INFORMATION_SCHEMA that I am trying to return with the schema information e.g.

TABLE_CATALOG',
'TABLE_SCHEMA',
'TABLE_NAME',
'COLUMN_NAME',
'ORDINAL_POSITION'
etc etc etc

I saw the solution you have offered while I was google'ing, however, the results are being sent to a web page and the site is hosted in a shared environment and I don't have access to the options you are suggesting. I'm sure it can be achieved in a single sql query though, I just can't figure it out!

Any other ideas? Smile


Lowell
--help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!
Vijaya Kadiyala
Vijaya Kadiyala
Hall of Fame
Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)Hall of Fame (3.1K reputation)

Group: General Forum Members
Points: 3093 Visits: 409
Hi
Lowell solution is right solution, so this way you are hard coding the column names any where in the query. This is so dynamic.

Thanks -- Vijaya Kadiyala
http://dotnetvj.blogspot.com

Thanks -- Vijaya Kadiyala
www.dotnetvj.com
SQL Server Articles For Beginers



Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search