EXEC() syntax error using ODBC

Posted by Mike Trader on Server Fault See other posts from Server Fault or by Mike Trader
Published on 2011-11-21T17:06:57Z Indexed on 2011/11/21 17:55 UTC
Read the original article Hit count: 223

I have written a little ETL application that I wish to run a few lines of TSQL from. If i enter a simple query like "SELECT * FROM MyTable" everything is fine. All single line commands run as expected.

A multiline query like this is also fine:

DECLARE @TableName NVARCHAR(MAX) 
set @TableName = 'MyTable' 
EXECute ( 'DROP TABLE '+ @TableName )

Howevery when I try and run:

DECLARE @TableName NVARCHAR(MAX)
OPEN Tables
FETCH NEXT FROM Tables INTO @TableName
WHILE @@FETCH_STATUS = 0
BEGIN
EXEC( 'DROP TABLE ' + @TableName   )
 FETCH NEXT FROM Tables INTO @TableName
END

I get a syntax error after TABLE in the EXEC() call. I have spent 6 hours trying to figure this out thinking perhaps I need to escape the single quote or something. I just cannot see the problem.

A set of fresh eyes would be appreciated.

© Server Fault or respective owner

Related posts about windows-server-2008

Related posts about sql-server