Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

T-SQL square brackets around a parameter

Posted on 2014-10-10
5
Medium Priority
?
565 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 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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
When trying to connect from SSMS v17.x to a SQL Server Integration Services 2016 instance or previous version, you get the error “Connecting to the Integration Services service on the computer failed with the following error: 'The specified service …
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

722 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