3

I'm a newbie in PL/SQL and I'm struggling with following issue. I'm looking for an answer for 4 hours and it's still not working...

I've got above 300 records in a table with SQL statements and my goal is to write a procedure that is able to loop over them under some conditions. I'd like also to check which queries passed and which ones failed.

So it goes like this (list of steps/pseudocode):

  • procedure or function that takes a condition as a parameter
  • iterates over records that match a condition
  • executes SQL statements which fit match
  • gives an information of success or failure of execution

I was trying to do something like this (below) but I think it's not correct - because I'm not looping over records.

DECLARE
   sql_stmt    VARCHAR2(3000);

BEGIN
   sql_stmt := 'SELECT testcase_sql
                FROM data_audit_testcase_v2
                WHERE testcase_desc LIKE ''Test 6 - EM%''';
   dbms_output.put_line('Sth: ' || sql_stmt);
   EXECUTE IMMEDIATE sql_stmt ;
END;

I'm thinking about adjusting this code to my issue:

FOR q IN (SELECT sql_text FROM query_table)
LOOP
  EXECUTE IMMEDIATE 'SELECT COUNT(*) FROM (' || q.sql_text || ')'
     INTO some_local_variable;
  <<do something with the local variable>>
END LOOP;

source here

What do you guys think?

Thank you for your help in advance,

Arthur

2 Answers 2

1

You have to do it like this:

DECLARE
   sql_stmt    VARCHAR2(3000);
    cur SYS_REFCURSOR;
    some_local_variable data_audit_testcase_v2.testcase_sql%TYPE;
BEGIN
   sql_stmt := 'SELECT testcase_sql
                FROM data_audit_testcase_v2
                WHERE testcase_desc LIKE :val';
   DBMS_OUTPUT.PUT_LINE('Sth: ' || sql_stmt);
    OPEN cur FOR sql_stmt USING 'Test 6 - EM%';
    LOOP
        FETCH cur INTO some_local_variable;     
        EXIT WHEN cur%NOTFOUND;
        <<do something with the local variable>>
    END LOOP;
    CLOSE cur;
END;
5
  • Thank you @Wernfried for your contribution. But I'm still not able to check if the query was executed. I tried to add sth like DMBS_OUTPUT.PUT_LINE(cur); to see what's in a current step of the loop, but it doesn't compile without warnings and doesn't work for me. Can I ask you for help?
    – Artekem
    Commented Feb 26, 2015 at 8:42
  • Make DMBS_OUTPUT.PUT_LINE(some_local_variable); Commented Feb 26, 2015 at 8:49
  • in line FETCH cur INTO some_local_varbiable; I'm getting an error: Inconsistent datatypes: expected %s got %s (it's ORA-06512). I'm trying to figure out what went wrong.
    – Artekem
    Commented Feb 26, 2015 at 9:16
  • It seems PUT_LINE can't print neither some_local_varbiale nor cur
    – Artekem
    Commented Feb 26, 2015 at 9:51
  • I've found the solution - I added second parameter to the SELECT, so PUT_LINE didn't know how to display it :).
    – Artekem
    Commented Feb 26, 2015 at 12:10
0

I've found the solution - I added second parameter to the SELECT, so PUT_LINE didn't know how to display it :).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.