Author |
Topic |
phil_mw60
Starting Member
4 Posts |
Posted - 2010-03-29 : 09:33:22
|
Hi,I have the following query which returns a list of constraints for the current database and the table name to which the constraint applies:select dc.name as [Constraint Name], dc.definition, so.name as [Table]from sys.default_constraints dcleft join dbo.sysobjects soon dc.parent_object_id = so.idand so.xtype = 'u'order by so.nameI would like to extend this query to show me the column that the constraint applies to.I have tried to join onto the syscolumns table but this returns numerous rows for each constraint.Does anyone have any ideas how to extend this query to show the column name? Thanks in advance,Phil |
|
russell
Pyro-ma-ni-yak
5072 Posts |
Posted - 2010-03-29 : 12:51:58
|
[code]select t.name [table], c.name [column], d.name [constraint]from sys.default_constraints djoin sys.tables ton t.object_id = d.parent_object_idjoin sys.columns con c.object_id = t.object_idand c.column_id = d.parent_column_id[/code] |
 |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
phil_mw60
Starting Member
4 Posts |
Posted - 2010-03-30 : 06:05:47
|
Hi,Thanks for your answers.I'm sure the INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE is probably easier but I have gone with Russell's solution of joining on the sys.columns table..I think I was almost there but didn't specify the join between sys.columns and sys.tables hence I was getting numerous rows returned!Thanks very much. |
 |
|
phil_mw60
Starting Member
4 Posts |
Posted - 2010-04-08 : 08:34:18
|
ps:Does anyone know of a SQL Server 2000 equivalent query to return details of table / column constraints for a given Database?I tried running:select t.name as [Table], dc.name as [Constraint Name], dc.definition, c.name as [ColumnName]from sys.default_constraints dcinner join sys.tables ton t.object_id = dc.parent_object_idinner join sys.columns con c.object_id = t.object_idand c.column_id = dc.parent_column_idorder by t.name, c.nameagainst a SQL 2000 instance but this fails stating invalid object names for sys.*Thanks!Phil |
 |
|
russell
Pyro-ma-ni-yak
5072 Posts |
Posted - 2010-04-08 : 10:06:05
|
[code]select o.name [table], c.name [column], object_name(d.constid) [constraint]from sysconstraints djoin sysobjects oon o.id = d.idjoin syscolumns con c.id = o.idand c.colid = d.colidwhere d.status & 5 = 5and o.xtype = 'U'[/code]have a look at these:sysconstraints http://msdn.microsoft.com/en-us/library/aa260399(SQL.80).aspxsyscolumns http://msdn.microsoft.com/en-us/library/aa260398(SQL.80).aspxsysobjects http://msdn.microsoft.com/en-us/library/aa260447(SQL.80).aspx |
 |
|
phil_mw60
Starting Member
4 Posts |
Posted - 2010-04-08 : 11:30:05
|
Hi Russell,Thanks, that query works a treat in my SQL 2000 database.I don't suppose it's possible to include the Constraint definition in SQL 2000 in the same way as selecting the sys.default_constraints.definition column in SQL 2005 / 2008?Thanks again,Phil |
 |
|
russell
Pyro-ma-ni-yak
5072 Posts |
Posted - 2010-04-08 : 12:40:14
|
look at the syscomments table.http://msdn.microsoft.com/en-us/library/aa260393(SQL.80).aspxselect o.name [table], c.name [column], object_name(d.constid) [constraint], cm.textfrom sysconstraints djoin sysobjects oon o.id = d.idjoin syscolumns con c.id = o.idand c.colid = d.colidjoin syscomments cmon cm.id = d.constidwhere d.status & 5 = 5and o.xtype = 'U' |
 |
|
|