Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL 2000: T-SQL to get foreign key relationships for a table

Similar but NOT IDENTICAL to SQL Server 2000 - Query a Table’s Foreign Key relationships

I need a T-SQL statement that will work SQL 2000 that given a table name, will return the foreign key relationships for that table e.g.

Table MyFristTable has a foreign key to MySecondTable, where MyFirstTable.ColA must be in MySecondTable.ColB. I'd be delighted, if the sql statement (or stored proc) is ran for MyFirstTable and returned a result set on the lines of

Column | FK_Table      | FK_COLUMN
----------------------------------
ColA   | MySecondTable | ColB

NB: I have samples for SQL 2005 that won't work because they rely on sys.foreign_key_columns

I'd rather not have to parse out the results of the sp_help statement.

Thanks,

like image 359
Binary Worrier Avatar asked Nov 30 '22 20:11

Binary Worrier


1 Answers

I had to do this exact thing for a query, and I found this stored procedure, after trying a version much like the sys table one:

exec sp_fkeys @fktable_name = 'foo'

It looks like this is available in SQL Server 2000. Also, I found that in a few cases there were minor differences between this stored proc and the queries here. I'm guessing sp_fkeys is the canonical version.

like image 125
Chris Avatar answered Dec 02 '22 09:12

Chris