What is the use of the square brackets [] in sql statements?
I've noticed that Visual Studio 2008 is placing square brackets around column names in sql. Do the brackets offer any advantage? When I hand code T-SQL I've never bothered with them.
Example:
Visual Studio:
SELECT [column1], [column2] etc...
My own way:
SELECT column1, column2 etc...
Solution 1:
The brackets are required if you use keywords or special chars in the column names or identifiers. You could name a column [First Name]
(with a space) – but then you'd need to use brackets every time you referred to that column.
The newer tools add them everywhere just in case or for consistency.
Solution 2:
They're handy if your columns have the same names as SQL keywords, or have spaces in them.
Example:
create table test ( id int, user varchar(20) )
Oh no! Incorrect syntax near the keyword 'user'. But this:
create table test ( id int, [user] varchar(20) )
Works fine.