Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
How may I get a list of all the column names in a table in my database?All help appreciated.
DustinMichaels
Constraint Violating Yak Guru
464 Posts
Posted - 2004-09-15 : 16:14:14
Query the system tables.
SELECT dbo.sysobjects.name AS ObjectName, dbo.sysobjects.id AS ObjectID, dbo.syscolumns.name AS ColumnName, dbo.systypes.name AS DataType, dbo.syscolumns.length,FROM dbo.sysobjects INNER JOIN dbo.syscolumns ON (dbo.sysobjects.id = dbo.syscolumns.id) INNER JOIN dbo.systypes ON (dbo.syscolumns.xusertype = dbo.systypes.xusertype)WHERE dbo.sysobjects.name = @NameOfTableOrView
rohans
Posting Yak Master
194 Posts
Posted - 2004-09-15 : 16:32:48
Thankx but I got something that worked and it seem a bit shorterselect col.name,obj.name, obj.type, obj.xtype from sysobjects objinner join syscolumns colon col.id = obj.idwhere obj.[name] like '%tablename%'All help appreciated.
tkizer
Almighty SQL Goddess
38200 Posts
Posted - 2004-09-15 : 16:34:52
Do not use the system objects for this. Use the INFORMATION_SCHEMA views. There is a view called COLUMNS that will give you what you need. SELECT *FROM INFORMATION_SCHEMA.COLUMNSModify the query as needed.Tara
rohans
Posting Yak Master
194 Posts
Posted - 2004-09-15 : 16:45:51
Tara, Great, I never knew about the information schema, it will now save me nuff heart ache where getting info on the DB is concerned.All help appreciated.