August 5, 2010 at 7:12 am
I'm stuck trying to find what is wrong with my code. It works for some databases and blow up for others with the error message
Incorrect syntax near the keyword 'Case'. [SQLSTATE 42000] (Error 156.
Incorrect syntax near the keyword 'with'. If this statement is a common table expression or an xmlnamespaces clause, the previous statement must be terminated with a semicolon. [SQLSTATE 42000]
Here is the code:
DECLARE @Database VARCHAR(255)
DECLARE @Table VARCHAR(255)
DECLARE @cmd NVARCHAR(500)
DECLARE @fillfactor INT
SET @fillfactor = 75
SET @Database = 'MyDBname'
SET @cmd = 'DECLARE TableCursor CURSOR FOR
SELECT table_catalog + ''.'' + table_schema + ''.'' + table_name as tableName
FROM ' + @Database + '.INFORMATION_SCHEMA.TABLES
WHERE table_type = ''BASE TABLE'''
-- create table cursor
EXEC (@cmd)
OPEN TableCursor
FETCH NEXT FROM TableCursor INTO @Table
WHILE @@FETCH_STATUS = 0
BEGIN
-- SQL 2005 command
SET @cmd = 'ALTER INDEX ALL ON ' + @Table + ' REBUILD WITH (FILLFACTOR = ' + CONVERT(VARCHAR(3),@fillfactor) + ')'
EXEC (@cmd)
FETCH NEXT FROM TableCursor INTO @Table
END
CLOSE TableCursor
DEALLOCATE TableCursor
August 5, 2010 at 7:24 am
Try putting [] around the table name, in case there are spaces, prohibited characters or reserved words
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
August 5, 2010 at 8:04 am
I don't see the word CASE in the entire code. Am I missing something here?
Pradeep Adiga
Blog: sqldbadiaries.com
Twitter: @pradeepadiga
August 5, 2010 at 8:11 am
Adiga (8/5/2010)
I don't see the word CASE in the entire code. Am I missing something here?
He's probably got a table called 'Case'
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
August 5, 2010 at 8:25 am
Yep, there is a table with the name 'case'. I'm going with your suggestion to see what happens.
Thanks
August 5, 2010 at 9:32 am
The script failed again with the same error. I'm not sure if I have done all needed to be done though. What I have changed was this line
SET @cmd = 'DECLARE TableCursor CURSOR FOR SELECT table_catalog + ''.'' + table_schema + ''.'' + [table_name] as tableName
Did I missed something else?
Thanks for your help
August 5, 2010 at 10:02 am
Yeah, you missed something. That's not the line throwing the error. This one is:
SET @cmd = 'ALTER INDEX ALL ON ' + @Table + ' REBUILD WITH (FILLFACTOR = ' + CONVERT(VARCHAR(3),@fillfactor) + ')'
You need to change that so that, once the string is built, the table name is wrapped in []
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
August 5, 2010 at 12:56 pm
I'm sorry I don't get it. Could you give some example or point me to where I can find out some more.
Thanks for you help
August 5, 2010 at 1:02 pm
...
SET @cmd = 'DECLARE TableCursor CURSOR FOR
SELECT table_catalog + ''.'' + table_schema + ''.'' + table_name as tableName
FROM [' + @Database + '].INFORMATION_SCHEMA.TABLES
WHERE table_type = ''BASE TABLE'''
...
Scott Pletcher, SQL Server MVP 2008-2010
August 5, 2010 at 1:14 pm
The database name was not the issue becasue there aren't any spaces in the name and the name itself isn't a sql reserved keyword. We have some table in the database that are the issue I belive like it is been pointed out earlier in the thread.
Thanks for your attempt to help.
August 5, 2010 at 1:19 pm
SET @cmd = 'DECLARE TableCursor CURSOR FOR
SELECT ''['' + table_catalog + ''].['' + table_schema + ''].['' + table_name + '']'' as tableName
FROM [' + @Database + '].INFORMATION_SCHEMA.TABLES
WHERE table_type = ''BASE TABLE'''
Scott Pletcher, SQL Server MVP 2008-2010
August 5, 2010 at 2:10 pm
Thanks Scott, that was the fix and appreciate your help.
August 7, 2010 at 4:57 pm
When creating dynamic SQL, it's a good idea to use the Quotename() function to appropriately quote table names, field names, et al. Quotename will automatically handle escaping the quote characters if they are contained in the string, which helps guard against SQL injection.
Drew
J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
Viewing 13 posts - 1 through 12 (of 12 total)
You must be logged in to reply to this topic. Login to reply