0

I'm in the middle of data migration, converting Sybase query in oracle 11g I've stuck on this thing from past 2 days

@Declare @myDate Datetime
Select @myDate = workingDate from MyTable

Then there are few sql statement in which the varaible myDate is getting used in where clause

For Ex

Select * form table1 
join table1 on table1.id = table.id
join table1 on table1.id =  mytable.id
where mytable.workingDate = my_date

// so at last what I want is to declare one variable which will gets its value from a select clause and use it in further sql statements in a complex query

I want to achieve the same in Oracle 11g without any added complexity

I'm new to oracle, your help is required.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
Shelly
  • 183
  • 1
  • 1
  • 11
  • Maybe similar to this: https://stackoverflow.com/a/40360471/230471 – William Robertson Jun 20 '20 at 08:59
  • How will the results be used? For a data migration, can you write the whole thing in PL/SQL, without relying on SQL\*Plus features? – William Robertson Jun 20 '20 at 09:32
  • @WilliamRobertson I'm in the hunt of simple conversion of declare of Sybase in oracle, I want use my_date in multiple where clauses further – Shelly Jun 20 '20 at 09:34
  • How will the results be used though? Are you just going to run a SQL\*Plus script and have it print out some results? Or since you mentioned migration, will there be `insert into x select from y` type of logic? If so you could do it all in PL/SQL and not need any `variable` or `print` etc. – William Robertson Jun 20 '20 at 09:38
  • We don't have enough context information do you want only to convert SQL scripts with SQL and anonymous PL/SQL **OR** do you want to convert Transact-SQL to PL/SQL procedures, functions and packages ? – pifor Jun 20 '20 at 09:38
  • ok I will make it simple, (Declare myDate Datetime Select myDate = workingDate from MyTable) --> Sybase thing in oracle I just want to use myDate in where clause further in sql statement, which I cannot expose over here – Shelly Jun 20 '20 at 09:39
  • Yes, something like that. The available data types are shown [here](https://docs.oracle.com/en/database/oracle/oracle-database/19/sqlrf/Data-Types.html). – William Robertson Jun 20 '20 at 09:44

2 Answers2

0

Are you using SQLPlus: I don't think you can declare a SQLPlus TIMESTAMP variable.

Try instead a VARCHAR2 variable and convert to TIMESTAMP:

SQL> select banner from v$version where rownum=1;

BANNER
--------------------------------------------------------------------------------
Oracle Database 19c Enterprise Edition Release 19.0.0.0.0 - Production

SQL> variable ko timestamp;
Usage: VAR[IABLE] [ <variable> [ NUMBER | CHAR | CHAR (n [CHAR|BYTE]) |
            VARCHAR2 (n [CHAR|BYTE]) | NCHAR | NCHAR (n) |
            NVARCHAR2 (n) | CLOB | NCLOB | BLOB | BFILE
            REFCURSOR | BINARY_FLOAT | BINARY_DOUBLE ] ]
SQL> --
SQL> variable my_date varchar2(50) ;
SQL> begin
  2  select workingDate  into :my_date from MyTable;
  3  end;
  4  /

PL/SQL procedure successfully completed.

SQL> print my_date;

MY_DATE
------------------------------
20-JUN-20 10.55.55.264286 AM

SQL> print :my_date;

MY_DATE
------------------------------
20-JUN-20 10.55.55.264286 AM

SQL> --
SQL> select * from mytable;

WORKINGDATE
------------------------------
20-JUN-20 10.55.55.264286 AM

SQL> select workingDate  from MyTable where workingDate = to_timestamp(:my_date, 'DD-MON-YY HH.MI.SSXFF AM');

WORKINGDATE
------------------------------
20-JUN-20 10.55.55.264286 AM

SQL> 
pifor
  • 7,419
  • 2
  • 8
  • 16
  • I'm using sql developer, and this sql script is getting called from java code hibernate – Shelly Jun 20 '20 at 09:30
  • I don't know hibernate and I cannot tell if it calls SQLPlus or it can only execute only SQL and PL/SQL statements. – pifor Jun 20 '20 at 09:35
  • it executes sql and pl/sql but the above thing is possible in sql? – Shelly Jun 20 '20 at 09:38
  • It's SQL but it can *only* be executed by SQL*Plus. – pifor Jun 20 '20 at 09:39
  • I tried this statement select workingDate from MyTable where workingDate = to_timestamp(:my_date, 'DD-MON-YY HH.MI.SSXFF AM'); it ask me to enter data, instead of using the value of my_date, which it already has, (i'm using sql developer) – Shelly Jun 20 '20 at 09:54
  • 1
    @Shelly - you should know that what will run in SQL Developer, what will run in SQL\*Plus and what you can call from Hibernate are three different things. You could spend a lot of time getting a SQL\*Plus/SQL Developer script working how you want, and then find it's no use in Hibernate and you have to start again. – William Robertson Jun 20 '20 at 16:44
  • Ok so I've confirmed that we are not using SQL*Plus, we are purely dependent on sql developer, in that case what approach I can follow to get the desire result of my question. I've rephrased my qestion – Shelly Jun 21 '20 at 06:00
0

variable doesn't support dates or timestamps. Also your final query should use :my_date (indicating a host variable), not my_date.

If the aim is to populate tables using variables populated in a previous step, you could use PL/SQL for the whole task, e.g.

declare
    myDate date;
begin
    select some_col into myDate from wherever;

    insert into target_table (col1, col2, col2)
    select x, y, z
    from   source_table
    where  business_date = myDate;
end;

Or better still, define PL/SQL packages to encapsulate your processing logic.

Regarding the code you posted, from Oracle 12.1 onwards, you can declare a ref cursor within PL/SQL and have clients such as SQL*Plus simply call the block and print the results:

declare
    my_date timestamp;
    results sys_refcursor;
begin
    select systimestamp into my_date
    from   dual;

    open results for
        select my_date as my_date from dual;
    
    dbms_sql.return_result(results);
end;
/
PL/SQL procedure successfully completed.

ResultSet #1

MY_DATE
---------------------------------------------------------------------------
20-JUN-20 10.02.29.130000000

1 row selected.

For earlier Oracle versions you still have to declare the ref cursor as a host variable in SQL*Plus:

set autoprint on

var results refcursor

declare
    my_date timestamp;
begin
    select systimestamp into my_date
    from   dual;

    open :results for
        select my_date as my_date from dual;
end;
/

(Or set autoprint off and then print results explicitly.)

I don't know Java though, so I don't know whether any of the approaches above will work in your environment.

William Robertson
  • 15,273
  • 4
  • 38
  • 44