Monday, 4 March 2013

Using The && Substitution Variable

Use the double-ampersand (&&) if you want to reuse the variable value without prompting the user each time.
SQL> select empno,ename,job,&&column_name from emp order by &column_name;
Enter value for column_name: sal
old   1: select empno,ename,job,&&column_name from emp order by &column_name
new   1: select empno,ename,job,sal from emp order by sal
    EMPNO ENAME      JOB             SAL
--------- ---------- --------- ---------
     7369 SMITH      CLERK           800
     7900 JAMES      CLERK           950
     7876 ADAMS      CLERK          1100
     7521 WARD       SALESMAN       1250
     7654 MARTIN     SALESMAN       1250
     7934 MILLER     CLERK          1300
     7844 TURNER     SALESMAN       1500
     7499 ALLEN      SALESMAN       1600
     7782 CLARK      MANAGER        2450
     7698 BLAKE      MANAGER        2850
     7566 JONES      MANAGER        2975
     7788 SCOTT      ANALYST        3000
     7902 FORD       ANALYST        3000
     7839 KING       PRESIDENT      5000
14 rows selected.


SQL> select empno,ename,job,&&column_name from emp order by &column_name; [IT DOES NOT PROMPT YOU FOR VALUE]
old   1: select empno,ename,job,&&column_name from emp order by &column_name
new   1: select empno,ename,job,sal from emp order by sal
    EMPNO ENAME      JOB             SAL
--------- ---------- --------- ---------
     7369 SMITH      CLERK           800
     7900 JAMES      CLERK           950
     7876 ADAMS      CLERK          1100
     7521 WARD       SALESMAN       1250
     7654 MARTIN     SALESMAN       1250
     7934 MILLER     CLERK          1300
     7844 TURNER     SALESMAN       1500
     7499 ALLEN      SALESMAN       1600
     7782 CLARK      MANAGER        2450
     7698 BLAKE      MANAGER        2850
     7566 JONES      MANAGER        2975
     7788 SCOTT      ANALYST        3000
     7902 FORD       ANALYST        3000
     7839 KING       PRESIDENT      5000
14 rows selected.

No comments:

Post a Comment