程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> 其他數據庫知識 >> 更多數據庫知識 >> 找出所有非xml索引並重新整理的sql

找出所有非xml索引並重新整理的sql

編輯:更多數據庫知識

復制代碼 代碼如下:
DECLARE cur CURSOR FOR
SELECT
[object_name]=s.name+'.'+OBJECT_NAME(A.object_id),
B.name
FROM sys.dm_db_index_physical_stats(DB_ID('AdventureWorks'),NULL,null,null,null) AS A
JOIN sys.indexes AS B
ON A.[object_id]=B.[object_id]
AND A.[index_id]=B.[index_id]
JOIN sys.objects AS o
ON A.[object_id]=o.[object_id]
JOIN sys.schemas AS s
ON o.[schema_id]=s.[schema_id]
WHERE A.[index_id]>0
AND NOT EXISTS(
SELECT *
FROM sys.xml_indexes
WHERE A.[object_id]=[object_id]
AND A.[index_id]=[index_id]
);
OPEN cur;
DECLARE @objname varchar(128),@indname varchar(128);
DECLARE @sql nvarchar(4000);
FETCH NEXT FROM cur INTO @objname,@indname;
--重整所有索引,在這裡先不管索引的碎片程度
WHILE @@FETCH_STATUS=0
BEGIN
SET @sql='ALTER INDEX '+@indname+' ON '+@objname+' REBUILD';
EXEC(@sql);
FETCH NEXT FROM cur INTO @objname,@indname;
END
CLOSE cur;
DEALLOCATE cur;

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved