6 Bind Variables in PL - SQL - RebellionRider
6 Bind Variables in PL - SQL - RebellionRider
Unlike user variables which can only be declared inside the declaration section of PL/SQL
block you can declare bind variable anywhere in the host environment and that is the
reason why we also refer bind variables as host variable.
Definition
www.rebellionrider.com/bind-variables-in-pl-sql/#.VcSRUCaqqko 1/6
11/1/22, 10:26 PM Bind Variables In PL/SQL | RebellionRider
See how easy it is to declare a bind variable in oracle database! You simply have to write
a command which starts with keyword VARIABLE followed by the name of your bind
variable which is completely user defined along with the data type and data width. That’s
how we declare a bind variable in Oracle database.
Did you notice that I didn’t write any PL/SQL block or section here to declare this bind
variable which is very unlike the user variable.
Let’s see.
VARIABLE;
Execute the above command and that will show you the list of all the bind variables that
you have declared in your session.
Let’s do an example and see the definition of this bind variable v_Bind2.
Variable v_bind2;
Execution of above command will show you the definition of bind variable RebellionRider.
So these are the few uses of Variable command in Oracle database. If you know any
other uses then do tweet and tell me at @RebellionRider.
Restriction:
If you are creating a bind variable of NUMBER datatype then you can
not specify the precision and scale.
www.rebellionrider.com/bind-variables-in-pl-sql/#.VcSRUCaqqko 2/6
11/1/22, 10:26 PM Bind Variables In PL/SQL | RebellionRider
This statement starts with keyword Exec which is the starting 4 alphabets of Keyword
Execute. You can either write whole keyword Execute or just the starting 4 alphabets
“Exec” both will work fine. This is followed by the name of our bind variable which is
v_bind1. After that we have assignment operator followed by the string Rebellion Rider,
as it’s a string thus it’s enclosed in single quotes.
That’s the first way of initializing the bind variable. The second way is:
BEGIN
:v_bind1 := ‘Manish Sharma’;
END;
/
This is a simple execution block where I initialized the bind variable v_bind1 with the
string Manish Sharma.
Unlike user variables which you can access simply by writing their name in your code,
you use colon before the name of bind variable to access them or in other words you can
www.rebellionrider.com/bind-variables-in-pl-sql/#.VcSRUCaqqko 3/6
11/1/22, 10:26 PM Bind Variables In PL/SQL | RebellionRider
reference bind variable in PL/SQL by using a colon (:) followed immediately by the name
of the variable as I did in the previous section.
BEGIN
:v_bind1 := ‘RebellionRider’;
DBMS_OUTPUT.PUT_LINE(:v_bind1);
END;
/
2. To see the output returned from this statement you have set the serveroutput on. You
can do that by simply writing and executing
I highly recommend you to read my PL/SQL Tutorial 1 to understand Blocks & Section in
PL/SQL you can also watch my video tutorial on the same.
Suppose you want to see the current value of bind variable v_bind1 for that simply write
the print command in your SQL*PLUS
Print :v_bind1;
Or
Print v_bind1;
Writing keyword PRINT without any argument will display you the current values of all
the bind variables with their names in the session.
And this command will set AutoPrint parameter on for the session which will
automatically print the values of bind variable.
If you are like me who prefer learning by watching video then you can check out the
video tutorial on this topic right here.
That’s all about Bind Variables. Next in the course you can learn about Conditional
Control statements in PL/SQL here.
Hope this article was helpful. Kindly please share it with your friends and help me reach
out to more people. Thanks guys & have a great day!
Manish Sharma
http://www.YouTube.com/RebellionRider
Manish Sharma, a recipient of the ORACLE ACE Award, is an Oracle database trainer & consultant. He
has an extremely successful YouTube channel named Rebellion Rider. It is one of the top ranking
channels when it comes to Oracle database tutorials with over 10 million views.
www.rebellionrider.com/bind-variables-in-pl-sql/#.VcSRUCaqqko 5/6