Is there any way to verify that an SQL statement will run, or is at least syntacticly correct, for alters, updates, deletes, and inserts. I am looking for something similar to ‘desc’ for selects. It seems like this would be a pretty common thing and I really don’t want to try to re-implement the MySQL parser. If anyone could point me in the right direction on this one I would appreciate it.
Use EXPLAIN in front of your query.
It is used to find out how the execution plan for that query is.
What indexes it will use etc.
But it will also need to parse the query in full, so you will get your syntactical test as a bonus.
Plus you can learn a lot about how to optimize queries.
Thanks for the suggestion but ‘desc’ is an alias for ‘EXPLAIN’. It only works for selects. I need something that works for all SQL statements. Any other ideas?
The other two suggestions I can give you is:
Execute the query with one table name purposely wrong.
If the syntax is valid then you will only get error 1146: Table xyz does not exist.
I sometimes do this when I want to make a rough validity check of a sql statement when I’m about to post it as a solution for somebody in this forum.
If you are using InnoDB tables then you can execute the query within a transaction which you end by performing a rollback.