问题
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,
回答1:
DECLARE @tableName sysname
SET @tableName = '' -- Your table name goes here
SELECT
c.name
, target.name
, targetc.name
FROM
-- source table
sysobjects t
-- source column
INNER JOIN syscolumns c ON t.id = c.id
-- general constraint
INNER JOIN sysconstraints co ON t.id = co.id AND co.colid = c.colid
-- foreign key constraint
INNER JOIN sysforeignkeys fk ON co.constid = fk.constid
-- target table
INNER JOIN sysobjects target ON fk.rkeyid = target.id
-- target column
INNER JOIN syscolumns targetc ON fk.rkey = targetc.colid AND fk.rkeyid = targetc.id
WHERE
t.name = @tableName
NOTE I have I think used only those system views available in SQL 2000 (ie the sysXXX ones rather than the SQL 2005 sys.XXX ones) but I have only actually tested this in a SQL 2005 environemnt.
回答2:
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.
回答3:
I needed something like this once, so I just looked at the source code of the system stored procedure and copied what I needed into my own procedure and made it work as I needed.
You might look at sp_helpconstraint's source code...
回答4:
I found this in google... so if this work is not my merit. Hope it help
SELECT
FK_Table = FK.TABLE_NAME,
FK_Column = CU.COLUMN_NAME,
PK_Table = PK.TABLE_NAME,
PK_Column = PT.COLUMN_NAME,
Constraint_Name = C.CONSTRAINT_NAME
FROM
INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS C
INNER JOIN
INFORMATION_SCHEMA.TABLE_CONSTRAINTS FK
ON C.CONSTRAINT_NAME = FK.CONSTRAINT_NAME
INNER JOIN
INFORMATION_SCHEMA.TABLE_CONSTRAINTS PK
ON C.UNIQUE_CONSTRAINT_NAME = PK.CONSTRAINT_NAME
INNER JOIN
INFORMATION_SCHEMA.KEY_COLUMN_USAGE CU
ON C.CONSTRAINT_NAME = CU.CONSTRAINT_NAME
INNER JOIN
(
SELECT
i1.TABLE_NAME, i2.COLUMN_NAME
FROM
INFORMATION_SCHEMA.TABLE_CONSTRAINTS i1
INNER JOIN
INFORMATION_SCHEMA.KEY_COLUMN_USAGE i2
ON i1.CONSTRAINT_NAME = i2.CONSTRAINT_NAME
WHERE i1.CONSTRAINT_TYPE = 'PRIMARY KEY'
) PT
ON PT.TABLE_NAME = PK.TABLE_NAME
WHERE PK.TABLE_NAME='something' -- the table for you are asking
来源:https://stackoverflow.com/questions/1026673/sql-2000-t-sql-to-get-foreign-key-relationships-for-a-table