Solved

T-SQL square brackets around a parameter

Posted on 2014-10-10
5
466 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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: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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Create a Calendar table 29 45
SQL Query 9 29
Error creating summetric key on SQL 2012 (Amazon) 7 48
Migrate SQL 2005 DB to SQL 2016 4 32
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.‚Äč
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

751 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