HiveBrain v1.2.0
Get Started
← Back to all entries
debugsqlMinor

Getting all undeclared variables from query

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
allqueryundeclaredgettingvariablesfrom

Problem

Imagine you try to run the following query:

SELECT *
FROM dbo.Customers c
WHERE id = @id
    AND dateTimeCreated = @date


You'll get an error message along the lines of Must declare the scalar variable "@id".

As I understand it, this happens during the initial parsing of the query, before any code is executed.

Is it possible to get SQL Server to report all the missing variables at once, or is this just how the query parser works?

Solution

Error list

The error list might give you what you need:

You could open View --> Error List in ssms

To get all the build + intelliSense errors:

Differences between error list & the messages tab on query execution:

Error list


The Error List contains all syntax and semantic errors found by
IntelliSense in the Database Engine Editor. This list of errors is
dynamically updated as you edit Transact-SQL scripts. The list
includes all errors that the editor has found in each Transact-SQL
script. The editor does not stop parsing a file after encountering
errors in a script. In SQL Server 2008, IntelliSense in the Database
Engine Editor does not support all Transact-SQL syntax elements. The
Error List contains only errors from the Transact-SQL syntax that is
supported by IntelliSense.

Messages Tab


The Messages tab at the bottom of the Database Engine Query Editor
window displays all errors and messages that are returned by the SQL
Server Database Engine when a Transact-SQL script is executed. This
list does not change until you execute the script again. The Database
Engine stops parsing a batch after it finds one or two compile errors;
therefore, the Messages tab might not list all errors in a script.
Source

So to answer the question:


Is it possible to get SQL Server to report all the missing variables
at once, or is this just how the query parser works?

Using general query excecution this is not possible as the parsing stops after one or two compile errors, but you could use the error list.

Context

StackExchange Database Administrators Q#253152, answer score: 4

Revisions (0)

No revisions yet.