#22003/4/3 9:39:56
原文地址:http://www.zdnet.com.cn/developer/tech/story/0,2000081602,39033283,00.htm
[清单A]
Listing A
Function to convert a simple Oracle DECODE statement to a SQL Server CASE Statement.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function decode(strOraDecode As String) As String
On Error GoTo ErrorHandler
Dim strVBCase As String
Dim strTemp As String
Dim counter As Integer
strVBCase = vbCrLf + " CASE "
counter = InStr(1, strOraDecode, "(", vbTextCompare)
strTemp = Mid(strOraDecode, 1 + counter, _
InStr(counter, strOraDecode, ",", vbTextCompare) _
- (1 + counter))
strVBCase = strVBCase + strTemp + vbCrLf
counter = InStr(counter + 1, strOraDecode, ",", vbTextCompare)
While InStr(counter + 1, strOraDecode, ",", vbTextCompare) > 0 And counter <> 0
strTemp = Mid(strOraDecode, 1 + counter, _
InStr(counter + 1, strOraDecode, ",", vbTextCompare) _
- (1 + counter))
counter = InStr(counter + 1, strOraDecode, ",", vbTextCompare)
strVBCase = strVBCase + " WHEN " + strTemp + " THEN "
If InStr(counter + 1, strOraDecode, ",", vbTextCompare) <> 0 Then
strTemp = Mid(strOraDecode, 1 + counter, _
InStr(counter + 1, strOraDecode, ",", vbTextCompare) _
- (1 + counter))
strVBCase = strVBCase + strTemp
counter = InStr(counter + 1, strOraDecode, ",", vbTextCompare)
Else
strTemp = Mid(strOraDecode, 1 + counter, _
InStr(counter + 1, strOraDecode, ")", vbTextCompare) _
- (1 + counter))
strVBCase = strVBCase + strTemp
counter = InStr(counter + 1, strOraDecode, ",", vbTextCompare)
End If
Wend
If counter <> 0 Then
strTemp = Mid(strOraDecode, 1 + counter, _
InStr(counter + 1, strOraDecode, ")", vbTextCompare) _
- (1 + counter))
strVBCase = strVBCase + vbCrLf + " ELSE " + strTemp + vbCrLf + " END "
Else
strVBCase = strVBCase + vbCrLf + " END "
End If
decode = strVBCase
Exit Function
ErrorHandler:
Debug.Assert True
Debug.Print Err.Description
decode = “”
End Function