Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

T-SQL square brackets around a parameter

Posted on 2014-10-10
5
Medium Priority
?
633 Views
Last Modified: 2014-10-10
I just had an SSRS developer ask me to put [ ] around a parameter eg [@ParameterName] in every WHERE clause in my stored procedure. I am curious to know why. The name is probably the same as a column in one of the tables but it really is something generic that is found in a lot of other sp's. So I am not sure why.

Thanks
0
Comment
Question by:phil916
5 Comments
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40372998
Sometimes they are optional, and people like to do it for style.

Sometimes they are not (for instance, if it is a reserved word or has a space in it), and they are therefore needed.

Either way, there's no harm in it. Some people like them; some don't.
0
 
LVL 15

Expert Comment

by:Haris Djulic
ID: 40373006
When using reserved names you must put the brackets around it in order for the SQL server to accept it..

select [select] from table_name..

Although you can use it, I strongly suggest you not to use it since it can cause unwanted results...

Here you can find complete list of reserved names : http://msdn.microsoft.com/en-us/library/ms189822%28v=sql.105%29.aspx
0
 

Author Comment

by:phil916
ID: 40373027
OK it is syntactically correct but I hope you caught that it was a parameter or variable beginning with an @. As long as the variable is still in scope you cannot have a duplicate so it should be unambiguous. It is also most definitely not a keyword as it is industry specific.

I don't like folks who tell me to make changes and not explain why or why it is a problem for them. At the very least I could learn something and avoid the problem in the future.
0
 
LVL 24

Accepted Solution

by:
Phillip Burton earned 2000 total points
ID: 40373043
Part of it is because brackets are often used in SSRS than in SSMS. It is the default used by the Wizards - see http://msdn.microsoft.com/en-GB/library/dd220516.aspx as examples of what the wizards do.
0
 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 40373407
Adding the brackets prevents potential errors later.  Thus, it makes perfect sense.  Who knows what names will be used in the future, or even what new names might become "reserved" that are not today.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

971 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question