I need to write a query on SQL server to get the list of columns in a particular table, its associated data types (with length) and if they are not null. And I have managed to do this much.
But now i also need to get, in the same table, against a column – TRUE
if that column is a primary key.
How do i do this?
My expected output is:
Column name | Data type | Length | isnull | Pk
Best Answer
To avoid duplicate rows for some columns, use user_type_id instead of system_type_id.
Just replace
YourTableName
with your actual table name - works for SQL Server 2005 and up.In case you are using schemas, replace
YourTableName
byYourSchemaName.YourTableName
whereYourSchemaName
is the actual schema name andYourTableName
is the actual table name.