Jan 5, 2014

Sql query for list out all unique columns of tables including primary key of a database in sql server


Write a sql query or script to get or list out all unique columns of tables  including primary key of a database in sql server

--Change the table name 
DECLARE @TableName As VARCHAR(100) = 'Student'

SELECT c.name 
FROM sys.columns c INNER JOIN sys.indexes  i
ON i.index_id = c.column_id
AND i.object_id = c.object_id
WHERE i.is_unique = 1
AND c.object_id = OBJECT_ID(@TableName)

No comments:

Post a Comment