It’s been long time since I worked with SQL Server last time. I have been working with Oracle and there are differences for sure. This one is simple.
- 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.
- 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) )
- Regardless of following a naming convention that avoids using reserved words, Microsoft does add new reserved words. Using brackets allows your code to be upgraded to a new SQL Server version, without first needing to edit Microsoft’s newly reserved words out of your client code. That editing can be a significant concern. It may cause your project to be prematurely retired….Brackets can also be useful when you want to Replace All in a script. If your batch contains a variable named @String and a column named [String], you can rename the column to [NewString], without renaming @String to @NewString.