Link to home
Start Free TrialLog in
Avatar of Jim Horn
Jim HornFlag for United States of America

asked on

exec @sql thinks @sql is a stored procedure, need it to execute as a query

Hi All

Using the below SQL 2008R2 code and a good #main table where column 'name' = table names, I'm trying to execute SQL statements for each #main.name.  

When I run the below code, it returns the error in the screen shot where it thinks @sql is a stored procedure name, and not a straight query.   What's the fix?

Declare @sql varchar(1000), @name varchar(1000) 

CREATE TABLE #id (id nvarchar(18) COLLATE SQL_Latin1_General_CP1_CI_AS) 

DECLARE cur_del CURSOR FOR 
SELECT name FROM #main

OPEN cur_del
FETCH NEXT FROM cur_del into @name

WHILE @@FETCH_STATUS = 0
   begin
   
   SET @sql = 'DELETE FROM ' + @name + ' WHERE id in (SELECT TOP 2 id FROM ' + @name + ')'
   exec (@sql)
   
   FETCH NEXT FROM cur_del INTO @name      -- Get the next vendor.
   
   end
   
CLOSE cur_del;
DEALLOCATE cur_del;

Open in new window

User generated image
Thanks in advance.
Jim
SOLUTION
Avatar of Phillip Burton
Phillip Burton

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Jim Horn

ASKER

That was up top, didn't copy-paste it into the question.  Added to the top.

Declare @sql varchar(1000), @name varchar(1000)
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>Looks like you can't use a variable or it would expect a stored procedure.
Didn't like that either, as I changed the code to below and it returns an 'Incorrect syntax near 'DELETE FROM ' message.
exec 'DELETE FROM ' + @name + ' WHERE id in (SELECT TOP 2 id FROM ' + @name + ')'

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That's not going to meet my needs.  

I case anyone's wondering, I have an SSIS package with 20+ tables where I'm handling inserts and updates.  This is a test script so that I run the package once, delete 2 rows from each target table, update 3 rows from each target table, run it again, and verify that the next package run had 2 inserts and 3 updates.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial