This is a tip on how to show line numbers in the SQL Server Management Studio (SSMS) query window.
Showing line numbers in SSMS is something that you may prefer to have enabled as default. As well as general preference, enabling this is particularly useful when debugging an error message in a SQL script.
To enable line numbers, click Tools at the top menu bar and select Options.
Navigate to Text Editor > Transact-SQL > General and tick Line Numbers.
Click OK, and you’ll see now see line numbers in the SSMS query window.
When you are debugging a SQL script within SSMS, showing line numbers might help you when encountering an error like this one below.
I’ve opened a random Stored Procedure, amended and attempted to run it, knowing it will fail.
The line our code has failed on is included in the output error message:
“Msg 102, Level 15, State 1, Procedure sp_addlogin, Line 33 [Batch Start Line 7]
Incorrect syntax near ‘gif’.
Msg 156, Level 15, State 1, Procedure sp_addlogin, Line 35 [Batch Start Line 7]
Incorrect syntax near the keyword ‘else’.“
Line numbers shown in these error messages aren’t always accurate, but it should give you a pointer to the section of broken code.
You can Double-Click the error message to bring you to the referenced line number, or use the Go To Line feature (CTRL + G).