Sei sulla pagina 1di 18

HTTP 5105 Database Design and

Development
Bind Variables and Substitution Variables

HTTP 5105 Database Design and Development 1 August 4, 2017


Introduction

Bind variables
Substitution variables

HTTP 5105 Database Design and Development 2 August 4, 2017


Bind Variables

Special type of variable that has several advantages


over a regular variable
SQL statements that use bind variables run more
efficiently then SQL statements that use regular
variables
Use bind variables whenever efficiency is a concern, it
does affect the way a SQL statement is executed
internally

HTTP 5105 Database Design and Development 3 August 4, 2017


Bind Variables

A bind variable can be used in regular SQL statements


that are executed outside a PL/SQL block
A SELECT statement that uses a bind variable named
invoice_id is not coded within a PL/SQL block

HTTP 5105 Database Design and Development 4 August 4, 2017


Bind Variables

The bind variable stays in scope for an entire script


More accurately a bind variable stays in effect for he
entire SQL Developer session
The bind variable can be used across all statements in
a script

HTTP 5105 Database Design and Development 5 August 4, 2017


Bind Variables

To declare a bind variable you code a statement that starts


with the VARIABLE keyword
Similar to declaring a regular PL/SQL variable except the
statement begins with the VARIABLE keyword
This statement is coded outside of a PL/SQL block
This makes sense when you consider that the scope of a
bind variable is larger since it can be used inside the PL/SQL
block or even outside
After you declare a bind variable you can reference it by by
prefacing it with a colon(:)
HTTP 5105 Database Design and Development 6 August 4, 2017
Bind Variables

A bind variable can be used in regular SQL statements


that are executed outside of a PL/SQL block
Follow the steps here to use a bind variable
The bind variable stays in scope for the entire script
and can be used in SQL statements or in PL/SQL
blocks
To code a bind variable, preface the variable name
with a colon (:) as we mentioned previously

HTTP 5105 Database Design and Development 7 August 4, 2017


Declare a Bind Variable

Use the VARIABLE keyword


Give the variable a name
Define the data type for the variable

HTTP 5105 Database Design and Development 8 August 4, 2017


Substitution Variables

If you want to prompt the user to enter a value for a


variable you can code a substitution variable
A substitution variable causes SQL Developer to prompt
the user who runs the script to enter a value that is
substituted for the substitution variable
You code a substitution variable by prefacing the variable
name with the ampersand character (&)
When you run the script SQL Developer will display a dialog
box asking the user to enter a value, in our case it is a value
for the invoice_id variable
The value entered could be assigned to a bind variable

HTTP 5105 Database Design and Development 9 August 4, 2017


Substitution Variables

In some cases, it may seem like a great deal of effort to


add a record to a table or even update a record
This can be very true if you need to modify many different
records
For example, say the CUSTOMERS table now needs to
contain data that identifies the marketing region for each
customer
After the Region column is added to the table using the
ALTER TABLE command, every customer record will need to
be updated with the value for the new column

HTTP 5105 Database Design and Development 10 August 4, 2017


Substitution Variables

The REGION column has been added, it contains NULLs


HTTP 5105 Database Design and Development 11 August 4, 2017
Substitution Variables

UPDATE can be used to set the Region value for each column.
However, this is tedious since the command must be reentered
for each state
HTTP 5105 Database Design and Development 12 August 4, 2017
Substitution Variables

Rather than type the same command again and again to modify
the column with the necessary values, we can use substitution
variables
A substitution variable in a SQL command instructs Oracle to use
a substituted value in place of the variable when command is
executed
To include a substitution variable in a SQL command, use the
ampersand (&) followed by the name to be used for the variable
This value will be substituted with actual data when the command
is executed

HTTP 5105 Database Design and Development 13 August 4, 2017


Substitution Variables

When the UPDATE is


executed, the user is
prompted to provide
values at execution
time.
The / (forward slash)
is used to execute the
command again.

HTTP 5105 Database Design and Development 14 August 4, 2017


Substitution Variables

If you did not want to or have the time to


update all regions at the same time, you could
create a script file of the command as we
discussed earlier
Once created, the script can be loaded and
executed whenever you need it

HTTP 5105 Database Design and Development 15 August 4, 2017


Substitution Variable with Bind
Variable
VARIABLE was already
declared
An anonymous PL/SQL block is
created to prompt user to
enter the invoice_id

HTTP 5105 Database Design and Development 16 August 4, 2017


Substitution Variable with Bind
Variable

The code is run in SQL Developer


The &invoice_id prompts the
user to enter a value for the
invoice id
A value of 1 is entered in the
dialog box
The PL/SQL script runs and the
value of 1 is assigned to the bind
variable via the substitution
HTTP 5105 Database Design and Development 17 August 4, 2017
script
Substitution Variable with Bind
Variable
Here is the complete cycle
SERVEROUTPUT is turned ON
The VARIABLE is declared
The first PL/SL anonymous block assigns
the substitution variable entry to the bind
variable value that was declared
A SELECT statement using the bind
variable value is entered and run with the
output showing
A DBMS_OUTPUT.PUT_LINE line is
entered to show the contents of the bind
variable as well

HTTP 5105 Database Design and Development 18 August 4, 2017

Potrebbero piacerti anche