Getting Table Columns in SQL Server

Few ways to get table columns in SQL Server:

Using sp_help

   1: EXEC sp_help 'SalesLT.Customer'

Using INFORMATION_SCHEMA

   1: SELECT 
   2:     * 
   3: FROM 
   4:     INFORMATION_SCHEMA.COLUMNS
   5: WHERE 
   6:     TABLE_CATALOG = 'SalesLT' 
   7:     AND TABLE_SCHEMA = 'SalesLT'
   8:     AND TABLE_NAME = 'Customer'

Using sys.columns DMV

   1: SELECT [name]
   2: FROM sys.columns
   3: WHERE OBJECT_NAME(object_id) = 'Customer'

 

Another userful tip:

Sometimes we need to specify column names in our T-SQL statements (INSERT/UPDATE/DELETE). It is very tedious to try and write them ourselves. Good thing in SSMS, you can drag the columns folder onto the code editor and this will list out all the columns in the table.

SQL Server Tip On Getting Table Columns

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s