SQL列出索引中没有特定列的所有表

时间:2015-07-01 02:27:53

标签: sql-server

我想列出索引中不包含特定列的所有表,但该列存在于表架构中。

我引用了https://docs.djangoproject.com/en/1.8/ref/models/querysets/,但查询列出了包含所有索引的所有表。

例如,如果表架构包含一列'日期'并且索引不包含列'日期'然后我想在我的结果中列出这个表。

1 个答案:

答案 0 :(得分:1)

DECLARE @search_column SYSNAME;

SET @search_column = 'Date';

SELECT s.name AS [Schema], t.name AS [Table]
FROM
    sys.schemas s
        INNER JOIN sys.tables t ON
            t.[schema_id] = s.[schema_id]
        INNER JOIN sys.columns c ON
            c.[object_id] = t.[object_id]
WHERE
    c.name = @search_column AND
    NOT EXISTS
    (
        SELECT *
        FROM
            sys.indexes i
                INNER JOIN sys.index_columns ic ON
                    ic.[object_id] = i.[object_id] AND
                    ic.index_id = i.index_id
        WHERE
            i.[object_id] = t.[object_id] AND
            ic.column_id = c.column_id
    )
ORDER BY
    s.name,
    t.name;