DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   MS SQL (http://www.daniweb.com/forums/forum127.html)
-   -   How do I get ONLY DATATYPE and NAME of all the columns available in given table_name (http://www.daniweb.com/forums/thread152321.html)

tonyaim83 Oct 20th, 2008 8:33 am
How do I get ONLY DATATYPE and NAME of all the columns available in given table_name
 
How do I get ONLY data type and name of all the columns available in given table_name

dickersonka Oct 20th, 2008 6:04 pm
Re: How do I get ONLY DATATYPE and NAME of all the columns available in given table_name
 
here is a couple more columns than you need, just so you can be clear on the query

SELECT
sysobjects.name AS "TABLE_NAME",
syscolumns.name AS "COLUMN_NAME",
systypes.name AS "DATA_TYPE",
syscolumns.LENGTH AS "LENGTH"
FROM       
        sysobjects
INNER JOIN
        syscolumns ON sysobjects.id = syscolumns.id
INNER JOIN                     
        systypes ON syscolumns.xtype = systypes.xtype
WHERE   
(sysobjects.xtype = 'U') and
sysobjects.name = 'MyTableName'
ORDER BY sysobjects.name, syscolumns.colid

substitute your table name in for 'MyTableName'

Geek-Master Oct 25th, 2008 9:47 pm
Re: How do I get ONLY DATATYPE and NAME of all the columns available in given table_name
 
In both SQL 2000 and 2005 you can run this query using the information_schema.columns

SELECT
    data_type,
    column_name
FROM information_schema.columns
WHERE table_name = 'table'

This should do it, just make sure to run this in the correct database. If you have two table names with the same name in different schemas you may need to change the WHERE clause to this

WHERE table_name = 'table'
AND table_schema = 'schema'


All times are GMT -4. The time now is 5:29 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC