I have decided to solve this problem by using vba and regular expression. Here is part of the code:
Dim strPattern As String: strPattern = "(FROM|JOIN|from|From|Join|join)\s+([^ ,]+)(?:\s*,\s*([^ ,]+))*\s*"
Dim strReplace As String: strReplace = ""
Dim regEx As New RegExp
Dim strInput As String
Set MyMatches = regEx.Execute(Form)
If MyMatches.Count <> 0 Then
With MyMatches
For myMatchCt = 0 To MyMatches.Count - 1
If Left(MyMatches.Item(myMatchCt), 6) <> "FROM (" And Left(MyMatches.Item(myMatchCt), 6) <> "JOIN (" Then
str = MyMatches.Item(myMatchCt)
lenght = Len(str)
format_data = Right(str, lenght - 4)
pos = InStr(format_data, ")") + InStr(format_data, "(select")
If pos = 0 Then
rst.AddNew
rst!block_id = rs("block_id")
rst!trans_table = format_data
rst.Update
End If
End If
Next
End With
End if
Parsed tables are written to rst
.
It works almost fine with many types of query, I interested only in select
queries. However I can't figure out how to handle subqueries, for example from this query I get odd bracket (
:
select *
from (
select * from t1
union
select * from t2
) t
where 1=1;
So what is wrong here?