![]() | ![]() |
Home |
|
|
Reference Manual Volumes 1 - 4 (Online Only) |
|
| Chapter 4 Expressions, Identifiers, and Wildcard Characters |
|
| Identifiers |
Identifiers are names for database objects such as databases, tables, views, columns, indexes, triggers, procedures, defaults, rules, and cursors.
Adaptive Server identifiers can be a maximum of 30 bytes in length, whether single-byte or multibyte characters are used. The first character of an identifier must be either an alphabetic character, as defined in the current character set, or the underscore (_ ) character.
Temporary table names, which begin with the pound sign (#), and local variable names, which begin with the at sign (@), are exceptions to this rule.
Subsequent characters can include letters, numbers, the symbols #, @, _, and currency symbols such as $ (dollars), ¥ (yen), and £ (pound sterling). Identifiers cannot include special characters such as !, %, ^, &, *, and . or embedded spaces.
You cannot use a reserved word, such as a Transact-SQL command, as an identifier. For a complete list of reserved words, see Chapter 5, "Reserved Words."
Tables with names that begin with the pound sign (#) are temporary tables. You cannot create other types of objects with names that begin with the pound sign.
Adaptive Server performs special operations on temporary table names to maintain unique naming on a per-session basis. Long temporary table names are truncated to 13 characters (including the pound sign); short names are padded to 13 characters with underscores (_). A 17-digit numeric suffix that is unique for an Adaptive Server session is appended.
Sensitivity to the case (upper or lower) of identifiers and data depends on the sort order installed on your Adaptive Server. Case sensitivity can be changed for single-byte character sets by reconfiguring Adaptive Server's sort order; see the System Administration Guide for more information. Case is significant in utility program options.
If Adaptive Server is installed with a case-insensitive sort order, you cannot create a table named MYTABLE if a table named MyTable or mytable already exists. Similarly, the following command will return rows from MYTABLE, MyTable, or mytable, or any combination of uppercase and lowercase letters in the name:
select * from MYTABLE
Object names need not be unique in a database. However, column names and index names must be unique within a table, and other object names must be unique for each owner within a database. Database names must be unique on Adaptive Server.
Delimited identifiers are object names enclosed in double quotes. Using delimited identifiers allows you to avoid certain restrictions on object names. Table, view, and column names can be delimited by quotes; other object names cannot.
Delimited identifiers can be reserved words, can begin with non-alphabetic characters, and can include characters that would not otherwise be allowed. They cannot exceed 28 bytes.
Warning!
Delimited identifiers may not be recognized by all front-end applications and should not be used as parameters to system procedures.
Before creating or referencing a delimited identifier, you must execute:
set quoted_identifier on
Each time you use the delimited identifier in a statement, you must enclose it in double quotes. For example:
create table "1one"(col1 char(3)) create table "include spaces" (col1 int)
create table "grant"("add" int)
insert "grant"("add") values (3)While the quoted_identifier option is turned on, do not use double quotes around character or date strings; use single quotes instead. Delimiting these strings with double quotes causes Adaptive Server to treat them as identifiers. For example, to insert a character string into col1 of 1table , use:
insert "1one"(col1) values ('abc')Do not not use:
insert "1one"(col1) values ("abc")To insert a single quote into a column, use two consecutive single quotation marks. For example, to insert the characters "a'b" into col1 use:
insert "1one"(col1) values('a''b')When the quoted_identifier option is set to on, you do not need to use double quotes around an identifier if the syntax of the statement requires that a quoted string contain an identifier. For example:
set quoted_identifier on create table '1one' (c1 int)
However, object_id() requires a string, so you must include the table name in quotes to select the information:
select object_id('1one')----------------------- 896003192
You can include an embedded double quote in a quoted identifier by doubling the quote:
create table "embedded""quote" (c1 int)
However, there is no need to double the quote when the statement syntax requires the object name to be expressed as a string:
select object_id('embedded"quote')You can uniquely identify a table or column by adding other names that qualify it--the database name, owner's name, and (for a column) the table or view name. Each qualifier is separated from the next one by a period. For example:
database.owner.table_name.column_name
database.owner.view_name.column_name
The naming conventions are:
[[database.]owner.]table_name
[[database.]owner.]view_nameUsing delimited identifiers within an object name
If you use set quoted_identifier on, you can use double quotes around individual parts of a qualified object name. Use a separate pair of quotes for each qualifier that requires quotes. For example, use:
database.owner."table_name"."column_name"
Do not use:
database.owner."table_name.column_name"Omitting the owner name
You can omit the intermediate elements in a name and use dots to indicate their positions, as long as the system is given enough information to identify the object:
database..table_name
database..view_nameReferencing your own objects in the current database
You need not use the database name or owner name to reference your own objects in the current database. The default value for owner is the current user, and the default value for database is the current database.
If you reference an object without qualifying it with the database name and owner name, Adaptive Server tries to find the object in the current database among the objects you own.
Referencing objects owned by the database ownerIf you omit the owner name and you do not own an object by that name, Adaptive Server looks for objects of that name owned by the Database Owner. You must qualify objects owned by the Database Owner only if you own an object of the same name, but you want to use the object owned by the Database Owner. However, you must qualify objects owned by other users with the user's name, whether or not you own objects of the same name.
Using qualified identifiers consistentlyWhen qualifying a column name and table name in the same statement, be sure to use the same qualifying expressions for each; they are evaluated as strings and must match; otherwise, an error is returned. Example 2 is incorrect because the syntax style for the column name does not match the syntax style used for the table name.
select demo.mary.publishers.city from demo.mary.publishers
city ----------------------- Boston Washington Berkeley
select demo.mary.publishers.city from demo..publishers
The column prefix "demo.mary.publishers" does not match a table name or alias name used in the query.
Use the system function valid_name, after changing character sets or before creating a table or view, to determine whether the object name is acceptable to Adaptive Server. Here is the syntax:
select valid_name("Object_name")If object_name is not a valid identifier (for example, if it contains illegal characters or is more than 30 bytes long), Adaptive Server returns 0. If object_name is a valid identifier, Adaptive Server returns a nonzero number.
Rename user objects (including user-defined datatypes) with sp_rename.
Warning!
After you rename a table or column, you must redefine all procedures, triggers, and views that depend on the renamed object.
In multibyte character sets, a wider range of characters is available for use in identifiers. For example, on a server with the Japanese language installed, the following types of characters may be used as the first character of an identifier: Zenkaku or Hankaku Katakana, Hiragana, Kanji, Romaji, Greek, Cyrillic, or ASCII.
Although Hankaku Katakana characters are legal in identifiers on Japanese systems, they are not recommended for use in heterogeneous systems. These characters cannot be converted between the EUC-JIS and Shift-JIS character sets.
The same is true for some 8-bit European characters. For example, the OE ligature, is part of the Macintosh character set (codepoint 0xCE). This character does not exist in the ISO 8859-1 (iso_1) character set. If the OE ligature exists in data being converted from the Macintosh to the ISO 8859-1 character set, it causes a conversion error.
If an object identifier contains a character that cannot be converted, the client loses direct access to that object.
|
|