我想在ASP中創建一個日誌函數,並且要做到這一點,我需要能夠解析UPDATE sql語句。 如果我有一個SQL語句是這樣的:解碼/解析UPDATE sql語句
UPDATE mytable SET aaa='test',bbb=123 WHERE id=508
我想作爲一個輸出是陣列說字段和值這樣的內容:
FieldArray = aaa,bbb
ValueArray = test,123
我怎麼能在ASP做這做也許直接在SQL?
我想在ASP中創建一個日誌函數,並且要做到這一點,我需要能夠解析UPDATE sql語句。 如果我有一個SQL語句是這樣的:解碼/解析UPDATE sql語句
UPDATE mytable SET aaa='test',bbb=123 WHERE id=508
我想作爲一個輸出是陣列說字段和值這樣的內容:
FieldArray = aaa,bbb
ValueArray = test,123
我怎麼能在ASP做這做也許直接在SQL?
忘記使用SQL。它的作用是使用,訪問和修改數據庫服務器上的數據。
在VB.NET中,您可以使用Left
,Mid
,Right
和InStr
函數來解析出您的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
如果數據庫是MySQL的,你有SUPER權限,而且你事先知道將要更新的表,你可以定義一個MySQL觸發器,應該能夠識別更改的項目,並在另一個表記錄他們
你有如何使用這個觸發器的例子SQL我張貼的例子嗎? –
不幸的不是。我會寫一個觸發器之前更新讀取表結構,並用它來查詢字段,以檢查哪一個(S)將被改變... – perissf
哇,謝謝。我會馬上嘗試:) –
工作真的很好:)再次感謝 –