I am not a regex expert but i really am not sure whether
you should do it using regex.
What you are talking about a lexical parser that understands a particular language, in your case a SQL code.
I would recommend going for something like
antlr that will logically parse your code and let you do complex compiler like activities.
Of course, this is just my opinion and based on your requirements, it may or may not be applicable.