Msg 102, Level 15, State 1, Line 1 Incorrect syntax near line 1 When running re-index script

  • I am trying to run the following script on SQL svr 2008. To re-index the Databases we use. EXEC sp_MSforeachtable @command1= "PRINT ‘?’ ALTER INDEX ALL ON ? REBUILD WITH (ONLINE = ON)"

    When I run it on our test DB prior to moving to production I get the following error once I execute the query.

    Msg 102, Level 15, State 1, Line 1

    Incorrect syntax near '‘'.

    Any ideas?

  • You've got the quotation marks wrong.

    Try this:

    EXEC sp_MSforeachtable @command1= 'PRINT ''?''; ALTER INDEX ALL ON ? REBUILD WITH (ONLINE = ON)'

    Note that the quotation marks around the command are single-quotes, and the quotation marks around the initial question mark are two single-quotes, not one double-quote.

    - Gus "GSquared", RSVP, OODA, MAP, NMVP, FAQ, SAT, SQL, DNA, RNA, UOI, IOU, AM, PM, AD, BC, BCE, USA, UN, CF, ROFL, LOL, ETC
    Property of The Thread

    "Nobody knows the age of the human race, but everyone agrees it's old enough to know better." - Anon

  • That did it! Thank you.

  • You might want to play it safe and put [] around the table name, in case you have some which are not valid identifiers or are reserved words.

    EXEC sp_MSforeachtable @command1= 'PRINT ''?''; ALTER INDEX ALL ON [?] REBUILD WITH (ONLINE = ON)'

    Do you really need to blanket rebuild every index every time?

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • pmcalister (7/11/2011)


    (ONLINE = ON)

    Also, with Standard edition this will be ignored

    -----------------------------------------------------------------------------------------------------------

    "Ya can't make an omelette without breaking just a few eggs" 😉

  • I have ran this on two test SQL servers. When it is executing it has ballooned one log file to 143GB. Then the server shut down because it used up all usable disk space.

    This script is definitely not what I want to use. If I ran it in production it would take the whole business down. I have gone back to a this standard script for re-indexing.

    EXEC sp_MSforeachtable @command1="print '?' DBCC DBREINDEX ('?')"

    GO

  • switch to bulk logged recovery mode for the index rebuilds and back to full afterwards.

    you should be using ALTER INDEX.. rather than DBCC DBREINDEX. Instead of blindly rebuilding indexes employ a routine to intelligently rebuild or reorganise based on fragmentation levels.

    There are many scripts available, have a look around

    -----------------------------------------------------------------------------------------------------------

    "Ya can't make an omelette without breaking just a few eggs" 😉

  • Thank you Perry. Will do.

  • Intelligent indexing script: http://sqlfool.com

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass

Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic. Login to reply