Home > Software engineering >  Creating table with defined variable
Creating table with defined variable

Time:09-23

I work in SQL Developer by Oracle. I have a simple query to creating new table based on condition where. I want to generate new table with defined variable like below. My code isn't working. How can I define variable and put its directly to query without any entry box?

DEFINE STARTDATE DATE:="TO_DATE('2021-06-01','YYYY-MM-DD')"

CREATE TABLE RESULTS AS
SELECT
*
FROM TABLE_1
WHERE DATA=ADD_MONTHS(:STARTDATE,2);

CodePudding user response:

That's not how define variables works. It is just a text replacement, not a pl/sql variable. try this:

DEFINE STARTDATE  =TO_DATE('2021-06-01','YYYY-MM-DD')

CodePudding user response:

DEFINE creates a substitution variable.

:STARTDATE uses a bind variable, it is not a substitution variable.

&startdate would be a substution variable.

You want something like:

DEFINE STARTDATE = "DATE '2021-06-01'"

CREATE TABLE RESULTS AS
SELECT *
FROM   TABLE_1
WHERE  DATA=ADD_MONTHS(&STARTDATE,2);

CodePudding user response:

In SQL*Plus, it is a substitution variable you'd use (BTW, define syntax you used is wrong).

For example:

SQL> select * From emp order by hiredate;

     EMPNO ENAME      JOB              MGR HIREDATE        SAL       COMM     DEPTNO
---------- ---------- --------- ---------- -------- ---------- ---------- ----------
      7369 SMITH      CLERK           7902 17.12.80        800                    20
      7499 ALLEN      SALESMAN        7698 20.02.81       1600        300         30
      7521 WARD       SALESMAN        7698 22.02.81       1250        500         30
      7566 JONES      MANAGER         7839 02.04.81       2975                    20
      7698 BLAKE      MANAGER         7839 01.05.81       2850                    30
      7782 CLARK      MANAGER         7839 09.06.81       2450                    10
      7844 TURNER     SALESMAN        7698 08.09.81       1500          0         30
      7654 MARTIN     SALESMAN        7698 28.09.81       1250       1400         30
      7839 KING       PRESIDENT            17.11.81       5000                    10
      7900 JAMES      CLERK           7698 03.12.81        950                    30
      7902 FORD       ANALYST         7566 03.12.81       3000                    20
      7934 MILLER     CLERK           7782 23.01.82       1300                    10
      7788 SCOTT      ANALYST         7566 09.12.82       3000                    20
      7876 ADAMS      CLERK           7788 12.01.83       1100                    20

14 rows selected.

Create a table out of it:

SQL> DEFINE STARTDATE = "1980-12-01"
SQL> create table a1 as select * from emp where trunc(hiredate, 'mm') =
  2                     trunc(add_months(to_date('&startdate', 'yyyy-mm-dd'), 2));
old   2:                    trunc(add_months(to_date('&startdate', 'yyyy-mm-dd'), 2))
new   2:                    trunc(add_months(to_date('1980-12-01', 'yyyy-mm-dd'), 2))

Table created.

SQL> select * from a1;

     EMPNO ENAME      JOB              MGR HIREDATE        SAL       COMM     DEPTNO
---------- ---------- --------- ---------- -------- ---------- ---------- ----------
      7499 ALLEN      SALESMAN        7698 20.02.81       1600        300         30
      7521 WARD       SALESMAN        7698 22.02.81       1250        500         30

SQL>
  • Related