CopyCode The Code is as follows: declare cur cursor
Select
[Object_name] = S. Name + '.' + object_name (A. object_id ),
B. Name
From SYS. dm_db_index_physical_stats (db_id ('adventureworks'), null) as
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;
-- Reassembles all indexes, regardless of the degree of fragmentation of the index.
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;