10. SQL ANSI–Configuration Properties
The SQL ANSI–configuration properties, as shown in Table 9, are used to set ANSI behavior in SQL Server.
Table 9 SQL ANSI–Configuration Properties
The connection default properties (there are
several) affect the environment of batches executed within a
connection. Most of the connection properties change SQL Server
behavior so that it complies with the ANSI standard. Because so few SQL
Server installations modify these properties, it's much safer to modify
them in code at the beginning of a batch than to set them at the server
or database level.
For example, T-SQL requires a begin transaction to start a logical unit of work. Oracle assumes a begin transaction
is at the beginning of every batch. If you prefer to work with implicit
(nonstated) transactions, then you're safer setting the implicit
transaction connection property at the beginning of your batch. For
these reasons, you should leave the connection properties at the
default values and set them in code if needed.
The SQL ANSI-configuration settings are set at three levels: server, database, and connection, as indicated in Table 9.
The sp_configure system stored procedure has the User Options setting
that enables manipulation of serverwide ANSI settings, and it works
across databases. The ALTER DATABASE command can set the default database setting for ANSI. Connection level settings are set with the SET command and override the default database setting.
In Management Studio, the ANSI settings can be enabled (ON) at the following levels:
- Server level: Checks the ANSI setting check box in the Server properties Connections tab .
- Database level: Checks the ANSI setting check box in the Database Properties Options tab .
- Connection level: Click the Query menu ? Query Options ? Execution ? ANSI, and check the ANSI setting check box.
Note
The sp_dboption procedure is no longer supported in this version of SQL Server. To set these options use the ALTER DATABASE command.
You can change the default database
setting for ANSI in the model system database and then the defaults
change for all future databases.
The database setting for ANSI
overwrites the server setting, and the connection setting overwrites
the server and database setting.
ANSI Defaults
SQL Server provides the SET ANSI_DEFAULTS command to manage a group of SQL Server settings. When SET ANSI_DEFAULTS is enabled, it enables the following settings (explained later in this section):
- SET ANSI_NULLS
- SET ANSI_NULL_DFLT_ON
- SET ANSI_PADDING
- SET ANSI_WARNINGS
- SET CURSOR_CLOSE_ON_COMMIT
- SET IMPLICIT_TRANSACTIONS
- SET QUOTED_IDENTIFIER
To set ANSI_DEFAULTS in code, do the following:
SET ANSI_DEFAULTS ON; ANSI Null Default
The ANSI_NULL_DEFAULT setting controls the default nullability. This setting is used when a NULL or NOT_NULL is not explicitly specified when creating a table. The default database setting for ANSI_NULL_DEFAULT is OFF.
To set the ANSI_NULL_DEFAULT option on for the AdventureWorks2012 sample database in code, do the following:
ALTER DATABASE AdventureWorks2012 SET ANSI_NULL_DEFAULT ON;
If the ANSI_NULL_DEFAULT option is not set at the database level, you can also set the nullability of new columns using the SET ANSI_NULL_DFLT_ON and SET ANSI_NULL_DFLT_OFF commands. SET ANSI_NULL_DFLT_ON can enable null values at the connection level:
SET ANSI_NULL_DFLT_ON ON;
SET ANSI_NULL_DFLT_OFF can be set to not enable null values at the connection level:
SET ANSI_NULL_DFLT_OFF ON;
To enable ANSI_NULL_DFLT_ON at the server level in code, do the following:
EXEC sp_configure ‘user options', 1024;
RECONFIGURE;
To enable ANSI_NULL_DFLT_OFF at the server level in code, do the following:
EXEC sp_configure ‘user options', 2048;
RECONFIGURE;
Note
Both SET ANSI_NULL_DFLT_ON and SET ANSI_NULL_DFLT_OFF commands cannot be set to ON at the same time. Either one can be ON and the other can be OFF or both can be OFF.
ANSI NULLs
The ANSI_NULLS connection setting can determine comparison evaluations. When set to ON, all comparisons to a null value evaluate to UNKNOWN. When set to OFF, the comparison to a null value evaluates to true if both values are NULL. The default database setting for ANSI_NULLS is OFF.
To enable ANSI_NULLS in code at the connection level, do the following:
SET ANSI_NULLS ON;
If SET ANSI_NULLS is not specified, then the settings of ANSI_NULLS of the current database apply. To enable ANSI_NULLS for the AdventureWorks2012 sample database in code, do the following:
ALTER DATABASE AdventureWorks2012 SET ANSI_NULLS ON;
To enable ANSI_NULLS at the server level in code, do the following:
EXEC sp_configure ‘user options', 32;
RECONFIGURE;
Note
The ANSI_NULLS option is deprecated and will always be ON in a future version of SQL Server.
ANSI Padding
The ANSI_PADDING connection setting affects only newly created columns. When set to ON,
data stored in char, varchar, binary, and varbinary data types retain
any padded zeros to the left of variable binary numbers, and any padded
spaces to the right or left of variable-length characters. When set to OFF, all leading and trailing blanks and zeros are trimmed. The default database setting for ANSI_PADDING is OFF.
To enable ANSI_PADDING in code at connection level, do the following:
SET ANSI_PADDING ON;
If SET ANSI_PADDING is not specified, then the settings of ANSI_PADDING of the current database applies. To enable ANSI_PADDING for the AdventureWorks2012 sample database in code, do the following:
ALTER DATABASE AdventureWorks2012 SET ANSI_PADDING ON;
To enable ANSI_PADDING at the server level in code, do the following:
EXEC sp_configure ‘user options', 16;
RECONFIGURE;
Note
ANSI_PADDING option is deprecated and will always be ON in a future version of SQL Server.
ANSI Warnings
The ANSI_WARNINGS
connection setting can handle ANSI errors and warnings such as
arithmetic overflow, divide-by-zero, and null values appearing in
aggregate functions. The default database setting for ANSI_WARNINGS is OFF. When this setting is OFF,
no warnings are raised when null values appear in aggregate functions,
and null values are returned when divide-by-zero occurs and overflow
errors occur. When the setting is ON, query is aborted and errors are raised when arithmetic overflow errors and divide-by-zero occur.
To set ANSI_WARNINGS in code at connection level, do the following:
SET ANSI_WARNINGS ON;
If SET ANSI_WARNINGS is not specified, then the settings of ANSI_WARNINGS of the current database apply. To enable ANSI_WARNINGS for the AdventureWorks2012 sample database in code, do the following:
ALTER DATABASE AdventureWorks2012 SET ANSI_WARNINGS ON;
To enable ANSI_WARNINGS at the server level in code, do the following:
EXEC sp_configure ‘user options', 8;
RECONFIGURE;
Arithmetic Abort
The ARITHABORT
connection setting can handle query termination if an arithmetic error
such as data overflow or divide-by-zero occurs. The default database
setting for ARITHABORT is OFF.
What exactly is terminated also depends on the ANSI_WARNINGS setting. Table 10 explains the behavior based on the values of ANSI_WARNINGS and ARITHABORT.
Table 10 ANSI_WARNINGS and ARITHABORT Behavior
ON |
ON |
Query is aborted. |
ON |
OFF |
Batch is aborted or transaction is rolled back. |
OFF |
ON |
Query is aborted. |
OFF |
OFF |
No warning is raised and null is returned. |
To set ARITHABORT in code at connection level, do the following:
SET ARITHABORT ON;
If ARITHABORT is not specified, then the settings of the current database apply. To enable ARITHABORT for the AdventureWorks2012 sample database in code, do the following:
ALTER DATABASE AdventureWorks2012 SET ARITHABORT ON;
To enable ARITHABORT at the server level in code, do the following:
EXEC sp_configure ‘user options', 64;
RECONFIGURE;
Arithmetic Ignore
The ARITHIGNORE
connection setting controls whether an error message is returned from
arithmetic overflow or divide-by-zero errors. To abort the query, you
need to use the ARITHABORT setting. Both ARITHABORT and ARITHIGNORE can be set to ON but ARITHABORT takes precedence over ARITHIGNORE. To set ARITHIGNORE in code, do the following:
SET ARITHIGNORE ON;
To enable ARITHIGNORE at the server level in code, do the following:
EXEC sp_configure ‘user options', 128;
RECONFIGURE;
Numeric Round Abort
The NUMERIC_ROUNDABORT connection setting controls the behavior of numeric decimal-precision-rounding errors in process. When NUMERIC_ROUNDABORT is set to ON and ARITHABORT is set to ON,
an error is generated, and no result is returned if the numeric-decimal
precision is lost in an expression value. Loss of numeric-decimal
precision can occur when a value with fixed precision is stored in a
column or variable with less precision. If ARITHABORT is set to OFF and NUMERIC_ROUNDABORT is set to ON, warning is returned and null is returned. When NUMERIC_ROUNDABORT is set to OFF,
the process proceeds without errors or warnings, and the result is
rounded down to the precision of the object in which the number is
stored. The default database setting for NUMERIC_ROUNDABORT is OFF.
To set NUMERIC_ROUNDABORT in code at the connection level, do the following:
SET NUMERIC_ROUNDABORT ON;
If NUMERIC_ROUNDABORT is not specified, then the settings of the current database apply. To enable NUMERIC_ROUNDABORT for the AdventureWorks2012 sample database in code, do the following:
ALTER DATABASE AdventureWorks2012 SET NUMERIC_ROUNDABORT ON;
To enable NUMERIC_ROUNDABORT at the server level in code, do the following:
EXEC sp_configure ‘user options', 8192;
RECONFIGURE;
Concatenation Null Yields Null
The CONCAT_NULL_YIELDS_NULL setting controls the behavior of the result when concatenating a string with a null. When set to ON, any string concatenated with a null results in a null. When set to OFF, any string concatenated with a null results in the original string, ignoring the null. The default database setting for CONCAT_NULL_YIELDS_NULL is OFF.
To set CONCAT_NULL_YIELDS_NULL in code at the connection level, do the following:
SET CONCAT_NULL_YIELDS_NULL ON;
If CONCAT_NULL_YIELDS_NULL is not specified, then the settings of the current database apply. To enable CONCAT_NULL_YIELDS_NULL for the AdventureWorks2012 sample database in code, do the following:
ALTER DATABASE AdventureWorks2012 SET CONCAT_NULL_YIELDS_NULL ON;
To enable CONCAT_NULL_YIELDS_NULL at the server level in code, do the following:
EXEC sp_configure ‘user options', 4096;
RECONFIGURE;
Use Quoted Identifier
The QUOTED_IDENTIFIER setting enables you to refer to an identifier, such as a column name, by enclosing it within double quotes. When set to ON, identifiers can be delimited by double quotation marks. When set to OFF, identifiers cannot be placed in quotation marks and must not be keywords. The default database setting for QUOTED_IDENTIFIER is OFF. To change the value to ON, use the following code.
To set QUOTED_IDENTIFIER in code at the connection level, do the following:
SET QUOTED_IDENTIFIER ON;
If QUOTED_IDENTIFIER is not specified, then the settings of the current database apply. To enable QUOTED_IDENTIFIER for the AdventureWorks2012 sample database in code, do the following:
ALTER DATABASE AdventureWorks2012 SET QUOTED_IDENTIFIER ON;
To enable QUOTED_IDENTIFIER at the server level in code, do the following:
EXEC sp_configure ‘user options', 256;
RECONFIGURE;
Note
When dealing with indexes on computed columns and indexed views, four of these defaults (ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS, and QUOTED_IDENTIFIER) must be set to ON.