0

For example if I create a table like this:

CREATE TABLE FIREARM_BUILD 
(
    [SCOPE] VARCHAR(max)
);

Do the brackets actually result in a column named [SCOPE]?

Edit: I believe my question is not a duplicate because the question in "question" does not explicitly answer what I'm asking.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
secondbreakfast
  • 4,194
  • 5
  • 47
  • 101
  • 1
    Possible duplicate of [What is the use of the square brackets \[\] in sql statements?](https://stackoverflow.com/questions/52898/what-is-the-use-of-the-square-brackets-in-sql-statements) – diiN__________ Dec 06 '17 at 13:58
  • 2
    Interesting that you haven't tried it yourself before asking – Tim Schmelter Dec 06 '17 at 13:58
  • @TimSchmelter Currently only have access to a database I don't have permission to do that. And http://sqlfiddle.com/ isnt working for me for some reason – secondbreakfast Dec 06 '17 at 13:59
  • 1
    @zero01alpha http://rextester.com/l/sql_server_online_compiler will work :) – Ilyes Dec 06 '17 at 14:02
  • 7
    Square brackets are used to enclose field names that would be illegal otherwise like `[my field name]` or `[date]` (one has spaces and the other is a [reserved word](https://learn.microsoft.com/en-us/sql/t-sql/language-elements/reserved-keywords-transact-sql)). Should you include square brackets around a field name like `Scope` they will have no real effect `[scope]` and `scope` are synonymous. If you are feeling evil, you can include square brackets in the actual name of the field by enclosing them in double quotes `"[scope]"`. Don't do that though, because you are not a monster. – JNevill Dec 06 '17 at 14:03
  • @Sami cool thanks, I will check that out! – secondbreakfast Dec 06 '17 at 14:04
  • @JNevill Hahaha, thanks for the advice and for the laugh – secondbreakfast Dec 06 '17 at 14:04
  • 1
    I'm sure it used to be explicitly documented that the quotes or brackets did not form part of the name but I'm blowed if I can find it in either [Database Identifiers](https://learn.microsoft.com/en-us/sql/relational-databases/databases/database-identifiers) or [QUOTED_IDENTIFIER](https://learn.microsoft.com/en-us/sql/t-sql/statements/set-quoted-identifier-transact-sql) – Damien_The_Unbeliever Dec 06 '17 at 14:34
  • 2
    It's documented here: "Delimited identifiers Are enclosed in double quotation marks (") or brackets ([ ]). " The identifier is "enclosed in" the brackets, therefore it doesn't "contain" the brackets. https://learn.microsoft.com/en-us/sql/relational-databases/databases/database-identifiers – David Browne - Microsoft Dec 06 '17 at 14:45

1 Answers1

1

I typically include brackets in field and type name definitions because when you script out an object that's what SSMS does, and it's easier to add a few than change a lot if you're aiming for consistency. If you're building an object name dynamically, such as below, you should probably include brackets because you don't really know that the underlying table/schema name is legal. That's obviously why SSMS does it as well.

declare @object [NVARCHAR](1000) = QUOTENAME(OBJECT_SCHEMA_NAME(@@PROCID)) + N'.' + QUOTENAME(OBJECT_NAME(@@PROCID));

For the procedure [dbo].[Get Monthly Report] this results in a value for @object of N'[dbo].[Get Monthly Report]'. Had you not bracketed the names using QUOTENAME this would result in a value for @object of N'dbo.Get Monthly Report'. In certain scenario, such as dynamic sql, the second would fail.