解析SELECT查询

时间:2015-09-22 07:15:48

标签: excel vba parsing

我想在VBA的帮助下找出Select查询中涉及的表名。例如,我必须在插槽时间和时间中找出以下查询中涉及的表格。

NSFetchRequest

VBA可以实现吗?

2 个答案:

答案 0 :(得分:0)

希望这会有所帮助:

Dim intFrom As Integer
Dim strSql As String
Dim colTables As New Collection

strSql = "SELECT MAX(theCount) FROM (SELECT FK_Hour, Count(FK_Hour) As theCount FROM (Select FK_Hour From slottime INNER JOIN time ON slottime.FK_Hour = time.Hour WHERE FK_Hour in (SELECT time.Hour FROM time WHERE time.day=0 ) ) As C GROUP By FK_Hour ) AS counts"
intFrom = 1

Do While intFrom > 0
    intFrom = InStr(intFrom, strSql, "FROM", vbTextCompare)
    If InStr(intFrom + 5, strSql, "(", vbTextCompare) = 0 Then 'not the bracket
        Call colTables.Add(Mid(strSql, intFrom + 5, InStr(intFrom + 6, strSql, " ", vbTextCompare) - intFrom - 5))
    End If

    If intFrom = 0 Then Exit Do

    intFrom = intFrom + 1
Loop

你应该为你的" JOIN"。

做同样的事情

最好的问候,Wouter

答案 1 :(得分:0)

Public Sub TableNames()
Dim SQuerry As String ' text to parse
Dim FStart As Long ' place to start (or resume) the analysis of the text being parsed
Dim TName As String ' current possible table name
Dim TAllNames As String ' All table names to parse

SQuerry = "SELECT MAX(theCount) FROM (SELECT FK_Hour, Count(FK_Hour) As theCount FROM (Select FK_Hour From slottime INNER JOIN time ON slottime.FK_Hour = time.Hour WHERE FK_Hour in (SELECT time.Hour FROM time WHERE time.day=0 ) ) As C GROUP By FK_Hour ) AS counts;"
FStart = 1
TAllNames = ","
Do Until found1 = "0" 'found1 + 6 > InStrRev(squerry, " ", -1, 1) Or
    found1 = InStr(FStart, SQuerry, "From", 1)
    If Mid(SQuerry, found1 + 5, 1) = "(" Then
        FStart = found1 + 1
    Else
        TName = Mid(SQuerry, found1 + 5, InStr(found1 + 5, SQuerry, " ", 1) - found1 - 5)
        If InStr(1, TAllNames, "," & TName & ",", 1) = 0 And found1 > 0 Then TAllNames = TAllNames & TName + ","
        FStart = found1 + 1
    End If
Loop

'tallnames is a string of comma separated values starting and ending with a blank value (comma)
Debug.Print TAllNames
MsgBox      TAllNames