我应该如何在 PL/SQL 过程中临时存储数据?

How should I temporarily store data within a PL/SQL procedure?

我对 PL/SQL 很陌生。我在一个名为 'FLEX_PANEL_INSPECTIONS' 的初始 table 中有数据,我试图使用 PL/SQL 过程在第二个 table 中总结数据,名为 'PANEL_STATUS_2'。然而,由于数据的性质,我不得不写一个案例陈述来正确地总结来自 FLEX_PANEL_INSPECTIONS 的数据。因此,我创建了第三个中间 table 来桥接两者(名为 'PANEL_STATUS_1'),因为 case 语句不允许 group by 子句中的列专门对数据进行排序(据我所知) - 当我尝试执行此操作时出现错误)。我不想在中间存储数据 table - 有什么方法可以使它成为临时的(即仅在程序运行时存在,以便不保留来自 'PANEL_STATUS_1' 的数据);在过程中创建一个视图,或者完全不需要中间 table?

任何对我的错误/误解 PL/SQL 的帮助或批评将不胜感激。这是我写的代码:

create or replace procedure PANEL_STATUS_PROCEDURE (panel_lot_id in number) as

begin

--Populate intermediate table with information about the status of the panels.
insert into PANEL_STATUS_1 (FLEX_LOT_ID, FLEX_PANEL_DMX, FLEX_PANEL_STATUS)   
select FLEX_LOT_ID, FLEX_PANEL_DMX,

--Sum the status values of the 4 panel inspections. A panel passes if and only if this sum = 4. 
case sum (FLEX_PANEL_STATUS)
    when 4 then 1
    else 0

end as new_panel_status

from FLEX_PANEL_INSPECTIONS
where FLEX_LOT_ID = panel_lot_id
group by FLEX_LOT_ID, FLEX_PANEL_DMX;

--Add information about the machine ID and the upload time to this table.
insert into PANEL_STATUS_2 (FLEX_LOT_ID, FLEX_PANEL_DMX, FLEX_PANEL_STATUS, MACHINE_ID, UPLOAD_TIME)
select distinct PANEL_STATUS_1.*, MACHINE_ID, UPLOAD_TIME
from PANEL_STATUS_1, FLEX_PANEL_INSPECTIONS

where (FLEX_PANEL_INSPECTIONS.FLEX_LOT_ID = PANEL_STATUS_1.FLEX_LOT_ID
       and FLEX_PANEL_INSPECTIONS.FLEX_PANEL_DMX = PANEL_STATUS_1.FLEX_PANEL_DMX)

and FLEX_PANEL_INSPECTIONS.FLEX_LOT_ID = panel_lot_id;

end PANEL_STATUS_PROCEDURE;
/

您可以将临时 table 创建为

create global temporary table gtt_panel_status
( column datatype ... )
on commit [delete|preserve] rows;

(在 on commit 子句中指定 deletepreserve)。

但是您通常不需要临时 table。您可以尝试使用 with 子句 (CTE),或者使用 select x, y, z from (select your subquery here).

行的内联视图

编辑:实际上更多地查看您的查询,我认为您实际需要的是一个分析 sum,即没有聚合的总数。例如,像这样:

create or replace procedure panel_status_procedure
    ( panel_lot_id in number )
as
begin
    -- Add information about the machine ID and the upload time to this table.
    insert into panel_status_2
         ( flex_lot_id
         , flex_panel_dmx
         , flex_panel_status
         , machine_id
         , upload_time )
    select distinct
           flex_lot_id
         , flex_panel_dmx
         , case sum(flex_panel_status) over (partition by flex_lot_id, flex_panel_dmx)
               when 4 then 1
               else 0
           end
         , machine_id
         , upload_time
    from   flex_panel_inspections pi
    where  pi.flex_lot_id = panel_lot_id;

end panel_status_procedure;