Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle "ORA-01008: not all variables bound" Error w/ Parameters

This is the first time I've dealt with Oracle, and I'm having a hard time understanding why I'm receiving this error.

I'm using Oracle's ODT.NET w/ C# with the following code in a query's where clause:

WHERE table.Variable1 = :VarA   AND (:VarB IS NULL OR table.Variable2 LIKE '%' || :VarB || '%')   AND (:VarC IS NULL OR table.Variable3 LIKE :VarC || '%') 

and I'm adding the parameter values like so:

cmd.Parameters.Add("VarA", "24"); cmd.Parameters.Add("VarB", "test"); cmd.Parameters.Add("VarC", "1234"); 

When I run this query, the server returns:

ORA-01008: not all variables bound  

If I comment out either of the 'AND (....' lines, the query completes successfully.

Why would the query run through alright if I'm only querying with two parameters, but not with three? The error I'm receiving doesn't even make sense

like image 992
John Avatar asked Sep 14 '09 14:09

John


People also ask

What does Ora-01008 Not all variables bound mean?

ORA-01008 not all variables bound. Cause: A SQL statement containing substitution variables was executed without all variables bound. All substitution variables must have a substituted value before the SQL statement is executed. Action: In OCI, use an OBIND or OBINDN call to substitute the required values.

What is bind variable in Oracle with examples?

You reference bind variables in PL/SQL by typing a colon (:) followed immediately by the name of the variable. For example :ret_val := 1; To change this bind variable in SQL*Plus, you must enter a PL/SQL block. For example. SQL> begin 2 :ret_val:=4; 3 end; 4 / PL/SQL procedure successfully completed.

Can we use bind variables in Oracle stored procedure?

REFCURSOR bind variables can also be used to reference PL/SQL cursor variables in stored procedures. This allows you to store SELECT statements in the database and reference them from SQL*Plus. A REFCURSOR bind variable can also be returned from a stored function.


2 Answers

The ODP.Net provider from oracle uses bind by position as default. To change the behavior to bind by name. Set property BindByName to true. Than you can dismiss the double definition of parameters.

using(OracleCommand cmd = con.CreateCommand()) {     ...     cmd.BindByName = true;     ... } 
like image 95
Christian13467 Avatar answered Oct 09 '22 22:10

Christian13467


It seems daft, but I think when you use the same bind variable twice you have to set it twice:

cmd.Parameters.Add("VarA", "24"); cmd.Parameters.Add("VarB", "test"); cmd.Parameters.Add("VarB", "test"); cmd.Parameters.Add("VarC", "1234"); cmd.Parameters.Add("VarC", "1234"); 

Certainly that's true with Native Dynamic SQL in PL/SQL:

SQL> begin   2     execute immediate 'select * from emp where ename=:name and ename=:name'   3     using 'KING';   4  end;   5  / begin * ERROR at line 1: ORA-01008: not all variables bound   SQL> begin   2     execute immediate 'select * from emp where ename=:name and ename=:name'    3     using 'KING', 'KING';   4  end;   5  /  PL/SQL procedure successfully completed. 
like image 30
Tony Andrews Avatar answered Oct 09 '22 22:10

Tony Andrews