2017-07-31 29 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 < =”,导致收盘时间采取那里,做出正确的,如果它不是。