0

I need just use var as some value in Oracle SQL, something like this:

    date = '21-Sep-10'

    SELECT * 
    FROM (
          SELECT * 
          FROM  table2
          WHERE some_date = date 
         ) table1
    WHERE table1.due_date = date;

How I can do this properly ?

Thanks !

1

1 Answer 1

1

It depends on tool you use. In SQL*Plus, you'd precede it with &

SELECT * FROM table WHERE id = &tmp;

In some other tools (SQL Developer, TOAD), that would be :

SELECT * FROM table WHERE id = :tmp;

If you're using the same variable more than once in the same query, precede it with two ampersands:

SQL> set linesize 200
SQL> set ver off
SQL> select *
  2  from (select *
  3        from emp
  4        where deptno = &&deptno
  5       )
  6  where deptno = &&deptno;

     EMPNO ENAME      JOB              MGR HIREDATE        SAL       COMM     DEPTNO
---------- ---------- --------- ---------- -------- ---------- ---------- ----------
      7782 CLARK      MANAGER         7839 09.06.81       2450                    10
      7839 KING       PRESIDENT            17.11.81       5000                    10
      7934 MILLER     CLERK           7782 23.01.82       1300                    10

SQL>

However, if you run the same query once again, you won't be prompted for the variable (as it is "remembered"):

SQL> /

     EMPNO ENAME      JOB              MGR HIREDATE        SAL       COMM     DEPTNO
---------- ---------- --------- ---------- -------- ---------- ---------- ----------
      7782 CLARK      MANAGER         7839 09.06.81       2450                    10
      7839 KING       PRESIDENT            17.11.81       5000                    10
      7934 MILLER     CLERK           7782 23.01.82       1300                    10

SQL>

You'll need to undefine it first:

SQL> undefine deptno
SQL> /
Enter value for deptno: 20

     EMPNO ENAME      JOB              MGR HIREDATE        SAL       COMM     DEPTNO
---------- ---------- --------- ---------- -------- ---------- ---------- ----------
      7369 SMITH      CLERK           7902 17.12.80        800                    20
      7566 JONES      MANAGER         7839 02.04.81       2975                    20
      7788 SCOTT      ANALYST         7566 09.12.82       3000                    20
      7876 ADAMS      CLERK           7788 12.01.83       1100                    20
      7902 FORD       ANALYST         7566 03.12.81       3000                    20

SQL>
Sign up to request clarification or add additional context in comments.

3 Comments

,thanks for your answer. Your approach related to promt condition . I have modified the example, please have a look. I need use the var more that one time in one select statement.
SQL Developer supports both substitutions (&var) and binds (:var)
You're welcome, ppostnov. I've added some more examples; have a look, please. | Yes, @thatjeff, I know, thank you for correcting me.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.