如何使用游标列出所有数据库(SQL Server)中的所有索引以及与索引相关的对象

时间:2019-01-05 02:09:06

标签: sql sql-server cursor data-dictionary

我想列出所有数据库(在我的情况下为48)中所有与索引和索引相关的对象,并决定使用游标。我在堆栈溢出中发现了一个代码,该代码对于给定的一个数据库执行完全相同的操作,但修改为使用游标,以便我可以在所有数据库中获得相同的信息。问题是代码似乎还可以,但是当我运行时它给了我错误。如何修复此代码并使其运行?谢谢

-- SQL query to collect  all indexes and related objects 
-- instance level from multiple databases 
DECLARE @db_nm VARCHAR(250), @sql NVARCHAR(1000)

DECLARE @results TABLE
                 (
                      [database_name] sysname,
                      [table_view] sysname,
                      [object_type] sysname,
                      [constraint_type] sysname, 
                      [constraint_name] sysname,
                      [columns] sysname,
                      [index_name] sysname,
                      [index_type] sysname
                 );

DECLARE db CURSOR LOCAL FAST_FORWARD FOR
    SELECT db.name
    FROM master.sys.databases AS db
    WHERE db.name NOT IN ('master', 'tempdb', 'model', 'msdb') 
      AND db.state_desc = 'ONLINE' --system db you're probably not looking for info in

OPEN db ;

FETCH NEXT FROM db INTO @db_nm;

WHILE @@fetch_status = 0
BEGIN
    -- select * from sys.objects t
    -- select schema_name(t.schema_id) + '.' + t.[name] as table_view from ---sys.objects t

    SET @sql= 'select  ''' + @db_nm + ''' AS [database_name], schema_name(t.schema_id) + t.[name] as [table_view],
      case when t.[type] = 'U' Then 'Table'
       case when t.[type] = 'V' then 'View'
        end as [object_type],
    case when c.[type] = 'PK' then 'Primary key'
        when c.[type] = 'UQ' then 'Unique constraint'
        when i.[type] = 1 then 'Unique clustered index'
        when i.type = 2 then 'Unique index'
        end as [constraint_type], 
    c.[name] as [constraint_name],
    substring(column_names, 1, len(column_names)-1) as [columns],
    i.[name] as [index_name],
    case when i.[type] = 1 then 'Clustered index'
        when i.type = 2 then 'Index'
        end as [index_type]
from [' + @db_nm + '].sys.objects t
    left outer join [' + @db_nm + '].sys.indexes i
        on t.object_id = i.object_id
    left outer join [' + @db_nm + '].sys.key_constraints c
        on i.object_id = c.parent_object_id 
        and i.index_id = c.unique_index_id
   cross apply (select col.[name] + ', '
                    from [' + @db_nm + '].sys.index_columns ic
                        inner join [' + @db_nm + '].sys.columns col
                            on ic.object_id = col.object_id
                            and ic.column_id = col.column_id
                    where ic.object_id = t.object_id
                        and ic.index_id = i.index_id
                            order by col.column_id
                            for xml path ('') ) D (column_names)
where is_unique = 1
and t.is_ms_shipped <> 1
order by schema_name(t.schema_id) + '.' + t.[name]';

    INSERT INTO @results
        EXEC (@sql)

    FETCH NEXT FROM db INTO @db_nm;
END;

CLOSE db;
DEALLOCATE db;

-- select * into #temp from @results
SELECT * FROM @results;

--Here is the code that fits my need   for one given database but want to loop it for all databases 
SELECT
    schema_name(t.schema_id) + '.' + t.[name] as table_view, 
    CASE WHEN t.[type] = 'U' THEN 'Table'
         WHEN t.[type] = 'V' THEN 'View'
    END AS [object_type],
    CASE WHEN c.[type] = 'PK' THEN 'Primary key'
         WHEN c.[type] = 'UQ' THEN 'Unique constraint'
         WHEN i.[type] = 1 THEN 'Unique clustered index'
         WHEN i.type = 2 THEN 'Unique index'
    END AS constraint_type, 
    c.[name] AS constraint_name,
    substring(column_names, 1, len(column_names)-1) as [columns],
    i.[name] as index_name,
    case when i.[type] = 1 then 'Clustered index'
         when i.type = 2 then 'Index'
                                end as index_type
from sys.objects t
    left outer join sys.indexes i
        on t.object_id = i.object_id
    left outer join sys.key_constraints c
        on i.object_id = c.parent_object_id 
        and i.index_id = c.unique_index_id
   cross apply (select col.[name] + ', '
                    from sys.index_columns ic
                        inner join sys.columns col
                            on ic.object_id = col.object_id
                            and ic.column_id = col.column_id
                    where ic.object_id = t.object_id
                        and ic.index_id = i.index_id
                            order by col.column_id
                            for xml path ('') ) D (column_names)
where is_unique = 1
and t.is_ms_shipped <> 1
order by schema_name(t.schema_id) + '.' + t.[name]

0 个答案:

没有答案