[英]How should I temporarily store data within a PL/SQL procedure?
我是PL / SQL的新手。 我在名為“ FLEX_PANEL_INSPECTIONS”的初始表中有數據,我正在嘗試使用PL / SQL過程在名為“ PANEL_STATUS_2”的第二個表中進行匯總。 但是,由於數據的性質,我必須編寫一個case語句,以便正確匯總FLEX_PANEL_INSPECTIONS中的數據。 因此,我創建了第三個中間表來橋接兩者(稱為“ PANEL_STATUS_1”),因為case語句不允許group by子句中的列專門對數據進行排序(據我所知-當出現以下情況時,我會報錯)我嘗試執行此操作)。 我不想在中間表中存儲數據-有什么方法可以使它臨時化(即僅在過程運行時才存在,以便不保留“ PANEL_STATUS_1”中的數據); 在過程中創建視圖,還是完全不需要中間表?
對於我對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;
/
您可以將臨時表創建為
create global temporary table gtt_panel_status
( column datatype ... )
on commit [delete|preserve] rows;
(在on commit
子句中指定delete
還是preserve
)。
但是,您通常不需要臨時表。 您可以嘗試with
子句(CTE),也可以沿select x, y, z from (select your subquery here)
行的內聯視圖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;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.