Friday, June 3, 2011

SQL Server : To Retrive Columns of an Object

To find the all the columns of a db objects like table  and view we can use below approach using SQL Sever meta data objects like sys.objects and sys.columns.

USE AdventureWorks2008
GO
SELECT DISTINCT c.name [Column Name]
FROM sys.columns c
INNER JOIN sys.objects o on c.object_id=o.object_id

WHERE object_id('Production.Product')=o.object_id
ORDER BY [Column Name]


Where the Production.Product is a db table and can be substituted by any other table or view.

Partial result:
-------------------------------------------------

Column Name
Class
Color
DaysToManufacture
DiscontinuedDate
FinishedGoodsFlag


Reference : DP (http://dptechnicalblog.blogspot.com/ )

No comments:

Post a Comment