EXEC() syntax error using ODBC
- by Mike Trader
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.