2017-07-31 28 views
0

我對Oracle很新穎。如何在Oracle中使用或創建臨時表

我只是當我試圖實現以下邏輯卡住。我在oracle中創建一個sql腳本,這將幫助我生成一個報告。這個腳本每天運行兩次,所以我不應該在下次運行時選擇相同的文件。

1)運行查詢並保存結果setand當作業運行時將訂單ID存儲在臨時表中@ 11 Am 2)運行查詢第二次@ 3 pm檢查臨時表並返回結果集不在臨時表中。

以下查詢將生成結果集,但不確定如何創建臨時表並在運行時生效。

select 
     rownum as LineNum, 
     'New' as ActionCode, 
     ORDER_ID, 
     AmountType, 
     trun(sysdate), 
     trun(systime) 
     from crd.V_IVZ_T19 t19 
where 
    (t19.acct_cd in 
     (select fc.child_acct_cd 
      from cs_config fc 
     where fc.parent_acct ike 'G_TRI_RPT')) 

and t19.date>= trunc(sysdate) 
and t19.date<= trunc(sysdate); 

任何幫助非常感謝。我不知道如何只獲得時間戳。

+0

你的日期檢查看起來像你想檢查一個範圍,但只會檢查t19.date是當前日期。 – BriteSponge

+3

您可以創建一個臨時表的地方,因爲一旦會話斷開,會刪除臨時表的數據。在普通表中,您存儲結果並在第二天處理之前,截斷表並在當天再次加載該表 – XING

+0

@BriteSponge,t19.date是視圖中的Orderdate。 – Usher

回答

1

TEMP表不是這裏的主意,導致臨時表數據不會長時間存儲數據(只是爲了一個會話),你只需要創建一個普通表。希望它會幫助你:

--- table for storing ORDER_ID for further checking, make a correct DataType, you also can add date period in the table to control expired order_ids'; 

CREATE TABLE order_id_store (
     order_id NUMBER, 
     end_date DATE 
); 


--- filling the table for further checking 
INSERT INTO order_id_store 
     SELECT ORDER_ID, trunc(sysdate) 
       FROM crd.V_IVZ_T19 t19 
       WHERE t19.order_id NOT IN (SELECT DISTINCT order_id FROM order_id_store) 
       AND t19.date>= trunc(sysdate) 
       AND t19.date<= trunc(sysdate); 

--- delete no need data by date period, for example for last 2 days: 
DELETE FROM order_id_store WHERE end_date <= trunc(SYSDATE - 2); 
COMMIT; 

---- for select report without already existed data 
SELECT 
     rownum as LineNum, 
     'New' as ActionCode, 
     ORDER_ID, 
     AmountType, 
     trun(sysdate), 
     trun(systime) 
FROM crd.V_IVZ_T19 t19 
WHERE 
    (t19.acct_cd in 
     (select fc.child_acct_cd 
      from cs_config fc 
     where fc.parent_acct ike 'G_TRI_RPT')) 
    AND t19.order_id NOT IN (SELECT DISTINCT order_id FROM order_id_store) 
    AND t19.date>= trunc(sysdate) 
    AND t19.date<= trunc(sysdate); 

我不知道你的「t19.date> =」和「t19.date < =」,導致收盤時間採取那裏,做出正確的,如果它不是。