This example demonstrates how to debug an Oracle stored procedure using PL/SQL Debugger for Oracle built into dbForge Studio for Oracle. It also illustrates different debugging techniques such as setting breakpoints, viewing data items, and so on. In this topic, sample database objects are created with the help of dbForge Studio for Oracle. In our example, we use a SCHEDULE_DETAIL test table and a SCHEDULE test database.
For the demo, we will do the following:
To create the FillSchedule procedure, do the following:
In the document that opens, copy, and paste the following script.
CREATE OR REPLACE PROCEDURE SCHEDULE.FILLSCHEDULE(DATE_FROM IN DATE,
DATE_TO IN DATE,
SCHEDULE_ID IN INT)
AS
TEMPLATE_COUNT SMALLINT;
TEMPLATE_ID INT;
DAY_COUNT INT;
TYPE REC_TYPE IS RECORD (
DATE_OUT DATE,
DAY_ORDER SMALLINT,
TIME_SHEET_DATE DATE
);
TYPE ENUM_TYPE IS TABLE OF REC_TYPE;
DAY_ENUM ENUM_TYPE;
DAY_ENUM2 REC_TYPE1;
BEGIN
DAY_ENUM2 := REC_TYPE1(DATE_FROM, NULL, SYSDATE);
DAY_ENUM2.ADMIT(1);
INSERT INTO SCHEDULE.WORK (
DATE_OUT, DAY_ORDER, TIME_SHEET_DATE
)
VALUES (DAY_ENUM2.DATE_OUT -- DATE_OUT - DATE
, DAY_ENUM2.DAY_ORDER -- DAY_ORDER - NUMBER(*, 0)
, DAY_ENUM2.TIME_SHEET_DATE -- TIME_SHEET_DATE - DATE
);
SELECT COUNT(*),
MAX(S.TEMPLATEID)
INTO TEMPLATE_COUNT,
TEMPLATE_ID
FROM SCHEDULE.SCHEDULE S
JOIN SCHEDULE.SCHEDULE_TEMPLATE_DETAIL STD
ON S.TEMPLATEID = STD.TEMPLATEID
WHERE S.SCHEDULEID = SCHEDULE_ID;
-- IF (TEMPLATE_ID IS NULL) THEN
-- RAISE_APPLICATION_ERROR(-20001, 'TEMPLATE_ID IS NULL' );
-- END IF;
SELECT DATE_OUT,
DAY_ORDER,
GETFIRSTDAYOFMONTH(DATE_OUT)
BULK COLLECT INTO DAY_ENUM
FROM (SELECT DATE_FROM - 1 + ROWNUM AS DATE_OUT,
NVL(NULLIF(MOD(ROWNUM, TEMPLATE_COUNT), 0), TEMPLATE_COUNT) AS DAY_ORDER
FROM ALL_OBJECTS
WHERE ROWNUM <= DATE_TO - DATE_FROM + 1) T;
DAY_COUNT := DAY_ENUM.COUNT;
-- IF (DAY_COUNT = 0) THEN
-- RAISE_APPLICATION_ERROR(-20001, 'ENUM IS EMPTY' );
-- END IF;
DELETE FROM SCHEDULE.SCHEDULE_DETAIL
WHERE SCHEDULEID = SCHEDULE_ID
AND DATEOUT BETWEEN DATE_FROM AND DATE_TO;
FOR I IN DAY_ENUM.FIRST .. DAY_ENUM.LAST
LOOP
INSERT INTO SCHEDULE.SCHEDULE_DETAIL (
TIMESHEETDATE, DATEOUT, SCHEDULEID, ABSENCECODE, WORKSHIFTCD
)
SELECT DAY_ENUM(I).TIME_SHEET_DATE,
DAY_ENUM(I).DATE_OUT,
SCHEDULE_ID,
STD.ABSENCECODE,
STD.WORKSHIFTCD
FROM SCHEDULE.SCHEDULE_TEMPLATE_DETAIL STD
WHERE STD.TEMPLATEID = TEMPLATE_ID
AND STD.DAYORDER = DAY_ENUM(I).DAY_ORDER;
DBMS_OUTPUT.PUT_LINE(DAY_ENUM(I).TIME_SHEET_DATE || ', ' || DAY_ENUM(I).DATE_OUT || ', ' || DAY_ENUM(I).DAY_ORDER);
END LOOP;
END;
/
The procedure has two input parameters: DateFrom and DateTo. Based on these input parameters the table is populated with test data.
Assume that we need to get Time Sheet for a specific time period. For this, we need to execute the procedure.
To debug a stored procedure, it should be executable.
To execute the FillSchedule procedure, do the following:
In the Edit Parameters:<procedure_name> dialog box, enter input parameters.
The pop-up window informing about the successful completion and the execution time appears.
Prior to debugging, you should compile the stored procedure for debugging. Otherwise, dbForge Studio for Oracle prompts you to compile it.
To compile the stored procedure for debugging, do the following:
In Database Explorer, right-click the procedure you want to compile, click Compile, and then select one of the following options on the shortcut menu:
In the compiling window that opens, the object you want to compile is selected by default. Click Compile, and then Close.
To start debugging the stored procedure, do the following:
On the Debug toolbar, click Start Debugging or press Ctrl + F5.
Note:
If you have not compiled the procedure, the dbForge Studio prompts you to compile it. In the pop-up window that opens, click Yes. In the Edit Parameters:<procedure_name> dialog box, specify the input parameters once again and click OK.
You can see that the yellow arrow identifies the stack frame where the execution pointer is currently located.
The Watches pane displays information about variables, their value, and type.
The Call Stack pane displays the line of the code where Oracle PL/SQL Debugger is currently located.
To set a breakpoint, use one of the following options:
In the Debug layout mode, you can use the following options:
After the debugging of the stored procedure has stopped, you can execute it. For this, on the Standard toolbar, click Execute. In the pop-up window informing that the stored procedure has been executed successfully, click Ok.
To verify this, you can retrieve the data from the table. To do this, perform the following steps:
In the document that opens, you can view the retrieved data on the results grid.