I'm studying the differences between bind variables(:) and ampersand operator(& / substitution variables). I realized that "double ampersand is stored variable values and reused these values where it is the same session.".
According to this fact, if I use the double ampersand, then I can avoid hard parsing or not?
Title asks for difference between &
and &&
.
Both are used with substitution variables (as you already know). Here's a simple and rather dummy example, but will illustrate the difference.
First option: only one &
used with the same substitution variable name; you're required to enter the same value twice (or, as many times as needed):
SQL> set ver off
SQL>
SQL> select e.ename, e.job, e.sal
2 from emp e join dept d on e.deptno = d.deptno
3 where e.deptno = &par_deptno
4 and d.deptno = &par_deptno;
Enter value for par_deptno: 10 --> this is from line #3
Enter value for par_deptno: 10 --> this is from line #4
ENAME JOB SAL
---------- --------- ----------
CLARK MANAGER 2450
KING PRESIDENT 5000
MILLER CLERK 1300
Re-run the same query - you'll again be prompted:
SQL> /
Enter value for par_deptno: 30
Enter value for par_deptno: 30
ENAME JOB SAL
---------- --------- ----------
ALLEN SALESMAN 1600
WARD SALESMAN 1250
MARTIN SALESMAN 1250
BLAKE MANAGER 2850
TURNER SALESMAN 1500
JAMES CLERK 950
6 rows selected.
SQL>
Second option: &&
, which results in only one prompt to enter the value:
SQL> select e.ename, e.job, e.sal
2 from emp e join dept d on e.deptno = d.deptno
3 where e.deptno = &&par_deptno
4 and d.deptno = &&par_deptno;
Enter value for par_deptno: 10
ENAME JOB SAL
---------- --------- ----------
CLARK MANAGER 2450
KING PRESIDENT 5000
MILLER CLERK 1300
What happens if I re-run it? The same (old!) value will be used, without any prompt:
SQL> /
ENAME JOB SAL
---------- --------- ----------
CLARK MANAGER 2450
KING PRESIDENT 5000
MILLER CLERK 1300
SQL>
If you want to use another substitution variable value, undefine it and then run the query:
SQL> undefine par_deptno
SQL> /
Enter value for par_deptno: 30
ENAME JOB SAL
---------- --------- ----------
ALLEN SALESMAN 1600
WARD SALESMAN 1250
MARTIN SALESMAN 1250
BLAKE MANAGER 2850
TURNER SALESMAN 1500
JAMES CLERK 950
6 rows selected.
SQL>
As of question you posted in the body (about hard parsing): substitution variables, as far as I can tell, won't help in that. Bind variables would.
Have a look at what BluShadow wrote on OTN Forums (PL/SQL 101 : Substitution vs. Bind Variables):
So, in summary, Substitution variables are variables that the user interface detects and prompts for text to substitute into the code before submitting it to the database, and Bind variables are placeholders in queries that allow SQL queries to be soft parsed rather than hard parsed when the query is re-used, help prevent SQL injection, and allow for the values to be supplied easily and seamlessly within the code issuing it.
Also, note that substitution variables can't be used everywhere; it depends on a tool you use. For example, SQL*Plus works with them, TOAD doesn't recognize them, etc.