반응형


IF EXISTS (SELECT name FROM sysobjects WHERE id = OBJECT_ID('index_cleanup')
                    AND OBJECTPROPERTY(OBJECT_ID('index_cleanup'), 'IsProcedure')=1)

    DROP PROCEDURE index_cleanup
GO

CREATE PROCEDURE index_cleanup
    @tabname nvarchar(150)
AS

DECLARE @idx_name NVARCHAR(150)
DECLARE @drop_idx_string NVARCHAR(200)

SET NOCOUNT ON

IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES
                              WHERE table_type = 'base table' AND table_name = @tablename)
    BEGIN
        RAISERROR('The table : ''%s'' does not exist.', 16, 1, @tabname)
        RETURN (1)
    END

SET @tablename = OBJECT_ID(@tabname)
IF EXISTS (SELECT id FROM sysindexes
                 WHERE id = @tabname AND indid BETWEEN 1 AND 254
                     AND status IN (96,10485856,8388704))

BEGIN
    DECLARE idx_cursor CURSOR
        FOR SELECT name FROM sysindexes
        WHERE id = @tabname AND indid BETWEEN 1 AND 254
            AND status IN (96,10485856,8388704)

    OPEN idx_cursor
    FETCH NEXT FROM idx_cursor INTO @idx_name
    WHIL @@FETCH_STATUS = 0
    BEGIN
        SET @drop_idx_string = ('DROP STATISTICS' + OBJECT_NAME(@tabname) + '.' + @idx_name)
        EXECUTE(@drop_idx_string)
        FETCH NEXT FROM idx_cursor INTO @id_name
    END
CLOSE idx_cursor
DEALLOCATE idx_cursor
END
PRINT '    ***Statistics Deleted ***'

IF EXISTS (SEECT id FROM sysindexes
                 WHERE id = @tabname AND indid BETWEEN 1 AND 254
         AND status NOT IN (96,10485856,8388704))
BEGIN
   DECLARE idx_cursor CURSOR
      FOR SELECT name FROM sysindexes
   WHERE id=@tabname AND indid BETWEEN 1 AND 254
       AND status NOT IN (96,10485856,8388704)
   OPEN idx_cursor
   FETCH NEXT FROM idx_cursor INTO @idx_name
     WHILE @@FETCH_STATUS = 0
 BEGIN
    SET @drop_idx_string = ('DROP INDEX '+OBJECT_NAME(@tabname)+'.'+@idx_name)
    EXECUTE(@drop_idx_string)
    FETCH NEXT FROM idx_cursor INTO @idx_name
 END
   CLOSE idx_cursor
   DEALLOCATE idx_cursor
END
PRINT '     *** Indexes Deleted ***'

GO

END

반응형

'연구개발 > DBA' 카테고리의 다른 글

각 데이터베이스의 사용량 확인  (0) 2010.06.18
PAGE 구조 파악 할 시  (0) 2010.06.16
Linked Server 쿼리  (0) 2010.06.11
T-SQL::DB_Restore_move_to  (0) 2010.05.26
[2005 2008 NF] 크래시 복구, 온라인복원, 미러링  (0) 2010.05.26

+ Recent posts