解码/解析UPDATE sql语句

时间:2011-08-08 14:27:36

标签: asp.net mysql sql

我想在ASP中创建一个日志函数,为此我需要能够解析UPDATE sql语句。 如果我有这样的SQL语句:

 UPDATE mytable SET aaa='test',bbb=123 WHERE id=508

我希望得到的输出是数组,说明字段和值如下:

FieldArray = aaa,bbb
ValueArray = test,123

我如何在ASP中或直接在SQL中执行此操作?

2 个答案:

答案 0 :(得分:0)

如果db是mysql并且您具有SUPER权限,并且事先知道将要更新的表,则可以定义一个能够识别已更改项并将其记录在另一个表中的mysql TRIGGER

答案 1 :(得分:0)

忘记使用SQL。它的作用是在数据库服务器上使用,访问和修改数据。

在VB.NET中,您可以使用LeftMidRightInStr函数来解析SQL字符串。

' Using your string : UPDATE mytable SET aaa='test',bbb=123 WHERE id=508
strWithoutFirstPart = Mid(strSQL, InStr(strSQL, "SET")) ' Output : SET aaa='test',bbb=123 WHERE id=508
strWithoutLastPart = Left(strWithoutFirstPart, InStr(strWithoutFirstPart, "WHERE")-1) ' OutPut : SET aaa='test',bbb=123

This has more information on the functions

以下是我为您的问题制作的程序:

'Global variables
Dim parameters, values
parameters = Array()
values = Array()

' Procedure for parsing an Update SQL Statement to get parameters and values.
Sub parseSQLUpdate(ByVal strSQL)
    ' Variables
    Dim strParsed, intEqualPos, intCommaPos, intLastPos, regExStrCommaTest

    ' Clearing the global variables.
    ReDim parameters(0), values(0)

    ' Parsing the string for the right section.
    strParsed = Trim(Mid(strSQL, InStr(UCase(strSQL), "SET") + 4))
    strParsed = Trim(Left(strParsed, InStr(UCase(strParsed), "WHERE") - 2))

    ' Getting positions.
    intEqualPos = InStr(strParsed, "=")
    intCommaPos = InStr(strParsed, ",")
    intLastPos = 0

    ' Preparing the Reg. Ex. variable
    Set regExStrCommaTest = new RegExp
    regExStrCommaTest.Pattern = "('[^']*'$)|(^[^']+$)"

    ' Looping for every parameter/value.
    Do While (intEqualPos > 0)
        ' Check if first loop.
        If (intLastPos = 0) Then
            parameters(UBound(parameters)) = Trim(Left(strParsed, intEqualPos - 1))

            ' Check if at last parameter/value.
            If (intCommaPos > 0) Then
                ' Check for commas in text.
                Do While (regExStrCommaTest.Test(Trim(Mid(strParsed, intEqualPos + 1, intCommaPos - intEqualPos - 1))) = False)
                    ' Go to next comma.
                    intCommaPos = InStr(intCommaPos + 1, strParsed, ",")

                    ' Already at end, so take remaining characters.
                    If (intCommaPos <= 0) Then
                        values(UBound(values)) = Trim(Mid(strParsed, intEqualPos + 1))
                        Exit Sub
                    End If
                Loop

                values(UBound(values)) = Trim(Mid(strParsed, intEqualPos + 1, intCommaPos - intEqualPos - 1))
            Else
                values(UBound(values)) = Trim(Mid(strParsed, intEqualPos + 1))
            End If
        Else
            ' Change arrays' size.
            ReDim Preserve parameters(UBound(parameters) + 1), values(UBound(values) + 1)

            parameters(UBound(parameters)) = Trim(Mid(strParsed, intLastPos + 1, intEqualPos - intLastPos - 1))

            ' Check if at last parameter/value.
            If (intCommaPos > 0) Then
                ' Check for commas in text.
                Do While (regExStrCommaTest.Test(Trim(Mid(strParsed, intEqualPos + 1, intCommaPos - intEqualPos - 1))) = False)
                    ' Go to next comma.
                    intCommaPos = InStr(intCommaPos + 1, strParsed, ",")

                    ' Already at end, so take remaining characters.
                    If (intCommaPos <= 0) Then
                        values(UBound(values)) = Trim(Mid(strParsed, intEqualPos + 1))
                        Exit Sub
                    End If
                Loop

                values(UBound(values)) = Trim(Mid(strParsed, intEqualPos + 1, intCommaPos - intEqualPos - 1))
            Else
                values(UBound(values)) = Trim(Mid(strParsed, intEqualPos + 1))
            End If
        End If

        ' Remembering last position
        intLastPos = intCommaPos

        ' Ending Loop if last position is 0
        If (intLastPos = 0) Then
            Exit Sub
        End If

        ' Getting new positions.
        intEqualPos = InStr(intLastPos, strParsed, "=")
        intCommaPos = InStr(intEqualPos, strParsed, ",")
    Loop
End Sub