Solved

T-SQL square brackets around a parameter

Posted on 2014-10-10
5
372 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 500 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 69

Expert Comment

by:ScottPletcher
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how the fundamental information of how to create a table.

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now