Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Check - UNIQUE constraint/UNIQUE INDEX/PRIMARY KEY

Author  Topic 

vision.v1
Yak Posting Veteran

72 Posts

Posted - 2015-03-05 : 05:09:51
Hi,

I am using below script for checking existence of "UNIQUE CONSTRAINT" on a given table and on column but its not working correctly.

I think need to do some changes to the below script such that need to check some more scenarios like existance of "UNIQUE constraint"/"UNIQUE INDEX"/"PRIMARY KEY CONSTRAINT" before creating the new UNIQUE constraint can some one assist on the script checking before creating the new "UNIQUE CONSTRAINT".



SELECT
c.table_name,
c.constraint_name,
c.constraint_type,
k.column_name
FROM
INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS c
JOIN
INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS k
ON
c.constraint_schema = k.constraint_schema
AND
c.constraint_name = k.constraint_name
AND
c.constraint_type IN ('UNIQUE')
WHERE
c.table_name = @tableName
AND
k.column_name = @columnName

Thanks,
Vision.v1
   

- Advertisement -