如何獲取繫結變數

楊奇龍發表於2011-03-10
oracle 在10g 時 可以捕獲繫結變數並且存到AWR中,但是有諸多不便:    
    Captured periodically(_cursor_bind_capture_interval), not at real time.
    Captured under maximum size(_cursor_bind_capture_area_size)
    Only bind variables in WHERE clause are captured
做如下實驗來演示如何獲取 繫結變數。
sys@ORACL> var  bid number;
sys@ORACL> var eid number;
sys@ORACL> exec :bid :=dbms_workload_repository.create_snapshot;

PL/SQL 過程已成功完成。

sys@ORACL> var b1 number;
sys@ORACL> var b2 number;
sys@ORACL> var b3 varchar2(1);

sys@ORACL> begin
  2  :b1 :=1;
  3  :b2 :=2000;
  4  :b3 :='s';
  5  end;
  6  /

PL/SQL 過程已成功完成。
sys@ORACL> update yang.t set object_name= :b3 where object_id between :b1 and :b2;
已更新119680行。
sys@ORACL> commit;
提交完成。
sys@ORACL> exec :eid :=dbms_workload_repository.create_snapshot;
PL/SQL 過程已成功完成。

sys@ORACL> var sqlid varchar2(100);

sys@ORACL> begin
  2  select sql_id into :sqlid
  3  from dba_hist_sqltext
  4  where sql_text like 'update yang.t%';
  5  end;
  6  /

PL/SQL 過程已成功完成。

sys@ORACL> print sqlid;

SQLID
--------------------
1bgn2wm9nad9j

sys@ORACL> col name for a10
sys@ORACL> col postion for a99
sys@ORACL> col value_string for a20

sys@ORACL> select snap_id,name,position,value_string,last_captured
  2  from dba_hist_sqlbind
  3  where sql_id=:sqlid
  order by snap_id desc;

   SNAP_ID NAME      POSITION VALUE_STRING         LAST_CAPTURED
---------- ---------- ---------- -------------------- --------------
       105 :B3                 1
       105 :B2                 3                2000                 10-3月 -11
       105 :B1                 2                 1                    10-3月 -11

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/22664653/viewspace-689029/,如需轉載,請註明出處,否則將追究法律責任。

相關文章