VB6:编写一个分析sqlserver存储过程执行语句"execute procedurename par1,par2,......."语法是否正确的函数

比较笨,花了两天时间。

'函数名: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

原文地址:https://www.cnblogs.com/jinzhenshui/p/1439608.html