2014-04-02 43 views
0

我已經從一組表中提取數據,這些表需要有一個where子句和數據作爲列。 我的團隊在每次運行查詢時手動輸入日期都出錯。我想編寫一個PL SQL來從數據庫(oracle)動態獲取日期字段。 以下是查詢。需要PL/SQL腳本想法

select oli.cart_key,lext.minor_order_number,wlkp.mf_wh_loc_id,oli.warehouse_type from tab1 oli,tab2 lext,tab3 wlkp where 
oli.cart_line_key = lext.cart_line_key and 
oli.warehouse_code = wlkp.warehouse_code and 
oli.warehouse_type = 'DC' and 
lext.fulfillment_partner_key = 106 and 
lext.order_status_secondary = 2000 and 
lext.date_sent_to_fp is not null and 
to_char(lext.date_sent_to_fp ,'DD-MON-YYYY HH24:MI:SS') >= ('01-APR-2014 03:00:00') and 
oli.active_flag = 'Y' and wlkp.active_flag = 'Y' 
order by wlkp.mf_wh_loc_id 

日期字段'01 -APR-2014 03:00:00'是我想從雙重表中獲得的東西。是否有可能這樣做?

請讓我知道。

問候, 阿倫

+0

您需要在此澄清。你說你想要一個參數,那麼你說它應該來自雙重表格。雙表沒有日期字段,所以你可能想要前者?或者你想使用一個帶有日期字段的參數表? –

+0

謝謝弗蘭克的建議。我想在凌晨3點的日期和時間來自系統/數據庫是dd-MON-YYYY 03:00:00。由於查詢在每天凌晨3點手動運行 – user3168807

回答

0

如果你想獲得當前的日期,你可以使用SYSDATE

select oli.cart_key,lext.minor_order_number,wlkp.mf_wh_loc_id,oli.warehouse_type from 
tab1 oli,tab2 lext,tab3 wlkp 
where 
oli.cart_line_key = lext.cart_line_key and 
oli.warehouse_code = wlkp.warehouse_code and 
oli.warehouse_type = 'DC' and 
lext.fulfillment_partner_key = 106 and 
lext.order_status_secondary = 2000 and 
lext.date_sent_to_fp is not null and 
to_char(lext.date_sent_to_fp ,'DD-MON-YYYY HH24:MI:SS') >= 
(SELECT TO_CHAR(SYSDATE, 'DD-MON-YYYY HH24:MI:SS') FROM DUAL) and 
oli.active_flag = 'Y' and wlkp.active_flag = 'Y' 
order by wlkp.mf_wh_loc_id 
+0

感謝您的建議madhivanan。我唯一的限制就是時間場應該始終在凌晨03點。那是dd-MON-YYYY 03:00:00。 – user3168807

+0

然後使用SELECT TO_CHAR(SYSDATE,'DD-MON-YYYY 03:00:00')FROM DUAL – Madhivanan

+0

它說日期格式無法識別,當我運行這個修改後的查詢 – user3168807

1

請做比較日期日期類型。你不希望根據你的比較字母(DEC < NOV)。它不僅非常簡單,而且還可以在每條記錄中保存對to_char函數的調用(1毫秒記錄,1毫秒調用!!)。

select oli.cart_key, 
     lext.minor_order_number, 
     wlkp.mf_wh_loc_id, 
     oli.warehouse_type 
    from tab1 oli, 
    join tab2 lext on oli.cart_line_key = lext.cart_line_key 
    join tab3 wlkp ON oli.warehouse_code = wlkp.warehouse_code 
where oli.warehouse_type = 'DC' 
    and lext.fulfillment_partner_key = 106 
    and lext.order_status_secondary = 2000 
    and lext.date_sent_to_fp is not null 
    and lext.date_sent_to_fp >= Trunc(SYSDATE) +3/24 
    and oli.active_flag = 'Y' 
    and wlkp.active_flag = 'Y' 
order by wlkp.mf_wh_loc_id 

更新:如果您在凌晨3點運行在4月1日查詢和數據庫處於同一時區,你不需要做任何事情。系統日期將在當天凌晨3點使用該值。

但是如果您想要將值更改爲今天的3.AM,只需將「SYSDATE」更改爲「Trunc(SYSDATE)+3/24」,即可從新時間戳中刪除時間信息並添加3小時。

注:

  1. 我改變了你的查詢中使用聯接語法。它更清晰,有時它會提高性能。
  2. 你不需要雙重表格。你已經有一張桌子可以選擇 。
+0

感謝您的建議borjab。我唯一的限制就是時間場應該始終在凌晨03點。那是dd-MON-YYYY 03:00:00 – user3168807

+0

當前日期?但是你說這個查詢是在3.AM執行的。不是嗎? – borjab

+0

當天凌晨3點說今天是4月2日。那麼這個條款應該包含('02 -APR-2014 03:00:00'),明天它應該成爲('03 -APR-2014 03:00:00') – user3168807