views:

46

answers:

1

Hi,

I have a table with a column that needs the data type upgrading. However running any alter script causes errors due to a unnamed unique constraint.

I need to drop this constraint but unfortunately I do not know the name. I have a script which currently lists all the unique constraints on the table but I need to find out how to go that one step further and associate the constraint names with the columns.

Select *
From sysobjects
Where sysobjects.xtype = 'UQ' AND sysobjects.parent_obj= OBJECT_ID(N'Users')

this returns

UQ__Users__45F365D3
UQ__Users__46E78AOC

I need to know which columns theses are linked to in order to delete the right one. I need to support SQL 2000, 2005, and 2008.

Any suggestions would be appreciated.

Thanks Ben

+2  A: 

You should be able to use INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE to establish this.

SELECT 
   CONSTRAINT_NAME 
FROM 
   INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE 
WHERE 
   TABLE_NAME = 'TableName' 
   AND COLUMN_NAME = 'ColumnName'

Not certain whether the view is fully supported in SQL 2000 though.

JamWheel
Thanks JamWheel. This seems to work fine and as expected on SQL 2000, 2005, and 2008!
Ben Cawley
If you post code or XML, **please** highlight those lines in the text editor and click on the "code" button (101 010) on the editor toolbar to nicely format and syntax highlight it!
marc_s