简体   繁体   中英

validating a plsql block

I am preparing a screen in which user will input an anonymous block (declare ... begin ... end) of plsql, and i will save it's block into a file an run it when it is necessary. But I want to validate the user's input against my database whether it has syntax errors or not before execution. when i googled it i found that I can use antlr , but i could not found any working sample. can anyone show me a sample, I am open for other solutions within the java and plsql context.

You can parse the plsql command with DBMS_SQL:

SQL> CREATE OR REPLACE PROCEDURE parse(p_command VARCHAR2) AUTHID CURRENT_USER IS
  2     l_cursor INTEGER;
  3  BEGIN
  4     l_cursor := dbms_sql.open_cursor;
  5     dbms_sql.parse(l_cursor, p_command, dbms_sql.native);
  6     dbms_sql.close_cursor(l_cursor);
  7  EXCEPTION
  8     WHEN OTHERS THEN
  9        dbms_sql.close_cursor(l_cursor);
 10        RAISE;
 11  END;
 12  /

Procedure created

SQL> exec parse ('BEGIN NULL;END;');

PL/SQL procedure successfully completed

SQL> exec parse ('BEGIN incorrect_statement;END;');

begin parse ('BEGIN incorrect_statement;END;'); end;

ERROR at line 1:
ORA-06550: line 1, column 7:
PLS-00201: identifier 'INCORRECT_STATEMENT' must be declared

Be careful with what you parse though: DDL will be executed on parse (!).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM