The error you've got is correct v_today
is not a valid identifier within the scope of the EXECUTE IMMEDIATE statement. You should be using a bind variable instead:
declare
v_TODAY number;
begin
select TODAY into v_TODAY from LOCATION;
execute immediate 'truncate table table_EOD';
execute immediate 'insert into table_EOD (key1, key2, ASOF, IDATE)
select key1, key2, ASOF, IDATE
from table
where ASOF = to_char(to_date(:1)-1)
and DATE between :1 and to_char(to_date(:1)+1)'
using v_today, v_today, v_today;
end;
/
Two further points. Firsly, if you have more than one row in LOCATION then your select into ...
will not work. This is because v_today
can only hold one value. The exception TOO_MANY_ROWS will be raised; you will have selected too many rows!
Secondly, there's no need for the second EXECUTE IMMEDIATE. You only have to do DML in an execute immediate if an object you're referencing doesn't exist prior to the compilation of the block or if you're dynamically generating the SQL.
You still have to do the truncate
inside and EXECUTE IMMEDIATE statement as this is DDL. This leaves you with the following:
declare
v_TODAY number;
begin
select TODAY into v_TODAY from LOCATION; -- Still wrong!
execute immediate 'truncate table table_EOD';
insert into table_EOD (key1, key2, ASOF, IDATE)
select key1, key2, ASOF, IDATE
from table
where ASOF = to_char(to_date(v_TODAY)-1)
and DATE between v_TODAY and to_char(to_date(v_TODAY)+1);
end;
/
Okay, from your comment I've just realised that you actually want today not some random date and that you're using a number as a date in LOCATION, which is the cause of your error. Why don't you just use Oracle's [sysdate
] for today's date?
declare
v_TODAY date := sysdate;
begin
execute immediate 'truncate table table_EOD';
insert into table_EOD (key1, key2, ASOF, IDATE)
select key1, key2, ASOF, IDATE
from table
where ASOF = trunc(sysdate) - 1
and DATE between trunc(v_TODAY) and trunc(v_TODAY) + 1;
end;
/
If you want to use your own dates you will have to work out a way of putting a date instead of a number in LOCATION.