Is it possible to validate a SQL code on syntax correctness with VBA?
In my scenario, the user is entering a SQL Statement into a text box of a user form. I would first like to validate if the SQL statement is correct syntax wise, and second check if it's a SELECT statement.
What the validation shall check
- Are the SQL keywords (which can be used in a SELECT statement) spelled correct
- Order of SQL keywords
- Is the statement a SELECT statement
Summarizing the comments
- The RDBMS is Oracle
- The Excel sheet does not have a connection to the Oracle database.
- The Excel sheet will be uploaded to a system, then the SQL codes will be executed on the database.
- I am aware of the possibility of SQL injection.
- The provided SQL statement shall be checked on syntax correctness. Column names shall not be checked, as database structure is not known.
SET NOEXEC ON, run it, and catch any errorNOEXECexists in PL-SQL - and they are notorious for only answering questions if you provide exactly the right wording, which kinda defeats the point here - but the essential answer is that you make the RDBMS parse SQL, rather than trying to do it yourself: and that, in turn, might introduce an inexperienced coder to a very important principle in programming: NEVER, EVER, WRITE A PARSER TO DO A JOB THAT AN EXISTING PARSER CAN DO.