Sql server 如何在SQLServer2005中检索数据库中的所有索引及其数据类型?

Sql server 如何在SQLServer2005中检索数据库中的所有索引及其数据类型?,sql-server,indexing,maintenance-plan,Sql Server,Indexing,Maintenance Plan,我有一个维护任务失败了,因为数据库中有一些索引的类型不允许在线重建索引。脱机在我的行业中不是一个选项,因此我必须创建自己的T-SQL任务,在特定索引上重建索引。数据库很大,有很多表和索引,因此有没有办法查询系统中的所有索引及其数据类型?这将显示所有索引的所有列,包括“基表”的列,如堆和聚集索引,以及所有包含列: select o.name as [object_name], ix.name as [index_name], coalesce(c.name,cc.name) as

我有一个维护任务失败了,因为数据库中有一些索引的类型不允许在线重建索引。脱机在我的行业中不是一个选项,因此我必须创建自己的T-SQL任务,在特定索引上重建索引。数据库很大,有很多表和索引,因此有没有办法查询系统中的所有索引及其数据类型?

这将显示所有索引的所有列,包括“基表”的列,如堆和聚集索引,以及所有包含列:

select o.name as [object_name],
    ix.name as [index_name],
    coalesce(c.name,cc.name) as [column_name],
    t.name as [type_name],
    coalesce(c.max_length, cc.max_length) as max_length
from sys.indexes ix
join sys.objects o
    on ix.object_id = o.object_id
left join sys.index_columns ic
    on ix.object_id = ic.object_id
    and ix.index_id = ix.index_id
    and ix.index_id > 1
left join sys.columns c
    on  ic.object_id = c.object_id
    and ic.column_id = c.column_id
left join sys.columns cc
    on ix.object_id = cc.object_id
    and ix.index_id in (1,0)
join sys.types t
    on t.system_type_id = coalesce(c.system_type_id,cc.system_type_id)  
where o.type = 'U'
order by object_name, index_name, column_name;

然后,您可以识别那些对联机索引重建不安全的BLOB/XML/CLR类型。请注意,在SQL server 2012中,取消了对使用LOB列构建在线索引的限制,请参阅。

我们最近制定了此类过程。为此,它具有HasBlobs标志


考虑到这点,这正是我想要的。非常感谢。
ALTER PROCEDURE sp_rebuild_local_idexes
@RebuildClustered bit = 0
AS
BEGIN
    DECLARE @objectid int
    DECLARE @indexid int
    DECLARE @schemaname nvarchar(130)
    DECLARE @objectname nvarchar(130)
    DECLARE @indexname nvarchar(130)
    DECLARE @partitions bigint
    DECLARE @frag float
    DECLARE @command nvarchar(4000)
    DECLARE @HasBlobs bit
    DECLARE @index_type_desc nvarchar(255)

    -- Conditionally select tables and indexes from the sys.dm_db_index_physical_stats function
    -- and convert object and index IDs to names.

    SELECT
    --object_name(object_id),
    object_id AS objectid,
    index_id AS indexid,
    avg_fragmentation_in_percent AS frag,
    CASE WHEN (
        SELECT st.object_id
            from sys.tables st
            inner join sys.columns sc
                on st.object_id=sc.object_id
            inner join sys.types styp
                on sc.system_type_id=styp.system_type_id and sc.max_length=styp.max_length
            inner join sys.schemas ss
                on st.schema_id=ss.schema_id
            where styp.schema_id=4
                    and styp.name<>'sysname'
                    and styp.name IN ('xml','nvarchar','varchar','image','text','ntext')
                    AND st.object_id = a.object_id
            group by st.object_id
    )  IS NULL THEN 0 ELSE 1 END AS HasBlobs,
    a.index_type_desc
    INTO #work_to_do
    FROM sys.dm_db_index_physical_stats (DB_ID(), NULL, NULL , NULL, 'LIMITED') AS a
    WHERE avg_fragmentation_in_percent >= 5.0 AND index_id > 0
              AND a.index_type_desc IN ('CLUSTERED INDEX','NONCLUSTERED INDEX')           
    ORDER BY a.index_type_desc

    -- Declare the cursor for the list of partitions to be processed.
    DECLARE partitions CURSOR FOR SELECT objectid,
            indexid,
            frag,
            HasBlobs,
            index_type_desc from #work_to_do
select * from #work_to_do
    -- Open the cursor.
    OPEN partitions

    -- Loop through the partitions.
    WHILE (1=1)
    BEGIN
        FETCH NEXT FROM partitions
        INTO @objectid, @indexid, @frag,@HasBlobs,@index_type_desc

        IF @@FETCH_STATUS < 0 BREAK

        IF @RebuildClustered = 1 AND @index_type_desc != 'CLUSTERED INDEX'
            CONTINUE;

        SELECT @objectname = QUOTENAME(o.name), @schemaname = QUOTENAME(s.name)
        FROM sys.objects AS o
        JOIN sys.schemas as s ON s.schema_id = o.schema_id
        WHERE o.object_id = @objectid

        SELECT @indexname = QUOTENAME(name)
        FROM sys.indexes
        WHERE object_id = @objectid AND index_id = @indexid and type!=3

        -- 30 is an arbitrary decision point at which to switch between reorganizing and rebuilding.
        IF @frag < 30.0
            SET @command = N'ALTER INDEX ' + @indexname + N' ON ' + @schemaname + N'.' + @objectname + N' REORGANIZE'

        IF @frag >= 30.0
        BEGIN
            print @indexname+ @schemaname+@objectname
            SET @command = N'ALTER INDEX ' + @indexname +
                            N' ON ' +
                            @schemaname +
                            N'.' + @objectname +
                            N' REBUILD' +
                            CASE WHEN @HasBlobs = 0 THEN ' WITH(ONLINE=ON)' ELSE '' END
            print @command
        END

        EXEC (@command)
        PRINT N'Executed: ' + @command


    END

    -- Close and deallocate the cursor.
    CLOSE partitions
    DEALLOCATE partitions
END