EXECUTE IMMEDIATE throwing ORA-00933 error
670700Nov 16 2008 — edited Nov 16 2008Hi,
I am trying to build a PL/SQL code that implements the EXECUTE IMMEDIATE statement within a cursor since EXECUTE IMMEDIATE doesn't support multi-record queries. I want to recursively place a single record into a variable by using the INTO clause USING a defined value. But my code keeps throwing an error that the SQL command is not properly terminate at line 15; which is the line of the EXECUTE IMMEDIATE statement.
I have tried outputting the SQL statement to the console to examine it in effort of finding the problem but the output appears to be correct. I have also performed other variations of what I am trying to accomplish but don't to be getting anywhere. Could someone possibly assist to identify what I'm doing wrong?
DECLARE
tbl VARCHAR2(50);
col VARCHAR2(50);
sqlcmd VARCHAR(300);
field_value VARCHAR2(20) := 'MEXICO';
match_count NUMBER;
CURSOR c1 IS SELECT TABLE_NAME, COLUMN_NAME FROM USER_TAB_COLS;
BEGIN
OPEN c1;
LOOP
FETCH c1 INTO tbl, col; -- fetches 2 columns into variables
EXIT WHEN c1%NOTFOUND;
sqlcmd := 'SELECT COUNT(*) FROM '||tbl||' WHERE '||col||' = '':col''';
EXECUTE IMMEDIATE sqlcmd INTO match_count USING field_value;
DBMS_OUTPUT.PUT_LINE(sqlcmd);
END LOOP;
CLOSE c1;
END;
/
I am more proficient with SQL than PL/SQL but am trying to better understand the semantics of the language.
Thanks for any assistance.