What characters are valid in an SQL Server database name?

后端 未结 4 1833
执笔经年
执笔经年 2020-12-08 09:15

We\'re going to provide our clients with a tool that (among other things) creates a new SQL Server database, and I want to be able to do basic validation on the database nam

4条回答
  •  爱一瞬间的悲伤
    2020-12-08 09:53

    The rules for identifiers state at the end:

    When identifiers are used in Transact-SQL statements, the identifiers that do not comply with these rules must be delimited by double quotation marks or brackets.

    By choosing a database name which does not conform to those rules, you have to enclose it always with double quotation marks or brackets.

    If the rules for regular identifiers are respected, you may use your database name without quotes/brackets.

    The following instructions are ok

    CREATE DATABASE [conformingName]
    CREATE DATABASE conformingName
    CREATE DATABASE [This & That | "Other"]
    

    but not

    CREATE DATABASE This & That | "Other"
    

    EDIT:

    I agree that this is not how one would understand the linked documentation: What does must comply with the rules for identifiers mean if the rules do not apply anymore as soon as the identifier is enclosed? The point about enclosing non conforming identifiers should be part of the rules.

提交回复
热议问题