It took two days.
'Function name: parsesrc
'Function: analyze the SQL Server Stored Procedure execution statement "procedurename par1, par2,..." Whether the syntax is correct.
'Input parameter: SRC as string, for example, "proceduretest' string 'half-bean', 343, '2017-2-5 '"
'Return value: String Array. parsesrc (0) = "Name of the stored procedure", parsesrc (1) = Number of valid parameters (if it is-1, it indicates that an invalid parameter exists), parsesrc (> 1) = input parameter of the corresponding position
'Call: 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 (SRC, vbtab, CHR (32), vbcrlf, CHR (32 )))
The array element of 'index = 1' stores the stored procedure name.
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
'Stored procedure parameters
Parmpos = 1
While Len (SRC)> 0
Commapos = 0
'The number of parameters increases from Index = 1, that is, the index of the first parameter is 2.
Parmpos = parmpos + 1
Redim preserve mynode (parmpos)
If left (SRC, 1) <> "'" Then
Iscomma = true' is ","
Else
Iscomma = false' starts "'"
End if
If iscomma then
'Take the location of the First ','
Commapos = instr (1, SRC ,",")
'No "," is a single parameter
If commapos = 0 then
If instr (1, SRC, "'") = 0 then
Strleft = SRC
Src = ""
Else
Goto exitit
End if
'The first parameter is ',' omitting the first parameter
Elseif commapos = 1 and Len (SRC)> 1 then
Strleft = ""
Src = right (SRC, Len (SRC)-1)
'The last one is ',' omitting the final Parameter
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
'Initialization login' is not finished
Isend = false
'Initialize the location of the first''
Singquotepos = 1
Singquotecount = 1
'If the contestant list has only one ''syntax error
If Len (SRC) = 1 then
Goto exitit
Else
'From the second position, keep searching for the next''
Nextsingquotepos = 1
While not isend
'Find the next '''
Nextsingquotepos = instr (nextsingquotepos + 1, SRC ,"'")
'If the next one is found, the number of "'" is greater than 1. Continue processing.
If singquotecount> 0 then
Singquotepos = nextsingquotepos
Singquotecount = singquotecount + 1
'The key point is to determine whether the entries are correctly written.
'If an even number of ''has been found, the following character must be", "or" several spaces, or "'" or "several spaces"
If singquotecount mod 2 = 0 then
Strleft = left (SRC, singquotepos)
Strright = right (SRC, Len (SRC)-singquotepos)
'No more characters
If trim (strright) = "" then
Isend = true
Src = ""
'Next is ","
Elseif left (TRIM (strright), 1) = "," then
Isend = true
Strright = trim (strright)
Src = right (strright, Len (strright)-1)
'Followed by the character "'"
Elseif left (strright, 1) = "'" Then
Isend = false
Else
Isend = true
Goto exitit
End if
'If you have found an odd number of '', continue the loop.
Else
Isend = false
End if
'Processing ended
Else
'If you have found an even number of''
If singquotecount mod 2 = 0 then
Strleft = left (SRC, singquotepos)
Strright = trim (right (SRC, Len (SRC)-singquotepos ))
'If there is no data next, it is the final parameter.
If strright = "" then
Isend = true
Src = ""
'Next is the separator ","
Elseif left (strright, 1) = "," then
Isend = true
Src = right (strright, Len (strright)-1)
'The parameter is invalid.
Else
Goto exitit
End if
The parameter is incorrect.
Else
Goto exitit
End if
End if
Wend
End if
End if
Mynode (parmpos) = strleft
Wend
The array element of 'index = 1' stores the number of parameters.
Mynode (1) = parmpos-1
Parsesrc = mynode
Exit Function
Exitit:
Mynode (parmpos) = "parameter error"
Mynode (1) =-1
Parsesrc = mynode
End Function