I need to write a query on SQL server to get the list of columns in a particular table including the generate number, its associated data types (with length), if they are null (Yes, else No) and lastly its source including the database and table name.
For instance, looking at AdventureWorksDW
from table [dbo].[DimProduct]
, I can see:
CREATE TABLE [dbo].[DimProduct]
(
[ProductKey] [INT] IDENTITY(1,1) NOT NULL,
[ProductAlternateKey] [NVARCHAR](25) NULL,
[ProductSubcategoryKey] [INT] NULL,
[WeightUnitMeasureCode] [NCHAR](3) NULL,
[SizeUnitMeasureCode] [NCHAR](3) NULL,
[EnglishProductName] [NVARCHAR](50) NOT NULL,
...
I want to create a select statement to provide me the following result:
Number Name Type Is Null Source
---------------------------------------------------------------------------------------------
1 ProductKey int No AdventureWorksDW.dbo.DimProduct
2 ProductAlternateKey nvarchar(25) Yes AdventureWorksDW.dbo.DimProduct
3 ProductSubcategoryKey int Yes AdventureWorksDW.dbo.DimProduct
… … … … …