Using Identifiers

The name of a database object 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. Most objects are required to have an identifier; however, identifiers are optional for some objects, such as constraints.

An object's identifier is created when the object is defined. You then use the identifier 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 they are used in Transact-SQL statements.

SELECT *
FROM TableX
WHERE KeyCol = 124

  

Delimited identifiers
Are enclosed either 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.
WHERE [order] = 10   --Identifier is a reserved keyword.

  

Both regular and delimited identifiers must contain from 1 to 128 characters. For local temporary tables, the identifier cannot exceed 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 70, the rules are:

  1. The first character must be one of the following:
  2. Subsequent characters can be:
  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.

Any identifier that does not comply with all these rules must always be delimited by double quotation marks or brackets when used in Transact-SQL statements.

See Also
ALTER TABLE CREATE VIEW
CREATE DATABASE DECLARE @local_variable
CREATE DEFAULT DELETE
CREATE PROCEDURE Delimited Identifiers
CREATE RULE INSERT
CREATE TABLE SELECT
CREATE TRIGGER UPDATE

  


(c) 1988-98 Microsoft Corporation. All Rights Reserved.