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.