How to debug a stored procedure

Last modified: May 28, 2025

The guide provides a step-by-step procedure how to debug a SQL Server stored procedure by stepping into it and illustrates different debugging techniques, such as setting breakpoints, viewing data items, etc.

Note

Prior to debugging, it is recommended to execute the Compile for Debugging operation, which adds debugging information to the procedure. After debugging, execute the Compile operation. These steps are detailed in the Debug the stored procedure section of this topic.

It is important to note that before performing these operations, it is advisable to back up the database.

Create a test database and populate it with tables

Before you start debugging a stored procedure, it’s required to create a test database in dbForge Studio for MySQL and then populate it with data.

To create a test database, execute the following script:

CREATE DATABASE TestScheduleDB;
GO

To create the test tables and populate them with data, execute the following script that creates three tables - schedule, scheduledetail, and scheduletemplatedetail.

USE TestScheduleDB;
GO

CREATE TABLE dbo.ScheduleTemplateDetail (
  TemplateId int NOT NULL,
  DayOrder int NOT NULL,
  AbsenceCode varchar(25) NULL,
  WorkShiftCd varchar(25) NULL,
  PRIMARY KEY CLUSTERED (TemplateId, DayOrder)
)
GO

CREATE TABLE dbo.ScheduleDetail (
  ScheduleId int NOT NULL,
  DateOut date NOT NULL,
  TimeSheetDate date NOT NULL,
  AbsenceCode varchar(25) NULL,
  PRIMARY KEY CLUSTERED (ScheduleId, DateOut)
)

CREATE TABLE dbo.Schedule (
  ScheduleId int IDENTITY,
  TemplateId int NOT NULL,
  PRIMARY KEY CLUSTERED (ScheduleId)
)
GO

INSERT INTO ScheduleTemplateDetail (TemplateId, DayOrder, AbsenceCode, WorkShiftCd)
VALUES
(1, 1, 'Vacation', 'Morning'),
(1, 2, 'Sick Leave', 'Afternoon'),
(1, 3, 'Paid Leave', 'Night'),
(2, 1, 'Sick Leave', 'Morning'),
(2, 2, 'Vacation', 'Afternoon'),
(2, 3, 'Paid Leave', 'Night'),
(3, 1, 'Paid Leave', 'Morning'),
(3, 2, 'Sick Leave', 'Afternoon'),
(3, 3, 'Vacation', 'Night'),
(4, 1, 'Paid Leave', 'Morning');


INSERT INTO Schedule (TemplateId)
VALUES
(1),
(2),
(3),
(4),
(1),
(2),
(3),
(4),
(1),
(2);

INSERT INTO ScheduleDetail (ScheduleId, DateOut, TimeSheetDate, AbsenceCode)
VALUES
(1, '2025-04-01', '2025-04-01', 'Vacation'),
(2, '2025-04-01', '2025-04-01', 'Sick Leave'),
(3, '2025-04-01', '2025-04-01', 'Paid Leave'),
(4, '2025-04-01', '2025-04-01', 'Vacation'),
(5, '2025-04-01', '2025-04-01', 'Sick Leave'),
(6, '2025-04-01', '2025-04-01', 'Paid Leave'),
(7, '2025-04-01', '2025-04-01', 'Vacation'),
(8, '2025-04-01', '2025-04-01', 'Sick Leave'),
(9, '2025-04-01', '2025-04-01', 'Paid Leave'),
(10, '2025-04-01', '2025-04-01', 'Vacation');

Create a stored procedure

Execute the following script to create the FillSchedule stored procedure that takes three input parameters - DateFrom, DateTo, and ScheduleId:

CREATE PROCEDURE FillSchedule 
  @pDateFrom DATE,
  @pDateTo DATE,
  @pScheduleId INT
