How to debug a stored procedure
Last modified: October 2, 2024
This example demonstrates how to debug Microsoft SQL Server stored procedure by stepping into the stored procedure. It also illustrates different debugging techniques such as setting breakpoints, viewing data items, and so on.
Create a sample procedure
In this topic, some sample database objects are used for demonstrating how to work with the dbForge Studio Debugger. Use scripts specified in this topic to create necessary objects in your test database.
Database procedure
Use the script below to create the procedure that takes two input parameters: DateFrom and DateTo. Based on these input parameters the table fills up with data.
CREATE PROCEDURE FillSchedule (IN DateFrom date
, IN DateTo date
, IN ScheduleId int)
SQL SECURITY INVOKER
READS SQL DATA
BEGIN
DECLARE DateOut date;
DECLARE TemplateCount tinyint;
DECLARE RowNumber tinyint;
DECLARE TimeSheetDate date;
SET
TemplateCount = (SELECT
COUNT(*)
FROM `schedule` s
JOIN scheduletemplatedetail sdt
ON s.TemplateId = sdt.TemplateId
WHERE s.ScheduleId = ScheduleId), DateOut = DateFrom
, RowNumber = 0;
SET TimeSheetDate = GetFirstDayOfMonth(DateFrom);
DROP TABLE IF EXISTS enumdays;
CREATE TEMPORARY TABLE enumdays (
DayOrder tinyint,
DateOut date,
TimeSheetDate date
);
WHILE (DateOut <= DateTo) DO
IF RowNumber = TemplateCount
THEN
SET RowNumber = 1;
ELSE
SET RowNumber := RowNumber + 1;
END IF;
INSERT INTO enumdays (DateOut
, DayOrder
, TimeSheetDate)
SELECT
DateOut,
RowNumber,
TimeSheetDate;
SET DateOut := DATE_ADD(DateOut, INTERVAL 1 DAY);
SET TimeSheetDate = GetFirstDayOfMonth(DateOut);
END WHILE;
DELETE
FROM s
USING scheduledetail s
WHERE s.ScheduleId = ScheduleId
AND s.DateOut BETWEEN DateFrom AND DateTo;
INSERT INTO scheduledetail (DateOut
, TimeSheetDate
, ScheduleId
, AbsenceCode)
SELECT
d.DateOut,
d.TimeSheetDate,
ScheduleId,
s.AbsenceCode
FROM enumdays d
CROSS JOIN (SELECT
s.TemplateId
FROM `schedule` s
WHERE s.ScheduleId = ScheduleId) s2
JOIN scheduletemplatedetail s
ON d.DayOrder = s.DayOrder
AND s.TemplateId = s2.TemplateId;
END
$$
Debug the stored procedure
Assume that you need to get Time Sheet for a specific time period. To do this, execute the FillSchedule procedure:
- In Database Explorer, choose your test database.
- Click Procedures and then right-click the FillSchedule procedure, and click Execute from the shortcut menu.
-
Enter the input parameters.
- Click OK.
Note
The actual document look may slightly vary from the above image, depending on the product you use.
A quick look through the code gives you nothing and you have no other choice but to start the debugger.
To start debugging the stored procedure:
- To compile the stored procedure for debugging, navigate to the required procedure in Database Explorer, select Compile, and then click Compile Dependants for Debugging from the shortcut menu.
- Click the Start Debugging button on the Debug toolbar, or press CTRL+F5.
- If you have not compiled the procedure, the dbForge Studio prompts you to compile it. Click Yes.
- Enter the stored procedure 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.
- To stop the debugging process, click the Stop button.
- Click Execute to run the stored procedure.
- You can see the message telling us that the stored procedure executed successfully. To verify this, you can retrieve the data from the table.
- In Database Explorer, choose the required database.
- Click Tables and then right-click the required table.
-
Click Retrieve Data from the shortcut menu.
-
dbForge Studio displays the result table.
Download dbForge Studio for SQL Server and try it absolutely free for 30 days!
Want to find out more?
Overview
Take a quick tour to learn all about the key benefits delivered by dbForge Studio for SQL Server.
All features
Get acquainted with the rich features and capabilities of the tool in less than 5 minutes.
Request a demo
If you consider employing this tool for your business, request a demo to see it in action.