Any idea how to make this syntax error go away? It’s on the final paranthesis of the whole procedure. I can’t seem to figure out what it is I’m missing. No other errors occur when I try to run it.
CREATE PROCEDURE pRebuildOrReorg
(
@db VARCHAR(50) = 'AdventureWorksLT2012',
@table VARCHAR(50) = 'SalesLT.Customer',
@indexName VARCHAR(50) = 'IX_Customer_EmailAddress'
)
AS
DECLARE @objID INT
DECLARE @dbID SMALLINT
DECLARE @indexID SMALLINT
DECLARE @avgFrag INT
DECLARE @cmd VARCHAR(100)
DECLARE @message VARCHAR(100)
-- 1. find the object id of the desired table
SET @objID = OBJECT_ID(@table)
SET @dbID = DB_ID(@db)
SET @IndexID = (SELECT index_id FROM sys.indexes WHERE name = @indexName)
-- 2. View the statistics for all indexes of the table
SELECT @avgFrag = avg_fragmentation_in_percent
FROM sys.dm_db_index_physical_stats(DB_ID('AdventureWorksLT2012'),
OBJECT_ID('SalesLT.Customer'),
@IndexID, NULL , 'DETAILED')
if @avgFrag >= 30
BEGIN SET @cmd = 'ALTER INDEX ' + @indexName + ' ON ' + @table + ' STATISTICS_NORECOMPUTE = OFF)'
EXEC (@cmd)
SET @message = 'Reorganized Index ' + @indexName
ALTER INDEX PK_StoreContact_CustomerID_ContactID
ON AdventureWorks.Sales.StoreContact
REORGANIZE
END
else if @avgFrag < 30
BEGIN SET @cmd = 'ALTER INDEX ' + @indexName + ' ON ' + @table + ' STATISTICS_NORECOMPUTE = OFF)'
EXEC (@cmd)
SET @message = 'Rebuilt Index ' + @indexName
ALTER INDEX IX_StoreContact_ContactID
ON AdventureWorks.Sales.StoreContact
REBUILD WITH(
fillfactor = 70,
online = ON
)
Welcome to your personal syntax check and debugging service.
Please add an END after that last line, on a new line.
Have you downloaded SQL Server Management Studio 2012 with its improved intellisense and error detection?