2011-06-28 99 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设置为会议...

相关问题