5

With respect to PLSQL, what is the difference between a 'Host Variable' and a 'Bind Variable'?

This link from askTom says that "the distinction blurs in plsql -- its very close to sql". Then what is that tiny, 'blurry' difference?

Statement 1:

SELECT 1 FROM dual WHERE dummy = :B1;

Statement 2:

SELECT 1 FROM dual WHERE dummy = v_var;

In these two statements, Statement 1 represents a bind variable and Statement 2 represents a Host variable, am I correct?

1
  • Bind/Host Variable applies only for embedded SQL/PLSQL... Bind is actually sending some value from your interface(Java or Pro*C) to Oracle.. And Host is the variable define in your interface and get its value from Oracle... So your statement goes absurd. Any static SQL in the PL/SQL blocks using variables itself, would be implicitly used in the way similar to bind variables.. (Actually the parsed SQL/Plan..) .. hence, With PL/SQL(Executed in Oracle directly and not via JDBC or any OCI based) there's NO bind variables literally.. Unless you have a dynamic SQL. Oct 8, 2014 at 15:53

3 Answers 3

4

Consider this snippet of C#:

int    v_empno = 7369;
string v_ename;

OracleCommand cmd = con.CreateCommand();
cmd.Parameters.Add("paramEmpno", OracleDbType.Decimal, v_empno, ParameterDirection.Input);
cmd.CommandText = "select e.ename from scott.emp e where e.empno = :1";
v_ename = cmd.ExecuteScalar().ToString();

v_empno and v_ename are host variables. Here you explicitly create your bind variable for use as :1 in your the statement.

Consider this snippet of PL/SQL:

declare
   v_empno  number := 7369;
   v_ename  varchar2(10);
begin
   select e.ename
     into v_ename
     from scott.emp e
    where e.empno = v_empno;
   dbms_output.put_line(v_ename);
end;
/

Again the declared variables v_empno and v_ename can be considered host variables, but when they are used in static SQL within the PL/SQL code, they are automatically turned into bind variables by the PL/SQL compiler/engine - you do not have to manually create your bind variable like in the C# example. If you examine the SQL that is actually executed by this piece of PL/SQL, it will look something like this:

   select e.ename
     from scott.emp e
    where e.empno = :B1

That is the PL/SQL compiler that automatically has created :B1 bind variable for your v_empno PL/SQL variable. And that is what Tom Kyte means that you cannot really make a proper distinction between host variable and bind variable in PL/SQL. When you write PL/SQL the variables are host variables when used in PL/SQL code and at the same time they are bind variables when used in the embedded SQL code. You do not need to make a distinction in PL/SQL, the compiler takes care of it for you.

3

As per documentation

Host variables are the key to communication between your host program and Oracle. Typically, a precompiler program inputs data from a host variable to Oracle, and Oracle outputs data to a host variable in the program. Oracle stores input data in database columns, and stores output data in program host variables.

A host variable can be any arbitrary C expression that resolves to a scalar type. But, a host variable must also be an lvalue. Host arrays of most host variables are also supported.

Bind variables are not simple text values. Their values are sent to the database, and the database can also set their values. PL/SQL itself takes care of most of the issues to do with bind variables. Every reference to a PL/SQL variable is in fact a bind variable.

For example:

int     empno; 
char    ename[10]; 
float   sal; 
... 
EXEC SQL SELECT ename, sal INTO :ename, :sal FROM emp 
     WHERE empno = :empno; 

empno, ename, sal are all Host variables
:empno, :ename, :sal are all bind variables

You can see a similar question here

2
  • 1
    This documentation refers to Pro*C/C++ Precompiler. I don't think that the term "HOST VARIABLE" is applicable to PL/SQL at all.
    – Rusty
    Oct 8, 2014 at 14:25
  • Thanks a lot for your answer, but , as @Rusty pointed out, it refers to Pro*C/C++ Precompiler and not PLSQL
    – A Nice Guy
    Oct 9, 2014 at 6:37
2

Bind/Host Variable applies only for embedded SQL/PLSQL...

Bind is actually sending some value from your interface(Java or Pro*C) to Oracle..

Host is the variable defined in your interface and get its value from Oracle...

So your statement goes absurd.

Any static SQL in the PL/SQL blocks using variables itself, would be implicitly used in the way similar to bind variables..

--It means next execution of this SQL could re-use the plan and cursor would be shared.. in case of Adaptive cursor sharing..

.. hence, With PL/SQL(Executed in Oracle directly and not via JDBC or any OCI based) there's NO bind variables literally.. Unless you have a dynamic SQL.

Example :

 EXECUTE IMMEDIATE 'SELECT NAME FROM EMP WHERE ID = : ID' USING V_MYID

Here :ID is Bind Variable ---- (To Accept Value to the Query from the caller)
and V_MYID is Host Variable.....(Defined in the caller/host itself)

The concept is similar to other Embedded SQL principles..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.