A very common question, I keep on getting is how to get column names for a particular tables. Well, it is pretty straightforward and easy to do. Let us see various methods to do it.
Method 1:
SELECT * FROM sys.columns WHERE object_id = OBJECT_ID('TableName')
Method 2:
SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = N'TableName'
Method 3:
SELECT SCHEMA_NAME(o.schema_id) SchemaName, o.Name, c.Name FROM sys.columns c JOIN sys.objects o ON o.object_id = c.object_id WHERE o.type = 'U' AND c.name = N'TableName' ORDER BY o.Name, c.Name
Method 4:
sp_help N'TableName'
Well, that’s it for today. Let me know what are your thoughts about this blog post. Let me know if you want me to create a video on this topic. I hope you like this blog post about how to Get Column Names of Table.
Here are my few recent videos and I would like to know what is your feedback about them. You can subscribe to my youtube channel here. Recently I have written a book about blogging. The name of the book is How to Become Successful Blogger and it is available to download via Kindle eBook over here. Let me know what you think about it.
- Forwarded Records and Performance – SQL in Sixty Seconds #155
- Hide Code in SSMS – SQL in Sixty Seconds #154
- Zoom in SSMS – SQL in Sixty Seconds #153
- Transfer Schema of Table – SQL in Sixty Seconds #152
- Find a Table in Execution Plan – SQL in Sixty Seconds #151
Reference:Â Pinal Dave (https://blog.sqlauthority.com)
3 Comments. Leave new
one more Method ti get it
SP_Columns TableName
Another option is the shortcut to highlight the name of your table in the SSMS IDE and hit Alt + F1. To my understanding that executes Method 4 (sp_help ‘object_name’) for you.
I have a personal preference for method #2 since I know I can easily refer to information_schema intellisense from SSMS. But for everyday use, I just drag/drop the columns from the table/column folder to the query editor.