0

I am trying to use loop variable as a string and use it directly with EXECUTE IMMEDIATE in PL/SQL loop. Here is my code:

BEGIN
    FOR i IN (
        SELECT table_name FROM user_tables WHERE LOWER(table_name) LIKE 'tblequityreturnstest_%'
    )
    LOOP
        vqs := 'DROP TABLE'||i||''
        EXECUTE IMMEDIATE vqs;
    END LOOP;
END;

But I am getting an error :

Error report -
ORA-06550: line 4, column 1:
PLS-00103: Encountered the symbol "EXECUTE" when expecting one of the following:

   * & = - + ; < / > at in is mod remainder not rem
   <an exponent (**)> <> or != or ~= >= <= <> and or like like2
   like4 likec between || member submultiset
06550. 00000 -  "line %s, column %s:\n%s"
*Cause:    Usually a PL/SQL compilation error.
*Action:
1
  • Is the problem really about "output" specifically? Also you should not include the tags in the question title. Commented Jun 2, 2017 at 11:32

1 Answer 1

3

Your syntax is wrong, try

DECALRE    
   vqs VARCHAR2(100);
BEGIN
   FOR i IN (SELECT table_name FROM user_tables WHERE LOWER(table_name) LIKE 'tblequityreturnstest_%') LOOP
      vqs := 'DROP TABLE '||i.table_name;
      EXECUTE IMMEDIATE vqs;
   END LOOP;
END;

or even simpler

BEGIN
   FOR i IN (SELECT table_name FROM user_tables WHERE LOWER(table_name) LIKE 'tblequityreturnstest_%') LOOP
      EXECUTE IMMEDIATE 'DROP TABLE '||i.table_name;
   END LOOP;
END;
Sign up to request clarification or add additional context in comments.

4 Comments

Yes, I anticipated this. I assumed your code just as a snippet.
DECLARE vqs varchar(1000) := ''; BEGIN FOR i IN (SELECT d.table_name FROM user_tables d WHERE (LOWER(d.table_name) LIKE 'tblequityreturnstest_%')) LOOP vqs := 'SET TIMING ON; SELECT a.Ticker, FLAnnReturn(ReturnData(a.EquityReturn, '||'Daily'||')) AS AnnReturn FROM '||i.table_name||' a GROUP BY a.Ticker ORDER BY 1; SET TIMING OFF;'; EXECUTE IMMEDIATE vqs; END LOOP; END; /
For something like this it can be simpler to construct the required DDL in the cursor, or else add a dummy column to the cursor and populate that procedurally in the loop, rather than declaring a separate variable. btw you don't need to assign null to a new string variable, as it's already null by default.
@MuditSharma if you have additional code please add it to the question or start a new one. Just noticed the SET TIMING IN in your dynamic SQL string, which is a SQL*Plus command. SQL*Plus is a separate command-line tool and PL/SQL can't host out to it, or anything else.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.