943,577 Members | Top Members by Rank

Ad:
  • MS SQL Discussion Thread
  • Unsolved
  • Views: 968
  • MS SQL RSS
Oct 20th, 2008
0

How do I get ONLY DATATYPE and NAME of all the columns available in given table_name

Expand Post »
How do I get ONLY data type and name of all the columns available in given table_name
Reputation Points: 8
Solved Threads: 0
Light Poster
tonyaim83 is offline Offline
49 posts
since Aug 2007
Oct 20th, 2008
0

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

MS SQL Syntax (Toggle Plain Text)
  1. SELECT
  2. sysobjects.name AS "TABLE_NAME",
  3. syscolumns.name AS "COLUMN_NAME",
  4. systypes.name AS "DATA_TYPE",
  5. syscolumns.LENGTH AS "LENGTH"
  6. FROM
  7. sysobjects
  8. INNER JOIN
  9. syscolumns ON sysobjects.id = syscolumns.id
  10. INNER JOIN
  11. systypes ON syscolumns.xtype = systypes.xtype
  12. WHERE
  13. (sysobjects.xtype = 'U') AND
  14. sysobjects.name = 'MyTableName'
  15. ORDER BY sysobjects.name, syscolumns.colid

substitute your table name in for 'MyTableName'
Reputation Points: 133
Solved Threads: 141
Veteran Poster
dickersonka is offline Offline
1,162 posts
since Aug 2008
Oct 25th, 2008
0

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

MS SQL Syntax (Toggle Plain Text)
  1. SELECT
  2. data_type,
  3. column_name
  4. FROM information_schema.COLUMNS
  5. 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

MS SQL Syntax (Toggle Plain Text)
  1. WHERE table_name = 'table'
  2. AND table_schema = 'schema'
Reputation Points: 12
Solved Threads: 6
Junior Poster
Geek-Master is offline Offline
156 posts
since Dec 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MS SQL Forum Timeline: Trouble Ticket System
Next Thread in MS SQL Forum Timeline: Need help with a trigger





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC