2

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.
16
  • 3
    In SQL Server (please tag the actual RDBMS - Oracle, MySQL,???) you can issue a SET NOEXEC ON, run it, and catch any error Commented Nov 30, 2015 at 13:39
  • 1
    Thanks for mentioning. It's a Oracle RDBMS. I do not have any connection to the database. Commented Nov 30, 2015 at 13:41
  • 3
    Little Bobby Tables is watching you. As Nick.McDermaid says this looks like an open invitation to SQL injection Commented Nov 30, 2015 at 14:40
  • 1
    @Nick.McDermaid - put that in as an full-sized answer: it is both correct and useful. We'll need an Oracle Dev to swing by and tell us where the equivalent to NOEXEC exists 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. Commented Nov 30, 2015 at 17:50
  • 1
    What are you going to do with the SQL once you've validated it with your external validator if you don't have a DB? Commented Nov 30, 2015 at 22:49

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.