| learn the delimited identifiers |
|
|
|
| programming - SQL Server | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Written by amr mazen | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Sunday, 30 November 2008 18:03 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
An identifier that complies with all the rules for the format of identifiers can be used with or without delimiters. An identifier that does not comply with the rules for the format of regular identifiers must always be delimited. Delimited identifiers are used in these situations :
Types of delimiters used in Transact-SQL: Note Delimiters are for identifiers only. Delimiters cannot be used for keywords, whether or not they are marked as reserved in SQL Server.
Quoted identifiers are valid only when the QUOTED_IDENTIFIER option is set to ON. By default, the Microsoft OLE DB Provider for SQL Server and SQL Server ODBC driver set QUOTED_IDENTIFIER ON when they connect. DB-Library does not set QUOTED_IDENTIFIER ON by default. Regardless of the interface used, individual applications or users may change the setting at any time. SQL Server provides a number of ways to specify this option. For example, in SQL Server Enterprise Manager and SQL Query Analyzer, the option can be set in a dialog box. In Transact-SQL, the option can be set at various levels using SET QUOTED_IDENTIFIER, the quoted identifier option of sp_dboption, or the user options option of sp_configure. When QUOTED_IDENTIFIER is ON, SQL Server follows the SQL-92 rules for the use of double quotation marks and the single quotation mark (') in SQL statements:
When QUOTED_IDENTIFIER is OFF, SQL Server follows these rules for the use of single and double quotation marks:
Delimiters in brackets can always be used, regardless of the setting of QUOTED_IDENTIFIER. Rules for Delimited IdentifiersThe rules for the format of delimited identifiers are:
These examples use quoted identifiers for table names and column names. Both methods for specifying delimited identifiers are shown: SET QUOTED_IDENTIFIER ON After the $Employee Data and ^$Employee Data tables are created and data is entered, rows can be retrieved: SET QUOTED_IDENTIFIER ON In this example, a table named table contains columns tablename, user, select, insert, and so on. Because TABLE, SELECT, INSERT, UPDATE, and DELETE are reserved keywords, the identifiers must be delimited each time the objects are accessed. SET QUOTED_IDENTIFIER ON If the SET QUOTED_IDENTIFIER option is not ON, the table and columns cannot be accessed unless bracket delimiters are used. SET QUOTED_IDENTIFIER OFF Here is the result set: Msg 170, Level 15, State 1 Here is the result set (using bracket delimiters): SET QUOTED_IDENTIFIER OFF Delimiting Identifiers with Multiple PartsWhen using qualified object names you may have to delimit more than one of the identifiers that make up the object name. Each identifier must be delimited individually, for example: /* SQL-92 quoted identifier syntax */ -Or- /* Transact-SQL bracketed identifier syntax */ There are some special rules regarding how you delimit multi-part stored procedure names in the ODBC CALL statement. For more information. Using Identifiers as Parameters in SQL ServerMany system stored procedures, functions, and DBCC statements take object names as parameters. Some of these parameters accept multipart object names, while others accept only single-part names. Whether a single-part or multipart name is expected determines how a parameter is parsed and used internally by SQL Server. Single-part Parameter NamesIf the parameter is a single-part identifier, the name can be specified:
For single-part names, the string inside the single quotation marks represents the object name. If delimiters are used inside single quotation marks, the delimiter characters are treated as part of the name. If the name contains a period or another character that is not part of the character set defined for regular identifiers, you must enclose the object name in single quotation marks, double quotation marks, or brackets. Multipart Parameter NamesMultipart names are qualified names that include the database or owner name in addition to the object name. SQL Server requires that when a multipart name is used as a parameter, the entire string that constitutes the multipart name must be enclosed in a set of single quotation marks. EXEC MyProcedure @name = 'dbo.Employees' If individual name parts require delimiters, each part of the name should be delimited separately as required. For example, if a name part contains a period, double quotation mark, or left or right bracket, use brackets or double quotation marks to delimit the part. Enclose the complete name in single quotation marks. For example, the table name, tab.one, contains a period. To prevent the name from being interpreted as a three-part name, dbo.tab.one, delimit the table name part. EXEC sp_help 'dbo.[tab.one]' This example shows the same table name delimited with double quotation marks. SET QUOTED_IDENTIFIER ON This table lists some of the Transact-SQL functions, DBCC statements, and system stored procedures that use multipart names.
Newer news items:
Older news items:
|



