Incorrect syntax near the keyword 'CROSS'. [Resolved]

Posted by Vuyiswamb under Sql Server on 5/22/2016 | Points: 10 | Views : 506 | Status : [Member] [MVP] [Administrator] | Replies : 1
Good Day All

i have been using the below declared Cursor from the Stored-procedure for around 3 years now. The last couple of weeks i have been getting the error

Incorrect syntax near the keyword 'CROSS'.

I dont have a Cross Join in this Cursor and this started after Infrastructures upgraded the hardware on the SQL Server , i noticed other settings were lost but was able to recover them.

USE [DBPS]
GO
/****** Object: StoredProcedure [dbo].[DATABASE_MAINTANANCE_PLAN] Script Date: 5/22/2010 12:32:30 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/*THIS SP WILL REBUILT ALL THE INDEXES IN ALL TABLES IN ALL DATABASES
THIS WILL BE CALLED FROM AN SSIS PACKAGE. THIS WILL USED IN CLIENTS TO REBUILT INDEXES AND IT WILL BE
RAN EVERY FRIDAY NIGHT
*/

ALTER PROC [dbo].[DATABASE_MAINTANANCE_PLAN]
AS
DECLARE @DatabaseName SYSNAME = DB_NAME(), @TableName VARCHAR(256)
DECLARE @FILLFACTOR INT = 85
DECLARE @DynamicSQL NVARCHAR(max) =
'DECLARE curAllTablesInDB CURSOR FOR SELECT TABLE_SCHEMA +
''.'' + TABLE_NAME AS TABLENAME
FROM ' + @DatabaseName + '.INFORMATION_SCHEMA.TABLES WHERE
TABLE_TYPE = ''BASE TABLE'''

BEGIN
EXEC sp_executeSQL @DynamicSQL -- create tables cursor
OPEN curAllTablesInDB
FETCH NEXT FROM curAllTablesInDB INTO @TableName
WHILE (@@FETCH_STATUS = 0)
BEGIN
SET @DynamicSQL = 'ALTER INDEX ALL ON ' + @TableName +
' REBUILD WITH (FILLFACTOR = ' + CONVERT(VARCHAR,@FILLFACTOR) + ')'
PRINT @DynamicSQL
-- ALTER INDEX ALL ON Purchasing.ShipMethod REBUILD WITH (FILLFACTOR = 85)
EXEC sp_executeSQL @DynamicSQL
FETCH NEXT FROM curAllTablesInDB INTO @TableName
END -- cursor WHILE
CLOSE curAllTablesInDB
DEALLOCATE curAllTablesInDB
END


Thanks

Thank you for posting at Dotnetfunda
[Administrator]



Responses

Posted by: Vuyiswamb on: 5/22/2016 [Member] [MVP] [Administrator] NotApplicable | Points: 25

Up
0
Down

Resolved
Thanks



I had a table named "CROSS" Cross is a keyword in SQL , that caused a problem for that Dynamic Query. Thanks its resolved now.


Thank you for posting at Dotnetfunda
[Administrator]

Vuyiswamb, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response