Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Brackets in SQL statements

Status
Not open for further replies.

hapax

Programmer
Joined
Nov 10, 2006
Messages
105
Location
US
I've seen brackets used around column names, etc. in SQL statements. For example in the following stored proc:

CREATE PROCEDURE [dbo].[GetProducts] (
@CategoryID int
) AS
SELECT [ProductID], [ProductName]
FROM [Products]
WHERE CategoryID = @CategoryID


What are these brackets for, and when should they be used?
 
They identify their contents as a column name. They are usually only needed if you have a reserved keyword as a column name, or a column name with a space.

Hope this helps,

Alex

[small]----signature below----[/small]
Numbers is hardly real and they never have feelings but you push too hard, even numbers got limits
Why did one straw break the camel's back? Here's the secret: the million other straws underneath it

My Crummy Web Page
 
>> What are these brackets for, and when should they be used?

In my opinion, the brackets make the code harder to read. Their purpose is to identify objects within the database. Anything within the brackets should be an object name. Obviously, the brackets are optional.

Brackets are necessary when you have an object name that is the same as a reserved word. When I say object, you should think: Database Name, Table Name, Column Name, Stored Procedure Name, FunctionName, etc...

For example, suppose you want to name a column Select...

Create Table Table(Select Int)

[tt][red]Server: Msg 156, Level 15, State 1, Line 1[/red]
Incorrect syntax near the keyword 'Select'.[/tt]

You can use the brackets to get around this error.

Code:
Create Table [Table]([Select] Int)

Select [Select] From [Table]


When should they be used? In my opinion... Never. As I already explained, they MUST be used when you have an object name that is a reserved word. Simply put, you should NOT name your objects the same as reserved words. If you follow this advice, then you will never need to use brackets.

Make sense?

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Create a database with a space in the name - for example Test Database. Now try to back it up. Some backup processes won't work with that. LiteSpeed is one. But put brackets around it [Test Database] and it will work. Of course, it's poor design to use spaces in database names or any other object name. But sometimes it happens. Just like sometimes a column is called Name, which is a reserved word and needs square brackets.

-SQLBill

The following is part of my signature block and is only intended to be informational.
Posting advice: FAQ481-4875
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top