0

I am using T-SQL to fetch some table names which satisfy a certain criteria - put the name of those tables in a temporary table - @MyTempTable and plan to delete all rows from all tables which have an entry in @MyTempTable.

I notice that my @MyTempTable is populated correctly with the names of the relevant tables that have to be deleted, but when I try to execute the DELETE command on each entry in @MyTempTable, it throws a syntax error saying "Must declare a table variable "@localVar". I guess this is because it is not letting me use the value of each row as the name of the table to delete entries from. Can you suggest how I can fix this?

/* Define temporary table */
DECLARE @MyTmpTable TABLE ( my_row varchar(20));

/* local variable holds the name of the table that has to be deleted based on the query below */
DECLARE @localVar varchar(20);

/* Declare curser */
DECLARE c5 CURSOR FOR (

    /* SELECT TOP 10 vc_merchant_name FROM dbo.merchant_versioned */
    select 
        t.name
    from 
        sys.tables t
    where 
        exists (
            select null from sys.columns c 
            where c.object_id = t.object_id and c.name = 'i_version_id')
);
OPEN c5;

FETCH NEXT FROM c5 INTO @localVar;
WHILE @@FETCH_STATUS = 0
BEGIN
    /*************************************************/
    DELETE FROM @localVar; /* <<<--- THIS DOESN"T WORK
    /*************************************************/
    FETCH NEXT FROM c5 INTO @localVar;
END

CLOSE c5;
DEALLOCATE c5;
marc_s
  • 675,133
  • 158
  • 1,253
  • 1,388
Darth.Vader
  • 3,249
  • 5
  • 38
  • 66
  • That just isn't going to work. You could build the SQL and use sp_execute: http://technet.microsoft.com/en-us/library/ff848746.aspx – Rob Nov 19 '13 at 23:56
  • @Mate: It says the same thing: "Must declare a table variable "@localVar". – Darth.Vader Nov 19 '13 at 23:59
  • @Rob: Can you please elaborate a little more - sorry for the ignorance - I just started with SQL Server. – Darth.Vader Nov 20 '13 at 00:00
  • @Matt: It just says - Incorrect syntax near 'SomeTable' – Darth.Vader Nov 20 '13 at 00:06
  • I think it would be wonderful if that worked! – sam yi Nov 20 '13 at 00:10
  • i agree with rob ... you will need to build the sql command string dynamically and execute it via sp_execute ... but then you will get foreign key constraint violations because you can't delete the records table by table in "random" order without violating constraints ... or don't you use them in your database design? – PrfctByDsgn Nov 20 '13 at 07:01

1 Answers1

2

You are getting that error because you are literally trying to execute

DELETE FROM @localVar

It's not just plugging in the value of that parameter like you want it to. You'll need to use dynamic SQL to accomplish this, and then as Rob said, use SP_EXECUTE.

DECLARE @localVar varchar(20);
DECLARE @qString varchar(max)
....
BEGIN
   set qString = 'DELETE FROM ' + @localVar
   SP_EXECUTE(qString)
END
...
Andrew
  • 7,336
  • 2
  • 22
  • 41
  • Thanks, Andrew. I think I am *almost* there. The only problem that I am having now (and hopefully the last one) - is that my Visual Studio does not recognize `sp_execute`. When I type `sp_` and wait for the IDE to auto complete, I don't see sp_execute there in the list. Any clue? – Darth.Vader Nov 20 '13 at 01:43
  • Are you on a version prior to 2008 R2? If so, try `SP_EXECUTESQL qString`. – Andrew Nov 20 '13 at 01:57
  • Actually that's not the problem, I realized that when I just type: sp_exec(SELECT 1) - without the quotes, it is happy, but as soon as I replace the parameters with @qString, it has this problem. Let me know if you have some clue. BTW - minor bug in your code: you might want to replace 'qString' with '@qString' in the SET statement. – Darth.Vader Nov 20 '13 at 02:09
  • 1
    Sorry, mixing up my syntax. Use EXECUTE(@qString). [SQL Fiddle](http://sqlfiddle.com/#!3/f6605/29) – Andrew Nov 20 '13 at 02:38