程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> DB2數據庫 >> DB2教程 >> DB2中查看存儲過程裡SQL語句的執行情況

DB2中查看存儲過程裡SQL語句的執行情況

編輯:DB2教程

本文為您介紹如何在DB2數據庫中,查看存儲過程裡SQL語句的執行情況。其中可以用snapshot查看動態SQL,用event monitor查看存儲過程裡SQL語句的執行情況。如果您對此有興趣,不妨一看,相信對您會有所幫助。

動態SQL可以用snapshot查看,存儲過程裡SQL語句的執行情況用event monitor查看。
但是event monitor沒有記錄SQL語句,而是記錄了package id和Section id。
根據package id和Section id再查詢系統表就可以得到原始的SQL語句。

例子如下:
(1)創建存儲過程
create procedure sales_status
(in quota integer)
dynamic result sets 2
language sql
begin
declare SQLSTATE char(5);

declare rs cursor with return for
select sales_person, sum(sales) as total_sales
from sales
group by sales_person
having sum(sales) > quota;

open rs;

insert into tt1 values (1),(2),(3),(4),(5);

end
@

(2)創建event monitor,並捕獲statement信息
db2 create event monitor ev2 for statements write to file 'D: mp'
db2 set event monitor ev2 state 1
db2 "call sales_status(10)"
db2 FLUSH EVENT MONITOR ev2
db2evmon -db sample -evm ev2 > 1.out

(3)查看輸出文件
在call sales_status(10)語句後面,可以找到
8) Statement Event ...
Appl Handle: 7
Appl Id: *LOCAL.DB2.070809034142
Appl Seq number: 00053

Record is the result of a flush: FALSE
-------------------------------------------
Type : Static
Operation: Execute
Section : 2
Creator : DB2ADMIN
Package : P2323139
Consistency Token : oAfgMJIX
Package Version ID : 
Cursor : 
Cursor was blocking: FALSE
-------------------------------------------
Start Time: 2007-08-09 12:33:22.394140
Stop Time: 2007-08-09 12:33:22.394599
Exec Time: 0.000459 seconds
Number of Agents created: 1
User CPU: 0.000000 seconds
System CPU: 0.000000 seconds
Fetch Count: 0
Sorts: 0
Total sort time: 0
Sort overflows: 0
Rows read: 1
Rows written: 5
...

10) Statement Event ...
Appl Handle: 7
Appl Id: *LOCAL.DB2.070809034142
Appl Seq number: 00053

Record is the result of a flush: FALSE
-------------------------------------------
Type : Static
Operation: Close
Section : 1
Creator : DB2ADMIN
Package : P2323139
Consistency Token : oAfgMJIX
Package Version ID : 
Cursor : RS
Cursor was blocking: TRUE
-------------------------------------------
Start Time: 2007-08-09 12:33:22.390159
Stop Time: 2007-08-09 12:33:22.398984
Exec Time: 0.008825 seconds
Number of Agents created: 1
User CPU: 0.000000 seconds
System CPU: 0.000000 seconds
Fetch Count: 3
Sorts: 1
Total sort time: 0
Sort overflows: 0
Rows read: 45
Rows written: 0
...

看到Package=P2323139, Section=1的SQL有 Rows read: 45
看到Package=P2323139, Section=2的SQL有 Rows read: 1, Rows written: 5

(4)查詢系統表,就可以看到原始的SQL語句了
select s.STMTNO, s.SECTNO, s.TEXT 
from SYSCAT.STATEMENTS s 
where s.PKGNAME='P2323139' ;

STMTNO SECTNO TEXT ----------- ------ -------
8 1 DECLARE RS cursor with return for select SALES_PERSON, SUM(SALES) as TOTAL_SALES from SALES group by SALES_PERSON having SUM(SALES) > :HV00008 :HI00008 
17 2 insert into TT1 values (1),(2),(3),(4),(5) 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved