SQLTreeo T-SQL Debugger · How-to

Find the statement that fails in a stored procedure

SQLTreeo T-SQL Debugger pauses on the statement that fails, before anything reacts, so you can look at the data and the open transaction, then fix and retry. What T-SQL tells you on its own.

SQLTreeo T-SQL Debugger pauses on the statement that fails, before anything reacts to the error. You can look at the data and the open transaction, fix them, and retry the statement. This article shows that with a small example, and then what T-SQL tells you on its own.

The example: a load that fails halfway

Run this in a test database. The staging table holds two rows named Bolt, and dbo.Item has a unique constraint on Name.

CREATE TABLE dbo.Item (ItemId int NOT NULL PRIMARY KEY, Name varchar(20) NOT NULL UNIQUE);
CREATE TABLE dbo.ItemStaging (ItemId int NOT NULL, Name varchar(20) NOT NULL);
INSERT dbo.ItemStaging VALUES (1, 'Bolt'), (2, 'Nut'), (3, 'Bolt');
GO
CREATE OR ALTER PROCEDURE dbo.LoadItems
AS
BEGIN
    BEGIN TRAN;
    INSERT dbo.Item (ItemId, Name) SELECT ItemId, Name FROM dbo.ItemStaging WHERE ItemId <= 2;
    INSERT dbo.Item (ItemId, Name) SELECT ItemId, Name FROM dbo.ItemStaging WHERE ItemId = 3;
    COMMIT;
END

The second INSERT fails on the duplicate Bolt. The first has already added two rows, and the transaction is still open.

Find the statement with SQLTreeo T-SQL Debugger: stop before the error has any effect

Switch Pause on errors on and run EXEC dbo.LoadItems; from the Batch tab. When the second INSERT fails, the run pauses before anything reacts to the error: before a CATCH block runs, and before SQL Server rolls anything back. The status bar and the error bar name the error number, the line and the message.

Now the transaction is open, with the first two rows in it, and you can look:

  1. Open the Query tab. It runs T-SQL on the connection the procedure runs on, so it sees the uncommitted rows. SELECT * FROM dbo.Item; shows the rows 1 and 2, and SELECT * FROM dbo.ItemStaging; shows the row that does not fit.
  2. Fix the data: UPDATE dbo.ItemStaging SET Name = 'Bolt 2' WHERE ItemId = 3;
  3. Press Retry. The failed statement runs again, now it succeeds, and the run goes on to COMMIT with all three rows.

What the buttons do:

  • Retry runs the statement again, after you fixed the data in the query window or the code in the procedure’s tab. After a code change, press Apply first.
  • Skip goes on with the next statement, as if the failed one had not been there.
  • Back to BEGIN TRAN rolls back the open transaction and puts the procedures back where they were at their BEGIN TRAN, with their variables as they were then. Use it to run the same part again with different data. It also works when SQL Server has already rolled the transaction back.
  • As SQL Server would lets the error take its normal course, as it would without the debugger: a CATCH block runs, the batch ends, or the transaction is rolled back.

Stop (Shift+F5) ends the run and rolls back the transaction it has open.

When this helps most

  • The error depends on data that is gone after the rollback, so it is hard to see in a CATCH block.
  • The failing statement is deep in a chain of procedures. The Call stack lists the procedures in the chain. Select one to see its variables and to run queries in it.
  • You need to try a fix without running the whole procedure again from the start.

Without the debugger: what T-SQL tells you

T-SQL can tell you where the error happened, if you catch it. Catch the error and ask where it happened:

BEGIN TRY
    EXEC dbo.LoadItems;
END TRY
BEGIN CATCH
    SELECT ERROR_NUMBER()    AS number,
           ERROR_PROCEDURE() AS procedure_name,
           ERROR_LINE()      AS line,
           ERROR_MESSAGE()   AS message,
           XACT_STATE()      AS xact_state,
           @@TRANCOUNT       AS trancount;
    IF @@TRANCOUNT > 0 ROLLBACK;
END CATCH;

The result is error 2627 in dbo.LoadItems at line 6, “Cannot insert duplicate key in object ‘dbo.Item’. The duplicate key value is (Bolt)”. Three things to know:

  • The line number counts from the CREATE PROCEDURE line, which is line 1. Line 6 is the second INSERT. When you open the procedure with Modify in SSMS, the script starts with a few lines of its own, so count from the ALTER PROCEDURE line, not from the top of the window.
  • ERROR_PROCEDURE() names the procedure where the error occurred, which can be a procedure that the one you called calls.
  • The transaction is still open in the CATCH block: XACT_STATE() is 1 and @@TRANCOUNT is 1. The two rows of the first INSERT are in it. Whether it commits or rolls back is your decision, and the code above rolls it back.

What T-SQL does not tell you is the data at that moment: which row is the duplicate, and what the table holds. By the time the CATCH block runs, you only have the message.

Good to know

  • The debugger runs the statements for real, so use a test database. What the procedure did before the pause stays in the database unless you roll it back with Back to BEGIN TRAN or Stop.
  • With As SQL Server would, a statement that changed nothing before it failed runs once more the normal way, and SQL Server reacts to the result.
  • Back to BEGIN TRAN does not give back identity values that were already used, and it does not undo anything outside the connection.

Next steps