• We use acursor driven proc but just altering it to be #temptbl oriented, you might handle it like this:

    DECLARE @SQLStatement nvarchar(4000)

    --DECLARE TableList CURSOR LOCAL FAST_FORWARD READ_ONLY FOR

    Declare @tmpReindexList table (SeqNo int identity(1,1) not null, SQLStatement nvarchar(4000) not null)

    insert into @tmpReindexList (SQLStatement)

    SELECT

    N'DBCC DBREINDEX (''['+ db_name() + '].' +

    QUOTENAME(TABLE_SCHEMA) +

    N'.' +

    QUOTENAME(TABLE_NAME)

    + ''')' as SQLStatement

    FROM

    INFORMATION_SCHEMA.TABLES

    WHERE EXISTS

    (

    SELECT *

    FROM sysindexes

    WHERE id =

    OBJECT_ID(QUOTENAME(TABLE_SCHEMA) +

    N'.' +

    QUOTENAME(TABLE_NAME))

    AND

    indid IN(0,1)

    )

    ORDER BY 1

    -- OPEN TableList

    declare @SeqNo int

    WHILE 1 = 1

    BEGIN

    --FETCH NEXT FROM TableList INTO @TableName

    Select top 1 @SeqNo = SeqNo, @SQLStatement=SQLStatement

    from @tmpReindexList

    if @@rowcount = 0 break

    print @SQLStatement

    EXEC(@SQLStatement)

    delete from @tmpReindexList where SeqNo = @SeqNo

    END

    -- CLOSE TableList

    -- DEALLOCATE TableList

    exec sp_updatestats

    dbcc updateusage(0) with count_rows

    btw I've kept the commented lines in the code so you can easily see the difference :hehe:

    Johan

    Learn to play, play to learn !

    Dont drive faster than your guardian angel can fly ...
    but keeping both feet on the ground wont get you anywhere :w00t:

    - How to post Performance Problems
    - How to post data/code to get the best help[/url]

    - How to prevent a sore throat after hours of presenting ppt

    press F1 for solution, press shift+F1 for urgent solution 😀

    Need a bit of Powershell? How about this

    Who am I ? Sometimes this is me but most of the time this is me