AS
BEGIN
  DECLARE @DateOut DATE;
  DECLARE @TemplateCount INT;
  DECLARE @RowNumber INT;
  DECLARE @TimeSheetDate DATE;

  -- Get templates
  SET @TemplateCount = (
    SELECT COUNT(*) 
    FROM Schedule s
    JOIN ScheduleTemplateDetail std ON s.TemplateId = std.TemplateId
    WHERE s.ScheduleId = @pScheduleId
  );
  
  SET @DateOut = @pDateFrom;
  SET @RowNumber = 0;
  SET @TimeSheetDate = dbo.GetFirstDayOfMonth(@pDateFrom);

  -- Use a temporary table for the data
  IF OBJECT_ID('tempdb..#EnumDays') IS NOT NULL DROP TABLE #EnumDays;
  CREATE TABLE #EnumDays (
    DayOrder INT,
    DateOut DATE,
    TimeSheetDate DATE
  );

  WHILE @DateOut <= @pDateTo
  BEGIN
    IF @RowNumber = @TemplateCount 
    BEGIN
      SET @RowNumber = 1;
    END
    ELSE
    BEGIN
      SET @RowNumber = @RowNumber + 1;
    END

    INSERT INTO #EnumDays (DateOut, DayOrder, TimeSheetDate)
    VALUES (@DateOut, @RowNumber, @TimeSheetDate);

    SET @DateOut = DATEADD(DAY, 1, @DateOut);
    SET @TimeSheetDate = dbo.GetFirstDayOfMonth(@DateOut);
  END;

  DELETE FROM ScheduleDetail 
  WHERE ScheduleId = @pScheduleId
    AND DateOut BETWEEN @pDateFrom AND @pDateTo;

  INSERT INTO ScheduleDetail (DateOut, TimeSheetDate, ScheduleId, AbsenceCode)
  SELECT 
    d.DateOut,
    d.TimeSheetDate,
    @pScheduleId,
    s.AbsenceCode
  FROM #EnumDays d
  CROSS JOIN (
    SELECT TemplateId FROM Schedule WHERE ScheduleId = @pScheduleId
  ) s2
  JOIN ScheduleTemplateDetail s
    ON d.DayOrder = s.DayOrder AND s.TemplateId = s2.TemplateId;
END;

Debug the stored procedure

Functions, unlike procedures, return a value as a result of their execution that you can use in a statement. For example, you can call a function and get a result with the SELECT statement. However, you cannot run this statement for a procedure or use a procedure value in it. That’s why it’s required to create a function and assign its value to the TimeSheetDate variable, located in the FillSchedule procedure.

To create a function, execute the following script:

CREATE FUNCTION dbo.GetFirstDayOfMonth (@pDate DATETIME)
RETURNS DATETIME
AS
BEGIN
  RETURN DATEADD(DAY, 1, DATEADD(MONTH, DATEDIFF(MONTH, 0, @pDate), 0));
END;

Note

The function must be created in the same database where you previously created the stored procedure.

To debug a stored procedure:

1. To compile the stored procedure for debugging, in Database Explorer, right-click the required procedure and select Compile > Compile Dependants for Debugging from the shortcut menu.

2. On the Debug toolbar, select Start Debugging or press CTRL+F5.

3. If you have not compiled the procedure, the dbForge Studio prompts you to compile it. To proceed, select Yes.

4. In the Edit Parameters window that opens, enter the input parameters and select OK to apply the changes.

Input parameters

You can see that the yellow arrow identifies the stack frame where the execution pointer is currently located.

5. To stop the debugging process, on the Debug toolbar, select Stop.

6. Select Execute to run the stored procedure.

7. You can see the message informing you that the stored procedure has been executed successfully.

To verify this, retrieve the data from the table:

1. In Database Explorer, select the required database and expand the Tables node.

2. Right-click the required table and select Retrieve Data from the shortcut menu.

3. dbForge Studio displays the result table.

Result

Download dbForge Studio for SQL Server and try it absolutely free for 30 days!