Results 1 to 2 of 2

Thread: Alter Index - Syntax Mssql 2005

  1. #1
    Join Date
    May 2005
    Posts
    111

    Alter Index - Syntax Mssql 2005

    Please help---sql doesn't like me using a variable in the following statement.

    declare @tblName varchar(30)
    set @tblName = 'tblFOO'

    ALTER INDEX ALL ON @tblName
    REBUILD WITH (FILLFACTOR = 80,
    ONLINE = ON, SORT_IN_TEMPDB = ON,
    STATISTICS_NORECOMPUTE = OFF)

    receive the following error:
    Incorrect syntax near '@tblName'.

  2. #2
    Join Date
    Sep 2002
    Posts
    5,938
    Need dynamic sql, try this:

    exec ('ALTER INDEX ALL ON ' + @tblName
    + ' REBUILD WITH (FILLFACTOR = 80,
    ONLINE = ON, SORT_IN_TEMPDB = ON,
    STATISTICS_NORECOMPUTE = OFF)
    ')

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •