我決定使用vba和正則表達式解決此問題。以下是部分代碼:如何解析SQL查詢以獲取涉及該查詢中的表名稱Access
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
將分析表寫入rst
。
它工作得很好,許多類型的查詢,我只對select
查詢感興趣。但我無法弄清楚如何處理子查詢,例如從該查詢,我得到奇怪的支架(
:
select *
from (
select * from t1
union
select * from t2
) t
where 1=1;
那麼這裏是錯的?
PS。我讀到這個共享軟件基於Access VBA的框架(60美元)https://www.arrow-of-time.com/AXF.aspx有一個SQL解析器。我沒有使用它。 – HarveyFrench