SQL Server 2000 - Query a Table’s Foreign Key relationships

前端 未结 4 494
暖寄归人
暖寄归人 2021-01-03 03:14

Nearly identical to Query a Table's Foreign Key relationships, but for SQL Server 2000

For a given table \'foo\', I need a query to generate a set of ta

4条回答
  •  有刺的猬
    2021-01-03 03:42

    Parents and children

    /* this will find out all of the foreign key references for a table*/
    
    DECLARE @tableName varchar(128)
    SET @tableName = 'tCounter'
    
    SELECT   
        pt.[name] AS 'parentTable',  
        ct.[name] AS 'childTable',  
        fk.[name] AS 'fkName', 
    *   
    FROM sys.foreign_keys fk        
        INNER JOIN sys.tables pt              
            ON pt.object_ID = fk.parent_object_id        
        INNER JOIN sys.tables ct              
            ON ct.object_ID = fk.referenced_object_id   
    WHERE pt.name = @tableName      
        OR ct.name = @tableName   
    ORDER BY pt.name, ct.name
    

提交回复
热议问题