此例基于OBE(Oracle By Example)的示例。是在SQL Developer中调试存储过程一文的进阶。复习了前文的一些基本概念,并纠正了官方示例的一个错误。
本文使用的是标准的HR 示例 Schema,调试的存储过程源代码如下:
sql
CREATE OR REPLACE PROCEDURE emp_list (
pmaxrows IN NUMBER
) AS
CURSOR emp_cursor IS
SELECT
l.state_province,
l.country_id,
d.department_name,
e.last_name,
j.job_title,
e.salary,
e.commission_pct
FROM
locations l,
departments d,
employees e,
jobs j
WHERE
l.location_id = d.location_id
AND d.department_id = e.department_id
AND e.job_id = j.job_id;
emp_record emp_cursor%rowtype;
TYPE emp_tab_type IS
TABLE OF emp_cursor%rowtype INDEX BY BINARY_INTEGER;
emp_tab emp_tab_type;
i NUMBER := 1;
BEGIN
OPEN emp_cursor;
FETCH emp_cursor INTO emp_record;
emp_tab(i) := emp_record;
WHILE
( emp_cursor%found )
AND ( i <= pmaxrows )
LOOP
i := i + 1;
FETCH emp_cursor INTO emp_record;
emp_tab(i) := emp_record;
END LOOP;
CLOSE emp_cursor;
FOR j IN REVERSE 1..i LOOP
dbms_output.put_line(emp_tab(j).last_name);
END LOOP;
END;
原文中的存储过程有错:
sql
WHILE ((emp_cursor%FOUND) AND (i <= pMaxRows) LOOP
此处改为了:
sql
WHILE (emp_cursor%FOUND) AND (i <= pMaxRows) LOOP
第一次调试出错:
错误提示很清晰,按下面赋权即可:
sql
grant DEBUG CONNECT SESSION, DEBUG ANY PROCEDURE to HR;
第二次调试仍出错:
sql
Connecting to the database Local-19c-HR-ORCLPDB1.
Executing PL/SQL: CALL DBMS_DEBUG_JDWP.CONNECT_TCP( '192.168.56.1', '51295' )
ORA-24247: network access denied by access control list (ACL)
ORA-06512: at "SYS.DBMS_DEBUG_JDWP", line 68
ORA-06512: at line 1
Process exited.
Disconnecting from the database Local-19c-HR-ORCLPDB1.
这会是ACL的原因,运行以下代码解决:
sql
begin
dbms_network_acl_admin.append_host_ace
(host=>'192.168.56.1',
ace=> sys.xs$ace_type(privilege_list=>sys.XS$NAME_LIST('JDWP') ,
principal_name=>'HR',
principal_type=>sys.XS_ACL.PTYPE_DB) );
end;
/
再次调试,以下是正确的信息:
sql
Connecting to the database Local-19c-HR-ORCLPDB1.
Executing PL/SQL: CALL DBMS_DEBUG_JDWP.CONNECT_TCP( '192.168.56.1', '51579' )
Debugger accepted connection from database on port 51579.
Source breakpoint: EMP_LIST.pls:30
设置断点,开始调试,输入值改为5:
输入Debug Host,即SQL Developer所在的主机:
程序运行到断点:
先来演示Run to Cursor
。将鼠标放到第44行,然后右键选择Run to Cursor
。
程序运行到当前Cursor处,Cursor这里指的是光标,而不是SQL中的游标:
再演示在运行中修改变量的值:
单击红框处,选中_value那行,右键选择Modify Value ...
:
修改原值King为James:
可以看到其生效了 ,然后点击上方的Resume按钮:
我们在输出中看到了James:
结束。