Using Identifiers

Accessing and Changing Relational Data

Accessing and Changing Relational Data

Using Identifiers

The database object name is known as its identifier. Everything in Microsoft® SQL Server™ can have an identifier. Servers, databases, and database objects such as tables, views, columns, indexes, triggers, procedures, constraints, rules, and so on can have identifiers. Identifiers are required for most objects, but are optional for some objects, such as constraints.

An object identifier is created when the object is defined. The identifier is then used to reference the object. For example, this statement creates a table with the identifier TableX, and two columns with the identifiers KeyCol and Description:

CREATE TABLE TableX
(KeyCol INT PRIMARY KEY, Description NVARCHAR(80))

This table also has an unnamed constraint. The PRIMARY KEY constraint has no identifier.

Classes of Identifiers

There are two classes of identifiers:

Regular identifiers

Conform to the rules for the format of identifiers. Regular identifiers are not delimited when used in Transact-SQL statements.

SELECT *
FROM TableX
WHERE KeyCol = 124

Delimited identifiers

Are enclosed in double quotation marks (") or brackets ([ ]). Identifiers that comply with the rules for the format of identifiers may or may not be delimited.

SELECT *
FROM [TableX]         --Delimiter is optional.
WHERE [KeyCol] = 124  --Delimiter is optional.

Identifiers that do not comply with all of the rules for identifiers must be delimited in a Transact-SQL statement.

SELECT *
FROM [My Table]      --Identifier contains a space and uses a reserved keyword.
WHERE [order] = 10   --Identifier is a reserved keyword.

Both regular and delimited identifiers must contain from 1 through 128 characters. For local temporary tables, the identifier can have a maximum of 116 characters.

Rules for Regular Identifiers

The rules for the format of regular identifiers are dependent on the database compatibility level, which can be set with sp_dbcmptlevel. For more information, see sp_dbcmptlevel. When the compatibility level is 80, the rules are:

  1. The first character must be one of the following:
    • A letter as defined by the Unicode Standard 2.0. The Unicode definition of letters includes Latin characters from a through z and from A through Z, in addition to letter characters from other languages.

    • The underscore (_), "at" sign (@), or number sign (#).

      Certain symbols at the beginning of an identifier have special meaning in SQL Server. An identifier beginning with the "at" sign denotes a local variable or parameter. An identifier beginning with a number sign denotes a temporary table or procedure. An identifier beginning with double number signs (##) denotes a global temporary object.

      Some Transact-SQL functions have names that start with double at signs (@@). To avoid confusion with these functions, it is recommended that you do not use names that start with @@.

  2. Subsequent characters can be:
    • Letters as defined in the Unicode Standard 2.0.

    • Decimal numbers from either Basic Latin or other national scripts.

    • The "at" sign, dollar sign ($), number sign, or underscore.
  3. The identifier must not be a Transact-SQL reserved word. SQL Server reserves both the uppercase and lowercase versions of reserved words.

  4. Embedded spaces or special characters are not allowed.

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

See Also

ALTER TABLE

CREATE DATABASE

CREATE DEFAULT

CREATE PROCEDURE

CREATE RULE

CREATE TABLE

CREATE TRIGGER

CREATE VIEW

DECLARE @local_variable

DELETE

Delimited Identifiers

INSERT

Reserved Keywords

SELECT

UPDATE