I'd like to test whether given SQL statement is syntactically and semantically valid (ie. no syntax errors and no field misspellings).
For most databases Connection.prepareStatement
and PreparedStatement.getMetaData
would do the trick (no exception == good query). Unfortunately Oracle's newest driver only parses like this only SELECT queries, but not other kind of queries. Older drivers don't do even that.
Is there some other facility provided by Oracle for parsing SQL statements?
You can use the Oracle DBMS_SQL package to parse a statement held in a string. For example:
You could wrap that up into a stored function that just returned e.g. 1 if the statement was valid, 0 if invalid, like this:
You could then use it something like this PL/SQL example: