Skip to Main Content

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, lalain.

Asked: July 26, 2001 - 10:27 am UTC

Last updated: August 28, 2013 - 7:03 pm UTC

Version: 8.0

Viewed 10K+ times! This question is

You Asked

HI sir

please tell me about

Q. What is basic difference between bind &host variables?

sir please sent me a complete answer of this question with its examples because i am so confussed to differ them.
but examples should be useable in our environment because yours previous procedures examples could not be executed in our environment and displayed some errors those still not removed by any possible actions.

THANKS

WAITING FOR YOUR POSITIVE REPLY IN MY INBOX.


and Tom said...

A bind variable is an INPUT into the query you provide.

A host variable is where you put your data.

In the query:

select * from emp where ename = :x


:X is the bind variable. In a pro*c program I might have some code like:

exec sql begin declare section;
VARCHAR x[20];
VARCHAR eno[20];
exec sql end declare section;

strcpy( x.arr, "KING" );
x.len = strlen(x.arr);

exec sql select empno into :eno from emp where ename = :x;


Here, :eno and :x are bind variables in the query. x and eno are the C variables that are the HOST variables.


In PLSQL, the following:

declare
x varchar2(20);
eno number;
begin
x := 'KING';

select empno into eno from emp where ename = x;
end;
/

eno and x are the bind variables AND the host variables (the distinction blurs in plsql -- its very close to sql)




Rating

  (11 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Very useful

Girish Hulikanitmath, August 04, 2001 - 4:25 am UTC

This is very good solution, many people have the confusion
abt the difference between those two.


follow up

A reader, August 09, 2001 - 9:26 pm UTC

Tom

In your pl/sql answer you mean eno is the host variable and x is the bind variable.

why do you say that the "the distinction blurs
in plsql -- its very close to sql"

what do you mean by saying "its very close to sql"

specify more precisely

A reader, August 09, 2001 - 9:28 pm UTC

Can you more preceisely state what is bind and what is host variable between x and eno , and why

is host var which is not also a bind var possible within a sql?

Mikito Harakiri, September 20, 2001 - 9:40 pm UTC

Therefore, a variable inside a sql statement is always called "bind variable". A variable outside of embedded sql is a "host variable". In case if they are literally the same then we call them both "host and bind", right? It looks like the syntax confuses symantics.

How to use "memset" function in Pro*C to assign '\0' for VARCHAR data type?

Tony, August 06, 2003 - 2:54 am UTC

I use VARCHAR data type extensively in Pro*C for bulk fetch and update. After each bulk fetch (ex: 100 rows in each fetch), I use a loop to add '\0' character for all VARCHAR data type columns for each row as below:

VARCHAR ename[30][100];
VARCHAR occpn[20][100];
.........
EXEC SQL DECLARE c1 CURSOR FOR SELECT ENAME, OCCUPATION FROM EMP;
....
EXEC SQL FETCH c1 INTO :ename, :occpn;
no_of_recs = sqlca.sqlerrd[2];
....
for (i = 0; i < no_of_recs ; i++)
{
ename[i].arr[ename[i].len] = '\0';
occpn[i].arr[occpn[i].len] = '\0';
}

As billons of rows are processed in this way, I want to minimize time taken for assigning '\0' character for each row. I try to use "memset" function for the above purpose to avoid the loop, but it doesn't work. Can you please let me know how to use "memset" function in this scenario?. Please let me know if there is any alternative.


Tom Kyte
August 06, 2003 - 8:04 am UTC

memset works on contigous memory. the zeros you want to add are randomly accessed.

One might question why you do this? If you want a null terminated string, why not use an array of char?


here is what I would have done:

void process( void )
{
typedef char enameType[30];
exec sql type enameType is string(30);
enameType ename[100];

char job[100][20];
exec sql var job is string(20);

int i;

exec sql whenever sqlerror do sqlerror_hard();

exec sql declare c cursor
for
select ename, job
from emp;

exec sql open c;
for( ; sqlca.sqlcode == 0; )
{
exec sql fetch c into :ename, :job;
for( i = 0; i < sqlca.sqlerrd[2]; i++ )
printf( "'%s', '%s'\n", ename[i], job[i] );
}
exec sql close c;
exec sql commit;
}

That shows two different ways.


typedef char enameType[30];
exec sql type enameType is string(30);
enameType ename[100];

is nice if you have lots of homogenous sized variables as it associates the datatype with a string.


char job[100][20];
exec sql var job is string(20);

is nice if you don't. if most variables are "their own size".

The output of that is:


[tkyte@tkyte-pc-isdn t]$ ./t

Connected to ORACLE as user: /

'SMITH', 'CLERK'
'ALLEN', 'SALESMAN'
'WARD', 'SALESMAN'
'JONES', 'MANAGER'
'MARTIN', 'SALESMAN'
'BLAKE', 'MANAGER'
'CLARK', 'MANAGER'
'SCOTT', 'ANALYST'
'KING', 'PRESIDENT'
'TURNER', 'SALESMAN'
'ADAMS', 'CLERK'
'JAMES', 'CLERK'
'FORD', 'ANALYST'
'MILLER', 'CLERK'



Why and When to use VARCHAR data type in Pro*C?

Tony, August 06, 2003 - 8:31 am UTC

Thanks a lot for your excellent quick answer.

VARCHAR data type is extensively used in all our existing pro*c applications that do bulk fetch and bulk update. I'm in the process of tuning them.
1) Will there be any problem if I change all VARCHAR data type to char data type as given in your example?.
2)Will there be any definite performance gain if I use char data type instead VARCHAR?
3) If char data type is sufficient, Why and when do we use VARCHAR data type?


Tom Kyte
August 06, 2003 - 8:59 am UTC

1) from the pro*c side? no.

from your code side? perhaps -- it'll change your code and if you made assumptions that "this is varchar" in your code and don't catch that, you'll have a bug of course.

2) you'd have to benchmark. I've never benchmarked it.

3) you tell me, you did it :)

Bulk bind using c structure in pro*C

Tony, August 12, 2003 - 7:07 am UTC

1.Is it possible to bulk bind (fetch & update) using structure? if yes, Could you provide an example please?

2. Where to specify PREFETCH=100 during compilation of pro*c programs? While calling proc or gcc or make?

Tom Kyte
August 12, 2003 - 8:55 am UTC

Structure for bulk update doesn't work

Tony, August 12, 2003 - 9:58 am UTC

Thanx a lot Tom,
Bulk fetch into structure works, but bulk update using structure doesn't work. Any clue please?


Tom Kyte
August 12, 2003 - 10:23 am UTC

my car won't start this morning. any clues?

but yes, in general -- the semantics of an UPDATE are not conducive to an array of structs.

A struct of arrays, yes.

Ok

Ram, December 25, 2003 - 12:21 am UTC

Dear sir,
I work under SQL*PLUS in the windows environment.When I issue a command like
sql> host cls
It never clears the screen but it clears the screen in
MS-DOS prompt.How to correct this?
Thanks in advance.
Bye!

Tom Kyte
December 25, 2003 - 10:10 am UTC

use the command line version?

the GUI version is not running "in dos", it is running in the window. the "host cls" command is just firing up a dos window, clearing the screen and exiting.

I never liked that gui version, i always just use the command line version when forced to use windows.



How to do this for integer?

Tony, January 21, 2004 - 9:06 am UTC

Hi,
To avoid assigning null terminator for char variables, we can use the following way.

char job[100][20];
exec sql var job is string(20);

How to do like this for integer variables?

Tom Kyte
January 21, 2004 - 9:36 am UTC

huh? int's are not "null terminated"?

if you fetch a number into a string -- that is just like you used to_char() -- meaning that you do it the same way you did it for "job" -- no different.

if you fetch into a string, you use the same technique.

if you fetch into int's -- you don't null terminate them.

A reader, August 26, 2013 - 10:18 am UTC

can you clearly explain it in pl/sql...
what do you mean by saying 'distinction blurs'..
Tom Kyte
August 28, 2013 - 7:03 pm UTC

the distinction between sql and plsql blurs - melds together - becomes hard to see - since plsql and sql are tied so tightly together.

in the example above -


declare
    x   varchar2(20);
    eno number;
begin
   x := 'KING';

   select empno into eno from emp where ename = x;
end;
/



x and eno are host variables - but we don't really call them that by convention when speaking about plsql. they are just plsql variables. They are bound into the sql statement transparently, they are fetched into transparently - you don't demark them with a ":" or anything. the line between sql and plsql is "blurry" since they are very tightly coupled.

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library