问题
I'm trying to execute an ad hoc query with a parameter (bind variable) using Visual Studio 2010 (.NET 4.0) and ODP 4.112.3.0, Oracle11g. It's operating in a 32 bit application pool under ASP.NET. I have the following code:
Public Shared Function LookupUpc(ByVal upc As Long) As DataTable
Dim upcLookup As New DataTable
Dim sql As String = "OPEN :outCursor FOR SELECT * FROM IMPROD.UPC_ID_XREF " + _
"INNER JOIN IMPROD.UPC_ID_DESCRIPTIONS ON IMPROD.UPC_ID_XREF.UPC_ID = IMPROD.UPC_ID_DESCRIPTIONS.UPC_ID " + _
"INNER JOIN IMPROD.UPC_ID_ATTRIBUTES ON IMPROD.UPC_ID_XREF.UPC_ID = IMPROD.UPC_ID_ATTRIBUTES.UPC_ID " + _
"WHERE IMPROD.UPC_ID_XREF.UPC = :inUPC"
Using dbConn As New OracleConnection(DataAccess.ConnectionString)
dbConn.Open()
Using dbCommand As New OracleCommand(sql, dbConn)
dbCommand.CommandType = CommandType.StoredProcedure
dbCommand.BindByName = True
dbCommand.Parameters.Add("inUPC", OracleDbType.Int64).Value = upc
dbCommand.Parameters.Add("outCursor", OracleDbType.RefCursor, DBNull.Value, ParameterDirection.Output)
Using dbAdapter As New OracleDataAdapter(dbCommand)
dbAdapter.Fill(upcLookup)
End Using
dbCommand.DisposeOfAllParameters()
End Using
End Using
Return upcLookup
End Function
When I execute the code, dbAdapter.Fill(upcLookup) throws an exception about a missing right parenthesis.
The SQL executes fine in Toad.
The outCursor references were added as an attempt to resolve the issue. The issue occurs whether or not a cursor is used to retrieve the results.
I modified the SQL and tried to use Oracle's syntax per a KB note regarding issues with binding and ODP and ANSI syntax joins:
SELECT * FROM IMPROD.UPC_ID_XREF, IMPROD.UPC_ID_DESCRIPTIONS,
IMPROD.UPC_ID_ATTRIBUTES WHERE IMPROD.UPC_ID_XREF.UPC = :inUPC
AND IMPROD.UPC_ID_XREF.UPC_ID = IMPROD.UPC_ID_DESCRIPTIONS.UPC_ID
AND IMPROD.UPC_ID_XREF.UPC_ID = IMPROD.UPC_ID_ATTRIBUTES.UPC_ID
This resulted in the same error (along with a cascade of other errors, including ">" being found where not expected, an extra semi colon being found, etc. The parenthesis error was the first listed.)
I stripped the SQL down to:
SELECT * FROM IMPROD.UPC_ID_XREF WHERE IMPROD.UPC_ID_XREF.UPC = :inUPC
but the missing parenthesis error continued. So I went even further and used this SQL:
SELECT IMPROD.UPC_ID_XREF.UPC FROM IMPROD.UPC_ID_XREF;
This removes the multi select (*) from the statement, the variable, and the JOINs. Yet it still threw an error:
ORA-06550: line 1, column 68:
PLS-00103: Encountered the symbol ">" when expecting one of the following:
. ( * @ % & = - + < / > at in is mod remainder not rem
<an exponent (**)> <> or != or ~= >= <= <> and or like like2
like4 likec as between || multiset member submultiset
The symbol "( was inserted before ">" to continue.
ORA-06550: line 1, column 90:
PLS-00103: Encountered the symbol ";" when expecting one of the following:
. ( * % & = - + < / > at in is mod remainder not rem
<an exponent (**)> <> or != or ~= >= <= <> and or like like2
like4 likec as between || multiset memb
Which is... a big huh?
I tried to use a constant in place of :inUPC and this did not help, so I think my binding is okay.
Speaking of binding, if I turn off BindByName I receive the error ORA-01008: not all variables bound. I know this contradicts my statement above, but there was originally only one variable to be bound.
I've Google'd and read through the huge morass of posts on SO about the same error but nothing seems to fit the bill.
I can wrap SQL Server around my finger, but I am a novice in the Oracle arena. Please help me Obi-Wan Kenobi, you're my only hope. slaps on Princess Leia buns and outfit; you'll have to imagine Peter Griffin as Leia to get the full effect
I know this will end up being some incredibly simple fix. Thank you for your time and help.
回答1:
This is pretty late coming, but it bit me.
You have explicitly stated that the SQL command is a stored procedure, when it should be text. Change:
dbCommand.CommandType = CommandType.StoredProcedure
to:
dbCommand.CommandType = CommandType.Text
I suspect it will work like magic then. It did for me, at any rate.
回答2:
I resolved the problem by using a procedure rather than attempting to execute the query ad hoc. (So the problem was not really solved, but worked around.) I had been attempting to avoid that situation as the application had no other database dependencies associated with it.
来源:https://stackoverflow.com/questions/24747684/execution-of-oracle-ad-hoc-query-with-bind-parameters-erroring-out-ora-00907-m