As part of a hubby project I am working on, I need to be able to take SQL strings supplied by users, and then run it.

But before running it, i will like to do some manipulations to the SQL. For example, I may want to have a copy of the SQL that does a COUNT query version of the original query...I might want to insert LIMIT clauses if not provided, or I might want to manipulate the columns specified, rename or even insert columns.

Now the question is: I am not sure how to go able this and what sort of tools to even look for. I think in essence I want to be able to manipulate the SQL not as a string but as a data structure I can manipulate.

Using regex and string manipulation does not sound like the right tool for the job. What will you say is the right way to go about this?


1 Answer 1


You are looking for an sql parser. A parser breaks the code into its parts. From there, you can manipulate the sql.

Note: I don't know your use case, but in most cases, it is a bad idea to take sql directly from the user. The risk of getting your database corrupted is essential.

  • $\begingroup$ Yeah sounds like an sql parser will do the job. KNow any you could recommend? $\endgroup$
    – dade
    Apr 28, 2023 at 7:15
  • $\begingroup$ I never used a parser for SQL and I don't know which programming language you are working in. So I do not have any recommendation. $\endgroup$
    – Broele
    Apr 28, 2023 at 8:16

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.