Benson, William (GE Global Research, consultant)
Benson at ge.com
Tue Nov 20 12:07:32 CST 2012
First, I am doing this through Excel VBA, using ADO and working with an Oracle provider. I am having heartburn getting some data pushed from Excel to Oracle. Apparently the command "SET DEFINE OFF" works only in SQL DEVELOPER and SQL PLUS, it is not some SQL one can simply throw at the Oracle provider via SQL. So in order to inject characters like
g_Conn.Execute "SET DEFINE OFF INSERT INTO SID_USERS (USER_SSO) VALUES ('Sanford & Sons');", True
'runtime error... ORA-00922 missing or invalid option
So I wrote a routine to escape characters like ampersand for any text which I plan to insert into a field using ADO and SQL. May not be robust, but it does turned Sanford & Sons into
'Sanford ' || '&' || 'Sons'
Now, that works in SQL PLUS but it will not work when trying ADO's Execute - I am still getting an "invalid character" error message.
INSERT INTO SID_USERS (
USER_SSO,
CONAME)
VALUES ( 'x',
'Sanford ' || '&' || 'Sons');
Function OracleText(str) As String
Const BADCHARS = "&"
Dim strWorkingText As String
Dim strBAD As String
Dim strFinal As String
Dim bMadesubstitution As Boolean
strWorkingText = str
strWorkingText = Replace$(strWorkingText, "'", "''")
Dim i As Long
Do Until strWorkingText = ""
For i = 1 To Len(strWorkingText)
bMadesubstitution = False
If InStr(BADCHARS, Mid(strWorkingText, i, 1)) > 0 Then
strBAD = Mid(strWorkingText, i, 1)
strFinal = strFinal & Mid(strWorkingText, 1, i - 1) & "'|| '" & strBAD & "' || '"
strWorkingText = Mid(strWorkingText, i + 1)
bMadesubstitution = True
Exit For
End If
Next
If Not bMadesubstitution Then
strFinal = strFinal & strWorkingText
strWorkingText = ""
End If
Loop
OracleText = strFinal
End Function