views:

106

answers:

2

Hi,

I need to find out which table(name) a particular constraint belongs to.

Does anyone have any TSQL to achieve this?

A: 

many things could be considered to be a constraint:

primary key
foreign key
unique index
check constraint
column default

your question is a little vague. Do you know the name of the constraint, the type, etc.?

Based on the limited info in your question. I suggest that you look at the source code to the master.sys.sp_helpconstraint stored procedure.

In Sql Server Management Studio, using the Object Explorer, ust navigate to: "Databases" - "System Databases" - "master" - "Programmability" - "Stored Procedures" - "System Stored Procedures" - "sys.sp_helpconstraint". It contains all the tsql to query all the various kinds of constraints.

KM
+1  A: 

This will not find indexes which are in sys.indexes

SELECT
   OBJECT_NAME(o.parent_object_id)
FROM
   sys.objects o
WHERE
   o.name = 'MyConstraintName' AND o.parent_object_id <> 0
gbn