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 name they provide. SQL Server's documentation explains what characters are valid in a database name. However, the documentation is apparently incorrect, because I can successfully create databases whose names violate the documented rules.
According to SQL Server's documentation for CREATE DATABASE, database names must comply with the rules for identifiers; and the rules for identifiers depend on the database compatibility level. When the compatibility level is 100 (which, according to SQL Server Management Studio, means "SQL Server 2008"), the name must start with a Unicode letter, _
, @
, or #
; followed by one or more letters, numbers, @
, $
, #
, or _
. The documentation clearly states that embedded spaces or special characters are not allowed.
This flies in the face of the available evidence, because I can use SQL Server Management Studio to create a database whose name is This & That | "Other"
-- which not only contains embedded spaces (explicitly forbidden), but contains special characters (|
, "
) that aren't even valid in a filename. I checked, and the database's compatibility level is indeed "SQL Server 2008 (100)", even though its name is documented to be invalid at that compatibility level.
Heck, I can even do CREATE DATABASE " "
(yes, that's a single space), which proves that the first character does not have to be a letter, underscore, at sign, or pound sign.
So I guess my question is, what characters are valid in an SQL Server database name? Are there any documented rules that are consistent with SQL Server's actual behavior?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…