Solved

T-SQL square brackets around a parameter

Posted on 2014-10-10
5
430 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: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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

828 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