sammarcow
sammarcow

Reputation: 2956

sql server management studio syntax

Can some one explain the significant of the brackets that MSManagement Studio always encapsulates columns in queries with. Ex..

SELECT TOP 1000 [id]
  ,[app_fn]
  ,[app_mn]
  ,[app_ln]
  ,[app_suffix]

What are they there for, and how can I use this to my advantage?

Upvotes: 3

Views: 1678

Answers (3)

gbn
gbn

Reputation: 432261

They allow to use identifiers that

  • have spaces
  • are reserved keywords
  • start with a number
  • use punctuation
  • otherwise invalid

Try this without the [ and ]

CREATE TABLE dbo.[CREATE TABLE] (
    [SELECT] int NOT NULL, 
    [int] varchar(20) NOT NULL,
    [NOT NULL] datetime NULL,
    [Mary's Lamb] datetime NULL,
    [666 The number of the "beast"!] datetime NULL
    )

In this case, SSMS just always uses them

Upvotes: 5

BugFinder
BugFinder

Reputation: 17858

Heres an example

I have a table called groups, it has fields user, group

well, user and group are both reserved words, by automatically putting the square brackets around table, fields etc no problems will arise.

so

select group from groups group by user

would fail

select [group] from [groups] group by [user]

wont.

Upvotes: 4

Neil Knight
Neil Knight

Reputation: 48547

They are normally used if you are using a keyword as a column name. (Naughty)

Also, if you have a space in your column name (also naughty).

Upvotes: 7

Related Questions