开发者

Why does SQL Server Management Studio generate code using square brackets?

When generating code/scripts,

  1. why does SQL Server Management Studio generate code using square brackets and not double-quotes?

    SELECT [NAME] FROM [TABLE]

  2. and is there a way (setting/registry entry) to configure it to use double-quotes (the standard) instead?

    SELECT "NAME" FROM "TABLE"

This is very MSFT-ty feature, given that all their documentation now indica开发者_Go百科te the double-quotes (see this)


why does sql-server (management studio) generate code using square brackets?

SELECT [NAME] FROM [TABLE]

To cope with the names that are reserved words or contain whitespaces.

Square brackets are the native way to wrap the identifiers to. They are asymmetrical and can be unmatched, while the quotes are symmetrical and should be matched (or doubled to include them into the name):

CREATE TABLE [[test] (id INT)
CREATE TABLE ["test] (id INT)
DROP TABLE "[test"
DROP TABLE """test"


Your link explains why, at least to me. The brackets are not setting-dependant, but the validity of " depends on the QUOTED_IDENTIFIER setting.


To answer the other half of your question, there is a workaround in SQL Server Management Studio with the Replace All command (Ctrl+H):

  • Find what: [\[\]]
  • Replace with: "
  • Check "Use:" and select "Regular expressions"
  • Press "Replace All"

Have in mind that this will also replace occurrences of brackets in your comment.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