I have tried to optimize a select query with a temp table (I'm more used to SQL Server so maybe there's a better way).
The query is basically this (a bit simplified): create table, fill it, use it, drop table.
create global temporary table trucks (
nb_trucks int,
warehouse int
)
on commit preserve rows
;
insert into trucks (
nb_trucks, warehouse
)
select
sum(pla.NB_TRUCKS)
,pla.id_site
from ASI_MAS_PLA_PLANIFICATION pla
group by
pla.id_site
;
select distinct
op.operation_id as OPERATION_CODE,
pla.id_site as WAREHOUSE_ID,
(
select
coalesce(sum(nb_trucks), 0)
from trucks
where
and trucks.warehouse = pla.id_site
) as NB_TRUCKS,
from ASI_MAS_PLA_PLANIFICATION pla
inner join ASI_MAS_PLA_CL_PLANIF_OP op
on op.planif_operation_id = pla.z_planif_operation_id
where
pla.z_remainder = 0
group by
op.operation_id
,pla.id_site
order by
op.operation_id
;
truncate table trucks;
drop table trucks;
For various technical reasons, I need to execute these actions in a single script (actually I only need to execute the "select" part, but not using a temp table sends the execution time through the roof).
I have placed the script inside a begin ... end;
block but it doesn't like that I "create" a table as first instruction.
As I saw here, I'm currently trying to wrap each statement with a "execute immediate" like this but I'm not very fond of this solution.
How can I execute multiple statements, including a "create", inside a single SQL script?