Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « SQL Server 2012 Business Intelligence Whitepaper and Data Tools releasedStandardize Table Aliases »
    comments

    Sometimes you get some dynamic SQL handed to you which is 2 pages long with all kind of parameters. Of course you yourself would never write such a monstrosity, but how can you verify that all parameters in the code have been declared? Let's say you have the following piece of dynamic SQL

    1. DECLARE @DynamicSql nvarchar(1000)
    2. SELECT @DynamicSql = N'DECLARE @Type nchar(3) = ''P'' SELECT *
    3. FROM master..spt_values where type =@Type'
    4.  
    5. exec( @DynamicSql )

    That will execute just fine. What if you forgot the DECLARE @Type nchar(3) = ''P'' part?

    1. DECLARE @DynamicSql nvarchar(1000)
    2. SELECT @DynamicSql = N'SELECT *
    3. FROM master..spt_values where type =@Type'
    4.  
    5. exec( @DynamicSql )

    Here is the error.
    Msg 137, Level 15, State 2, Line 2
    Must declare the scalar variable "@Type".

    What if there was something that could help you find these kind of problems? There is, in SQL Server 2012 we now have the sp_describe_undeclared_parameters stored procedure

    Here is how Books On Line describes the sp_describe_undeclared_parameters stored procedure

    Returns a result set that contains metadata about undeclared parameters in a Transact-SQL batch. Considers each parameter that is used in the @tsql batch, but not declared in @params. A result set is returned that contains one row for each such parameter, with the deduced type information for that parameter. The procedure returns an empty result set if the @tsql input batch has no parameters except those declared in @params.

    Here is how you can easily test the code. Change exec( @DynamicSql ) to exec sp_describe_undeclared_parameters @DynamicSql

    1. DECLARE @DynamicSql nvarchar(1000)
    2. SELECT @DynamicSql = N'SELECT *
    3. FROM master..spt_values where type =@Type'
    4.  
    5. EXEC sp_describe_undeclared_parameters  @DynamicSql

    Here is what you see

    There are more columns in the output, feel free to run this yourself to see all of them. Basically it will give you type, precision, length parameter name and more

    How many times have you done something like the following

    1. EXEC sp_executesql @tsql =
    2. N'SELECT id, name, xtype
    3. FROM sys.sysobjects
    4. WHERE id = @id OR NAME = @name',
    5. @params = N'@id int',@id = 1,@name = 'sysobjects'

    Running that will give you the following error

    Msg 137, Level 15, State 2, Line 3
    Must declare the scalar variable "@name".

    Here is how to quickly test that as well, take the code from above and grab everything except for this part ,@id = 1,@name = 'sysobjects'

    Basically, you now have this

    1. exec sp_executesql @tsql =
    2. N'SELECT id, name, xtype
    3. FROM sys.sysobjects
    4. WHERE id = @id OR NAME = @name',
    5. @params = N'@id int'

    Now change sp_executesql to sp_describe_undeclared_parameters

    Run that

    1. exec sp_describe_undeclared_parameters @tsql =
    2. N'SELECT id, name, xtype
    3. FROM sys.sysobjects
    4. WHERE id = @id OR NAME = @name',
    5. @params = N'@id int'

    Here is what you get

    parameter_ordinal name	suggested_system_type_id suggested_system_type_name	
    1	          @name	231	                 nvarchar(128)	

    Again there are more columns so feel free to run this yourself. So far I showed you examples with just one parameter, the stored procedure will return all undeclared parameters

    Here is what it looks like with 4 undeclared parameters

    1. DECLARE @DynamicSql nvarchar(1000)
    2. SELECT @DynamicSql = N'SELECT *
    3. FROM master..spt_values where type =@Type
    4. AND Name =@Name
    5. And number =@number
    6. and high = @high'
    7.  
    8. EXEC sp_describe_undeclared_parameters  @DynamicSql

    Here is the output

    parameter_ordinal name suggested_system_type_id	suggested_system_type_name suggested_max_length
    1	         @Type	239	                nchar(3)	           6
    2	         @Name	231	                nvarchar(35)	          70
    3	        @number	56	                int	                   4
    4	          @high	56	                int	                   4

    As you can see the sp_describe_undeclared_parameters stored procedure can come in handy to check if dynamic SQL has undeclared parameters

    About the Author

    User bio imageDenis has been working with SQL Server since version 6.5. Although he worked as an ASP/JSP/ColdFusion developer before the dot com bust, he has been working exclusively as a database developer/architect since 2002. In addition to English, Denis is also fluent in Croatian and Dutch, but he can curse in many other languages and dialects (just ask the SQL optimizer) He lives in Princeton, NJ with his wife and three kids.
    Social SitingsTwitterFacebookLinkedInHomePageFlickrLTD RSS Feed
    2812 views
    InstapaperVote on HN

    2 comments

    Comment from: datengarten [Member] Email
    datengarten I suppose the sp_describe_undeclared_parameters is only available in sql server 2012?
    03/06/13 @ 01:50
    Comment from: SQLDenis [Member] Email
    SQLDenis Correct SQL Server 2012 only
    03/06/13 @ 02:08

    Leave a comment


    Your email address will not be revealed on this site.

    To mislead the spambots.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)