Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Help with T-SQL Statement??

Help with T-SQL Statement??

Scheduled Pinned Locked Moved Database
databasetoolshelpquestion
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    ostinoh
    wrote on last edited by
    #1

    Hello - I'm in the process of using a script that will help with re-indexes. I keep getting the following errors. Msg 102, Level 15, State 1, Line 2 Incorrect syntax near '('. Msg 156, Level 15, State 1, Line 10 Incorrect syntax near the keyword 'ELSE'. My database name is Backdrop and the table is dbo.PJTRAN and the index is pjtran0. Here is the script if someone can help me understand why I keep getting those errors. IF EXISTS (SELECT [object_id],[index_id],[avg_fragmentation_in_percent] FROM sys.dm_db_index_physical_stats (DB_ID(N'Backdrop'), OBJECT_ID(N'PJTRAN.PJTRAN0'), NULL, NULL , 'DETAILED') WHERE [avg_fragmentation_in_percent] < 30) BEGIN ALTER INDEX ALL ON PJTRAN REORGANIZE END ELSE ALTER INDEX ALL ON PJTRAN REBUILD Thank you..

    D M 2 Replies Last reply
    0
    • O ostinoh

      Hello - I'm in the process of using a script that will help with re-indexes. I keep getting the following errors. Msg 102, Level 15, State 1, Line 2 Incorrect syntax near '('. Msg 156, Level 15, State 1, Line 10 Incorrect syntax near the keyword 'ELSE'. My database name is Backdrop and the table is dbo.PJTRAN and the index is pjtran0. Here is the script if someone can help me understand why I keep getting those errors. IF EXISTS (SELECT [object_id],[index_id],[avg_fragmentation_in_percent] FROM sys.dm_db_index_physical_stats (DB_ID(N'Backdrop'), OBJECT_ID(N'PJTRAN.PJTRAN0'), NULL, NULL , 'DETAILED') WHERE [avg_fragmentation_in_percent] < 30) BEGIN ALTER INDEX ALL ON PJTRAN REORGANIZE END ELSE ALTER INDEX ALL ON PJTRAN REBUILD Thank you..

      D Offline
      D Offline
      Don Burton
      wrote on last edited by
      #2

      Have you executed the script in Query Analyzer?

      O 1 Reply Last reply
      0
      • D Don Burton

        Have you executed the script in Query Analyzer?

        O Offline
        O Offline
        ostinoh
        wrote on last edited by
        #3

        This is for a SQL 2005 database. I'm using SSMS.

        1 Reply Last reply
        0
        • O ostinoh

          Hello - I'm in the process of using a script that will help with re-indexes. I keep getting the following errors. Msg 102, Level 15, State 1, Line 2 Incorrect syntax near '('. Msg 156, Level 15, State 1, Line 10 Incorrect syntax near the keyword 'ELSE'. My database name is Backdrop and the table is dbo.PJTRAN and the index is pjtran0. Here is the script if someone can help me understand why I keep getting those errors. IF EXISTS (SELECT [object_id],[index_id],[avg_fragmentation_in_percent] FROM sys.dm_db_index_physical_stats (DB_ID(N'Backdrop'), OBJECT_ID(N'PJTRAN.PJTRAN0'), NULL, NULL , 'DETAILED') WHERE [avg_fragmentation_in_percent] < 30) BEGIN ALTER INDEX ALL ON PJTRAN REORGANIZE END ELSE ALTER INDEX ALL ON PJTRAN REBUILD Thank you..

          M Offline
          M Offline
          Mycroft Holmes
          wrote on last edited by
          #4

          If what exists, the structure does not make sense, you are selecting a row and then ask if something exists. Change the script to:

          DECLARE
          @Count INT

          SELECT @Count = COUNT(*)
          FROM sys.dm_db_index_physical_stats
          (DB_ID(N'Backdrop'), OBJECT_ID(N'DBO.table'), NULL, NULL , 'DETAILED')
          WHERE [avg_fragmentation_in_percent] < 30

          IF @Count > 0

          Never underestimate the power of human stupidity RAH

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups