Excessive
Excessive

Reputation: 63

List all foreign keys of a table, multiple foreign keys to same table

I use the following SQL to query a given table's keys (primary and foreign keys) and their descriptions. I am using SQL Server 2005.

SELECT  c.name 'Column Name' ,
    t.name 'Data type' ,
    c.max_length 'Max Length' ,
    c.precision ,
    c.scale ,
    c.is_nullable ,
    ISNULL(i.is_primary_key, 0) 'Primary Key' ,
    CAST (( SELECT  COUNT(*)
            FROM    ( SELECT    cx.object_id
                      FROM      sys.foreign_key_columns fkc
                                INNER JOIN sys.columns cx ON fkc.parent_column_id = cx.column_id
                                                          AND fkc.parent_object_id = cx.object_id
                                INNER JOIN sys.columns cref ON fkc.referenced_column_id = cref.column_id
                                                          AND fkc.referenced_object_id = cref.object_id
                      WHERE     cx.column_id = c.column_id
                                AND fkc.parent_object_id = OBJECT_ID('[dbo].Cards')
                    ) xxx
          ) AS BIT) AS 'Foreign Key' ,
    ( SELECT    OBJECT_SCHEMA_NAME(fkc.referenced_object_id)
      FROM      sys.foreign_key_columns fkc
                INNER JOIN sys.columns cy ON fkc.parent_column_id = cy.column_id
                                             AND fkc.parent_object_id = cy.object_id
                INNER JOIN sys.columns cref ON fkc.referenced_column_id = cref.column_id
                                               AND fkc.referenced_object_id = cref.object_id
      WHERE     cy.column_id = c.column_id
                AND fkc.parent_object_id = OBJECT_ID('[dbo].Cards')
    ) 'Schema Name' ,
    ( SELECT    OBJECT_NAME(referenced_object_id)
      FROM      sys.foreign_key_columns fkc
                INNER JOIN sys.columns cy ON fkc.parent_column_id = cy.column_id
                                             AND fkc.parent_object_id = cy.object_id
                INNER JOIN sys.columns cref ON fkc.referenced_column_id = cref.column_id
                                               AND fkc.referenced_object_id = cref.object_id
      WHERE     cy.column_id = c.column_id
                AND fkc.parent_object_id = OBJECT_ID('[dbo].Cards')
    ) 'Referenced table' ,
    ( SELECT    cref.name
      FROM      sys.foreign_key_columns fkc
                INNER JOIN sys.columns cy ON fkc.parent_column_id = cy.column_id
                                             AND fkc.parent_object_id = cy.object_id
                INNER JOIN sys.columns cref ON fkc.referenced_column_id = cref.column_id
                                               AND fkc.referenced_object_id = cref.object_id
      WHERE     cy.column_id = c.column_id
                AND fkc.parent_object_id = OBJECT_ID('[dbo].Cards')
    ) 'Referenced column name' ,
    ( SELECT    sep.value [Description]
      FROM      sys.extended_properties sep
      WHERE     OBJECT_ID('[dbo].Cards') = sep.major_id
                AND c.column_id = sep.minor_id
    ) Description FROM sys.columns c
    INNER JOIN sys.types t ON c.user_type_id = t.user_type_id
    LEFT OUTER JOIN sys.index_columns ic ON ic.object_id = c.object_id
                                            AND ic.column_id = c.column_id
    LEFT OUTER JOIN sys.indexes i ON ic.object_id = i.object_id
                                     AND ic.index_id = i.index_id WHERE   c.object_id = OBJECT_ID('[dbo].Cards');

The query works very well for all tables, unless the table has two foreign keys to the same table.

The error I'm getting is,

"Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression."

What am I doing wrong?

Upvotes: 3

Views: 185

Answers (2)

sepupic
sepupic

Reputation: 8687

I didn't check your second query but the problem is still there: the subquery that returnes more than 1 value is this one

( SELECT    sep.value [Description]
  FROM      sys.extended_properties sep
  WHERE     OBJECT_ID('person.stateProvince') = sep.major_id
            AND c.column_id = sep.minor_id                 
) Description 

you should add additional condition in the where clause:

and class = 1 -- OBJECT_OR_COLUMN

your query does not consider more than 1 indexes for table, joining on c.column_id = sep.minor_id you cat get more rows because when the class is 7 (index) minor_id is the index id, not the column id

Upvotes: 1

Excessive
Excessive

Reputation: 63

I have solved my own problem.

Posting here in case anyone stumbles upon this question.

SELECT  c.name 'Column Name' ,
    t.name 'Data type' ,
    c.max_length 'Max Length' ,
    c.precision ,
    c.scale ,
    c.is_nullable ,
    ISNULL(i.is_primary_key, 0) 'Primary Key' ,
    CAST (( SELECT  COUNT(*)
            FROM    ( SELECT    cx.object_id
                      FROM      sys.foreign_key_columns fkc
                                INNER JOIN sys.columns cx ON fkc.parent_column_id = cx.column_id
                                                          AND fkc.parent_object_id = cx.object_id
                                INNER JOIN sys.columns cref ON fkc.referenced_column_id = cref.column_id
                                                          AND fkc.referenced_object_id = cref.object_id
                      WHERE     cx.column_id = c.column_id
                                AND fkc.parent_object_id = OBJECT_ID('[dbo].Cards')
                    ) xxx
          ) AS BIT) AS 'Foreign Key' ,
    ( SELECT    TOP 1 OBJECT_SCHEMA_NAME(fkc.referenced_object_id)
      FROM      sys.foreign_key_columns fkc
                INNER JOIN sys.columns cy ON fkc.parent_column_id = cy.column_id
                                             AND fkc.parent_object_id = cy.object_id
                INNER JOIN sys.columns cref ON fkc.referenced_column_id = cref.column_id
                                               AND fkc.referenced_object_id = cref.object_id
      WHERE     cy.column_id = c.column_id
                AND fkc.parent_object_id = OBJECT_ID('[dbo].Cards')
    ) 'Schema Name' ,
    ( SELECT    TOP 1 OBJECT_NAME(referenced_object_id)
      FROM      sys.foreign_key_columns fkc
                INNER JOIN sys.columns cy ON fkc.parent_column_id = cy.column_id
                                             AND fkc.parent_object_id = cy.object_id
                INNER JOIN sys.columns cref ON fkc.referenced_column_id = cref.column_id
                                               AND fkc.referenced_object_id = cref.object_id
      WHERE     cy.column_id = c.column_id
                AND fkc.parent_object_id = OBJECT_ID('[dbo].Cards')
    ) 'Referenced table' ,
    ( SELECT    TOP 1 cref.name
      FROM      sys.foreign_key_columns fkc
                INNER JOIN sys.columns cy ON fkc.parent_column_id = cy.column_id
                                             AND fkc.parent_object_id = cy.object_id
                INNER JOIN sys.columns cref ON fkc.referenced_column_id = cref.column_id
                                               AND fkc.referenced_object_id = cref.object_id
      WHERE     cy.column_id = c.column_id
                AND fkc.parent_object_id = OBJECT_ID('[dbo].Cards')
    ) 'Referenced column name' ,
    ( SELECT    sep.value [Description]
      FROM      sys.extended_properties sep
      WHERE     OBJECT_ID('[dbo].Cards') = sep.major_id
                AND c.column_id = sep.minor_id
    ) Description FROM    sys.columns c
    INNER JOIN sys.types t ON c.user_type_id = t.user_type_id
    LEFT OUTER JOIN sys.index_columns ic ON ic.object_id = c.object_id
                                            AND ic.column_id = c.column_id
    LEFT OUTER JOIN sys.indexes i ON ic.object_id = i.object_id
                                     AND ic.index_id = i.index_id WHERE   c.object_id = OBJECT_ID('[dbo].Cards');

Upvotes: 0

Related Questions