Friday, October 24, 2008

MS SQL - Query to Get Columns Definition

To get the Column Definitions of a Given table in MS SQL, use this query:

SELECT so.name ,
sc.name ,
st.name ,
sc.length ,
CASE
WHEN sc.status = 0x80
THEN 'Y'
ELSE 'N'
END AS IsIdent ,
ColOrder
FROM sysobjects so
INNER JOIN syscolumns sc
ON so.id= sc.id
INNER JOIN systypes st
ON sc.xtype = st.xusertype
WHERE so.Name = 'YourTableName'
ORDER BY ColOrder

I used this for generating a Bulk Insert statement for a XML Rowset in C#.

No comments:

Post a Comment