Home All Groups Group Topic Archive Search About

How to get the list of all SQL Server foreign keys in a database by using Microsoft.SqlServer.Manage

Author
7 Apr 2010 4:02 PM
Gilbert Tordeur
Hello.

I have found many post telling how to create a foreign key with SMO but no
one explaining how to list them.

Anybody knows ?

Thank you,
Gilbert

Author
7 Apr 2010 5:18 PM
Tom Dacon
"Gilbert Tordeur" <gilbert.tord***@orange.fr> wrote in message
news:epHLDvm1KHA.3744@TK2MSFTNGP04.phx.gbl...
> Hello.
>
> I have found many post telling how to create a foreign key with SMO but no
> one explaining how to list them.
>
> Anybody knows ?
>
> Thank you,
> Gilbert
>

On SQL Server, it's:

select * from sys.foreign_keys

works for sure on SQL Server 2005, don't know about others.

Tom Dacon
Dacon Software Consulting
Author
8 Apr 2010 7:02 AM
Gilbert Tordeur
Hi Tom.

Many thanks for your answer.

In fact I was looking for an answer using SMO, independent of any SQL Server
version and more appropriate - for me - with dotnet spirit. Another post
gave me the answer.

Regards,
Gilbert
Author
7 Apr 2010 5:28 PM
Patrice
> I have found many post telling how to create a foreign key with SMO but no
> one explaining how to list them.

Or try
http://msdn.microsoft.com/en-us/library/microsoft.sqlserver.management.smo.table.foreignkeys.aspx
if you want to keep using SMO.

Not familiar enough with SMO to know if you can query all FKs in one go...

--
Patrice
Author
8 Apr 2010 7:02 AM
Gilbert Tordeur
Hi Patrice.

Wonderful !

Thank you,
Gilbert.