Sunday, February 19, 2012

extracting sql table column names

I am using the following to extract the column names of a table. I would like to do this for the whole database. Currently I am cutting the results into an excel spread. Is there a better way of doing this? Here is the query

SELECT name
FROM syscolumns
WHERE [id] = OBJECT_ID('tablename')

The following query uses the Information_Schema.Columns to list all columns, data type, and the table names.

Select C.Table_Catalog DB,C.Table_Schema, C.Table_Name, Column_Name, Data_Type

from Information_Schema.Columns C

join

Information_Schema.Tables T

on C.table_name = T.table_name

Where Table_Type = 'BASE TABLE'

Karl

|||

Thanks Karl,

I am sure this is a silly question, but what do the letters C and T represent?

KEB

|||

These are Aliases for the Views mentioned, its much easier to reference them in the select statement than typing the whole name as identifier again for the case that the views / tables have the same columnnames.

But anyway, use the following add-in to make sure that the columns you are getting are only from user created tables:

<Condition>

AND OBJECTPROPERTY(OBJECT_ID(table_name),'IsMSShipped') = 0

--its makes sure that only user (non microsoft shipped) created tables are displayed.


HTH, Jens Suessmeyer.

No comments:

Post a Comment