SQL 2000:获取表的外键关系的 T-SQL

发布于 2024-07-25 23:05:47 字数 489 浏览 1 评论 0原文

与 SQL Server 2000 - 查询表的外键类似但不相同关系

我需要一个T-SQL语句,该语句将在给定表名的SQL 2000中工作,将返回该表的外键关系,例如

表MyFristTable有一个到MySecondTable的外键,其中MyFirstTable.ColA必须在MySecondTable中.ColB. 如果为 MyFirstTable 运行 sql 语句(或存储过程)并返回 NB 行的结果集,我会很高兴

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

:我有 SQL 2005 的示例,但它们不起作用,因为它们依赖 sys.foreign_key_columns

我宁愿不必解析 sp_help 语句的结果。

谢谢,

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,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

差↓一点笑了 2024-08-01 23:05:47

我必须为查询做同样的事情,在尝试了一个与 sys 表非常相似的版本后,我发现了这个存储过程:

exec sp_fkeys @fktable_name = 'foo'

看起来这可以在 SQL Server 2000
另外,我发现在某些情况下,此存储过程和此处的查询之间存在细微差别。 我猜 sp_fkeys 是规范版本。

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.

冰雪梦之恋 2024-08-01 23:05:47
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

注意 我认为只使用了 SQL 2000 中可用的系统视图(即 sysXXX 视图而不是 SQL 2005 sys.XXX 视图),但我实际上只在 SQL 2005 环境中对此进行了测试。

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.

葮薆情 2024-08-01 23:05:47

我曾经需要这样的东西,所以我只是查看系统存储过程的源代码,然后将我需要的内容复制到我自己的过程中,并使其按我需要的方式工作。

您可以查看 sp_helpconstraint 的源代码...

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...

梦里梦着梦中梦 2024-08-01 23:05:47

我在谷歌中发现了这个......所以如果这项工作不是我的优点。 希望有帮助

 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文