比較笨,花了兩天時間。
'函數名:parsesrc
'功能:分析sqlserver預存程序執行語句"procedurename par1,par2,......."文法是否正確的函數
'入參:src as string,如"proceduretest '字串''後半部分',343,'2005-2-5'"
'傳回值:字串數組.parsesrc(0)="預存程序的名稱",parsesrc(1)=有效參數的個數(如果為-1,表示有非法參數),parsesrc(>1)=對應的位置的入參
'調用:dim result() as string
' result=parsesrc("procedurename par1,par2,.......")
'
Function parsesrc(src As String) As String()
Dim mynode() As String
Dim strleft, strright As String
Dim iscomma, isend As Boolean
Dim parmpos, commapos, spacepos, singquotepos, singquotecount, nextsingquotepos As Integer
src = Trim(Replace(Replace(src, vbTab, Chr(32)), vbCrLf, Chr(32)))
'index=1的數組元素儲存的是預存程序名
spacepos = InStr(1, src, Chr(32))
If spacepos > 0 Then
strleft = Left(src, spacepos - 1)
src = Trim(Right(src, Len(src) - spacepos))
ReDim mynode(2)
mynode(0) = strleft
Else
Exit Function
End If
'預存程序的參數
parmpos = 1
While Len(src) > 0
commapos = 0
'參數個數從第index=1開始遞增,即第一個參數的index=2
parmpos = parmpos + 1
ReDim Preserve mynode(parmpos)
If Left(src, 1) <> "'" Then
iscomma = True '是","開頭
Else
iscomma = False '是"'"開頭
End If
If iscomma Then
'取第一個“,”的位置
commapos = InStr(1, src, ",")
'沒有“,”是單個參數
If commapos = 0 Then
If InStr(1, src, "'") = 0 Then
strleft = src
src = ""
Else
GoTo exitit
End If
'第一個是“,”省略第一個參數
ElseIf commapos = 1 And Len(src) > 1 Then
strleft = ""
src = Right(src, Len(src) - 1)
'最後一個是“,”省略最後的參數
ElseIf commapos = Len(src) And Len(src) > 1 Then
strleft = Left(src, commapos - 1)
src = ""
If InStr(1, strleft, "'") > 0 Then
GoTo exitit
End If
Else
strleft = Left(src, commapos - 1)
src = Right(src, Len(src) - commapos)
If InStr(1, strleft, "'") > 0 Then
GoTo exitit
End If
End If
Else
'初始化“'”沒有結束
isend = False
'初始化第一個“'”的位置
singquotepos = 1
singquotecount = 1
'如果參賽列表僅有一個“'”語法錯誤
If Len(src) = 1 Then
GoTo exitit
Else
'從第二個位置開始,不斷尋找下一個“'”
nextsingquotepos = 1
While Not isend
'尋找下一個“'”
nextsingquotepos = InStr(nextsingquotepos + 1, src, "'")
'如果找到下一個了,找到的“'”數量+1,繼續處理
If singquotecount > 0 Then
singquotepos = nextsingquotepos
singquotecount = singquotecount + 1
'非常關鍵的地方,判斷參賽的寫法是否正確
'如果已經找到了偶數個“'”,接下來的字元必須是“,”或“ 若干空格,或“'”或“若干空格”
If singquotecount Mod 2 = 0 Then
strleft = Left(src, singquotepos)
strright = Right(src, Len(src) - singquotepos)
'接下來沒有字元了
If Trim(strright) = "" Then
isend = True
src = ""
'接下來是“,”
ElseIf Left(Trim(strright), 1) = "," Then
isend = True
strright = Trim(strright)
src = Right(strright, Len(strright) - 1)
'緊接著的字元是“'”
ElseIf Left(strright, 1) = "'" Then
isend = False
Else
isend = True
GoTo exitit
End If
'如果已經找到了奇數個“'”,繼續迴圈
Else
isend = False
End If
'處理結束
Else
'如果已經找到了偶數個“'”
If singquotecount Mod 2 = 0 Then
strleft = Left(src, singquotepos)
strright = Trim(Right(src, Len(src) - singquotepos))
'如果接下來沒有任何資料,即是最後的參數
If strright = "" Then
isend = True
src = ""
'接下來的是分割符號“,”
ElseIf Left(strright, 1) = "," Then
isend = True
src = Right(strright, Len(strright) - 1)
'參數非法
Else
GoTo exitit
End If
'奇數個的“'”,參數錯誤
Else
GoTo exitit
End If
End If
Wend
End If
End If
mynode(parmpos) = strleft
Wend
'index=1的數組元素儲存的是參數的個數
mynode(1) = parmpos - 1
parsesrc = mynode
Exit Function
exitit:
mynode(parmpos) = "參數錯誤"
mynode(1) = -1
parsesrc = mynode
End Function