Published on

November 28, 2022

How to Parse and Debug SQL Server Code in SSMS

When working with large SQL scripts, it can be challenging to identify all errors in your code before executing it. Syntax issues and other problems may arise, especially when dealing with a significant amount of code. However, SQL Server Management Studio (SSMS) provides a useful feature that allows you to parse your queries and detect syntax errors before execution.

To parse your code in SSMS, you can either paste it into a query window or open the query file. Once you have your code in the query window, you can click the check mark or press Ctrl+F5 to initiate the parsing process. If your code is free of syntax errors, SSMS will display the message “The command(s) completed successfully.”

However, if there are errors in your code, SSMS will indicate the problematic lines. By double-clicking on the error line, SSMS will take you directly to the line of code that needs attention. This feature simplifies the process of identifying and fixing errors in your SQL scripts.

Let’s consider an example to illustrate this process:

--create view
CREATE VIEW vwTest 
AS
SELECT * 
FROM tableDoesNotExist1 t1 
INNER JOIN tableDoesNotExist2 t2 ON t1.id = t2.id
GO

--insert new records
INSERT INTO vwTest VALUES(1)
INSERT INTO vwTest VALUES(2,3)
INSERT INTO vwTest VALUES(3)
INSERT INTO vwTest VALUES(4,5,6)
GO

--create view 2
CREATE VIEW vwTest2
AS
SELECT * 
FROM tableDoesNotExist3 t1 
INNER tableDoesNotExist4 t2 ON t1.id = t2.id
GO

--create proc
CREATE PROC uspTest
AS
SELECT * vwTest2
GO

When we parse this code using SSMS, we encounter two errors. The first error refers to a join issue, while the second error indicates incorrect syntax. By double-clicking on the red error lines, SSMS takes us directly to the problematic lines of code.

After identifying the errors, we can fix the code and parse it again:

--create view
CREATE VIEW vwTest 
AS
SELECT * 
FROM tableDoesNotExist1 t1 
INNER JOIN tableDoesNotExist2 t2 ON t1.id = t2.id
GO

--insert new records
INSERT INTO vwTest VALUES(1)
INSERT INTO vwTest VALUES(2,3)
INSERT INTO vwTest VALUES(3)
INSERT INTO vwTest VALUES(4,5,6)
GO

--create view 2
CREATE VIEW vwTest2
AS
SELECT * 
FROM tableDoesNotExist3 t1 
INNER JOIN tableDoesNotExist4 t2 ON t1.id = t2.id
GO

--create proc
CREATE PROC uspTest
AS
SELECT * FROM vwTest2
GO

After the second parse, there are no errors, indicating that the code is now free of syntax issues. At this point, we can confidently execute the query by pressing F5 or clicking on the execute icon.

However, it’s important to note that the parsing process in SSMS only detects syntax errors and not object issues. Therefore, it’s possible for new errors to surface during execution. To identify these errors, you can follow the same process of double-clicking on the red error messages to navigate to the problematic lines of code.

For example:

This is the first error:
This is the second error:
This is the third error:

By using this parsing and debugging feature in SSMS, you can streamline the process of identifying and fixing errors in your SQL Server code. Although it may not catch all issues, it significantly simplifies the task of locating problematic lines of code compared to manual searching.

It’s worth mentioning that if you edit the source code by inserting or deleting lines, the reference marks in the error messages may no longer work. Therefore, it’s advisable to copy the source code to an editor and make the necessary fixes after identifying the errors in SSMS. However, double-clicking on the error messages will still take you to the problematic lines.

By leveraging the parsing and debugging capabilities of SSMS, you can enhance your productivity and ensure the smooth execution of your SQL Server code.

Article Last Updated: 2021-10-05

Click to rate this post!
[Total: 0 Average: 0]

Let's work together

Send us a message or book free introductory meeting with us using button below.