2011-06-28 95 views
1

我有一個執行sql語句的程序。在一次交易中,我希望使用相同的 sysdate更新多個表。 例如。如果這3個語句在一個事務中執行(運行在一個事務中以下3條語句)有沒有辦法使用** ** ** sysdate來執行多個sql語句?

update table1 set some_col = 'updated' where some_other_col < sysdate; 
delete from table2 where some_col < sysdate; 
insert into table3 (col1, col2) select c1, c2 from table4 where some_col < sysdate; 

,在「SYSDATE」每個人在使用將我們在目前的這種說法無論時間戳運行,不在交易開始時。

可能創建一個存儲過程,並最初使用PL/SQL選擇sysdate到一個變量,但我寧願只從外部程序運行sql語句。

+0

我想你回答了你自己的問題。每個查詢將有一個稍微不同的日期時間的sysdate你正在做的上面。我認爲你需要製作存儲過程。 – Limey

回答

5

我可以創建一個存儲過程, 最初選擇SYSDATE成 變量,使用PL/SQL,但我更喜歡 剛剛從 外部程序運行SQL語句

使用匿名塊代替存儲過程,類似於(未測試):

declare 
    v_sysdate date := sysdate; 
begin 
    update table1 set some_col = 'updated' where some_other_col < v_sysdate; 
    delete from table2 where some_col < v_sysdate; 
    insert into table3 (col1, col2) select c1, c2 from table4 where some_col < v_sysdate; 
    commit; 
exception 
    when others then 
    rollback; 
    raise; 
end; 
+0

你可以像普通的sql一樣運行/執行它(即從外部程序)? – Gerrat

+0

...我不相信這和存儲過程有什麼區別 - 除了它沒有被存儲...它仍然是PL/SQL,而不是SQL,並且不能被外部程序運行(直接) – Gerrat

+0

存儲過程和匿名塊之間有很多不同之處。什麼「外部程序」不能直接運行(但可以運行單個DML語句)? – tbone

0

恐怕它的工作原理應該是這樣,t他會重新計算每個查詢的時間。只需將時間戳存儲在程序中的變量中,然後在查詢中使用該變量即可。

0

哪個外部程序?如果您在使用SQL * Plus,這會工作:

var d char(50) 
begin select sysdate into :d from dual; end; 
/

update table1 set some_col = 'updated' where some_other_col < :d; 
delete from table2 where some_col < :d; 
insert into table3 (col1, col2) select c1, c2 from table4 where some_col < :d; 

你可能需要調整你的NLS_DATE_FORMAT設置爲會議...

相關問題