0

Oracle中如何获取事务开始时间?是否有这样的内置函数可用于查询?

更具体地说,我需要一个函数,我们称之为它trans_begin_time(),以这种方式使用:

UPDATE MYTABLE
SET batch_update_time = trans_begin_time()
WHERE some_criteria 

如果存在这样的功能,它将通过两种方式帮助我:

首先,我可以使用相同的日期值更新所有记录 - 请注意sysdate这不起作用,因为这是一个非常大的表,并且sysdate会给出不同的值。

其次,我可以在任何地方使用它,而无需sysdate事先存储值,然后在查询中使用它。

谢谢。

4

2 回答 2

3
11:43:20 SYSTEM@sandbox> create table test as select * from dual;         

Table created.                                                                 

Elapsed: 00:00:00.04                                                           
11:43:26 SYSTEM@sandbox> set transaction read write name 'test';          

Transaction set.                                                               

Elapsed: 00:00:00.00                                                           
11:43:35 SYSTEM@sandbox> insert into test values (1);                     

1 row created.                                                                 

Elapsed: 00:00:00.00                                                           
11:43:42 SYSTEM@sandbox> select name, start_time from v$transaction;      

NAME       START_TIME                                                          
---------- --------------------                                                
test       10/12/12 11:43:34                                                   

Elapsed: 00:00:00.01                                                           
11:43:47 SYSTEM@sandbox> commit;                                          

Commit complete.                                                               

Elapsed: 00:00:00.01                                                           
11:44:00 SYSTEM@sandbox> select name, start_time from v$transaction;      

no rows selected                                                               

Elapsed: 00:00:00.00                                                           
11:44:02 SYSTEM@sandbox>                     

或者,对于每个会话:

11:59:15 SYSTEM@Sandbox> set transaction name 'test_by_addr';                           

Transaction set.                                                                             

Elapsed: 00:00:00.00                                                                         
11:59:18 SYSTEM@Sandbox> insert into test values(1);                                    

1 row created.                                                                               

Elapsed: 00:00:00.00                                                                         
11:59:21 SYSTEM@Sandbox> get xact                                                       
  1  select s.sid                                                                            
  2        ,s.serial#                                                                        
  3        ,s.username                                                                       
  4        ,s.status                                                                         
  5        ,t.name                                                                           
  6        ,t.start_time                                                                     
  7* from v$transaction t join v$session s on t.addr = s.taddr                               
11:59:27 SYSTEM@Sandbox> /                                                              

       SID    SERIAL# USERNAME                       STATUS   NAME       START_TIME          
---------- ---------- ------------------------------ -------- ---------- --------------------
        39       1015 SYSTEM                         ACTIVE   test_by_ad 10/12/12 11:59:17   
                                                              dr                             


Elapsed: 00:00:00.01                                                                         
11:59:30 SYSTEM@Sandbox> select SYS_CONTEXT('USERENV','SID') from dual;                 

SYS_CONTEXT('USERENV','SID')                                                                 
----------------------------
39                                                                                           

Elapsed: 00:00:00.01                                                                         
于 2012-10-12T04:01:05.707 回答
1

您可以使用为单个会话返回相同日期的函数。

SQL> CREATE OR REPLACE PACKAGE pkg AS
  2     FUNCTION get_session_date RETURN DATE;
  3  END;
  4  /

Package created.

SQL> CREATE OR REPLACE PACKAGE BODY pkg AS
  2     g_date DATE;
  3     FUNCTION get_session_date RETURN DATE IS BEGIN RETURN g_date; END;
  4  BEGIN
  5     g_date := SYSDATE;
  6  END;
  7  /

Package body created.

SQL> SELECT to_char(pkg.get_session_date, 'dd/mm/yyyy hh24:mi:ss') FROM dual;

TO_CHAR(PKG.GET_SES
-------------------
12/10/2012 10:07:32

SQL> EXEC dbms_lock.sleep(10);

PL/SQL procedure successfully completed.

SQL> SELECT to_char(pkg.get_session_date, 'dd/mm/yyyy hh24:mi:ss') FROM dual;

TO_CHAR(PKG.GET_SES
-------------------
12/10/2012 10:07:32
于 2012-10-12T08:08:52.597 回答