Nielsen c39.tex V4 - 07/21/2009 2:17pm Page 922 Part VI Enterprise Data Management TABLE 39-8 Cursor-Configuration Properties Property Level* Graphic Control Code Option Cursor Threshold S Management Studio EXEC sp_configure ‘cursor threshold’ Cursor Close on Commit SDC Management Studio ALTER DATABASE <DB Name> SET cursor_close_on_commit Cursor Default D Management Studio ALTER DATABASE <DB Name> SET cursor_default * The configuration level refers to Server, Database, or Connection. For information about cursor concepts, writing, and avoiding cursors, refer to Chapter 22, ‘‘Kill the Cursor!’’ Cursor threshold The cursor threshold property sets the number of rows in a cursor set before the cursor keysets are generated asynchronously. The Query Optimizer estimates the number of rows that will be returned from the result set. If the estimated number of rows is greater than the cursor threshold, then the cursor is generated asynchronously; otherwise, it is generated synchronously, causing a delay because the query has to wait until all the rows are fetched. Every cursor keyset will be generated asynchronously if the cursor threshold property is set to 0. The default of -1 causes all keysets to be generated synchronously, which is OK for smaller keysets. For larger cursor keysets, though, this may be a problem. In Management Studio, the cursor threshold option can be set to the desired value in the ‘‘Cursor threshold’’ box in the Server Properties Advanced tab (refer to Figure 39-9). When you are working with cursors, the following code will permit synchronous cursor keysets for cur- sors of up to 10,000 rows: EXEC sp_configure ‘show advanced options’, 1; RECONFIGURE; EXEC sp_configure ‘cursor threshold’, 10000; RECONFIGURE; Cursor close on commit This property will close an open cursor after a transaction is committed when set to ON.Ifitissetto OFF (the default), then cursors remain open across transactions until a close cursor statement is issued. 922 www.getcoolebook.com Nielsen c39.tex V4 - 07/21/2009 2:17pm Page 923 Configuring SQL Server 39 The CURSOR_CLOSE_ON_COMMIT option can be set from Management Studio and code at the server, database, and connection level. In Management Studio, the CURSOR_CLOSE_ON_COMMIT option can be turned on at the various levels as follows: ■ Server level: Check the ‘‘cursor close on commit’’ checkbox in the Server properties Connec- tions tab (refer to Figure 39-8). ■ Database level: Select True for the ‘‘cursor close on commit enabled’’ box in the Database Properties Options tab (refer to Figure 39-2). ■ Connection level: To set this property for current queries, click the Query menu ➪ Query Options ➪ Execution ➪ ANSI and check the SET CURSOR_CLOSE_ON_COMMIT check box. To set this property for all future connections, click the Tools menu ➪ Options ➪ Query Execution ➪ ANSI and check the SET CURSOR_CLOSE_ON_COMMIT check box. To set CURSOR_CLOSE_ON_COMMIT in code, do the following: ■ Server level EXEC sp_configure ‘user options’, 4; RECONFIGURE; ■ Database level (example to set the option on for the AdventureWorks2008 sample database) ALTER DATABASE AdventureWorks2008 SET CURSOR_CLOSE_ON_COMMIT ON; ■ Connection level SET CURSOR_CLOSE_ON_COMMIT ON; Cursor default This property will make each cursor local to the object that declared it when set to local.Whenitis set to global (the default), the cursor’s scope can be extended outside the object that created it. In Management Studio, the CURSOR_DEFAULT option can be set to the desired scope in the ‘‘Default cursor’’ box in the Database Properties Options tab (refer to Figure 39-2).To set the cursor default for the AdventureWorks2008 sample database to LOCAL in code, do the following: ALTER DATABASE AdventureWorks2008 SET CURSOR_DEFAULT LOCAL; SQL ANSI–configuration properties The SQL ANSI–configuration properties, shown in Table 39-9, are used to set ANSI behavior in SQL Server. 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 mod- ify them in code at the beginning of a batch than to set them at the server or database level. 923 www.getcoolebook.com Nielsen c39.tex V4 - 07/21/2009 2:17pm Page 924 Part VI Enterprise Data Management TABLE 39-9 SQL ANSI–Configuration Properties Property Level* Graphic Control Code Option ANSI Defaults C Management Studio SET ANSI_DEFAULTS ANSI Null Behavior SDC Management Studio ALTER DATABASE <DB Name> SET ANSI_NULL_DFLT_OFF SET ANSI_NULL_DFLT_ON ANSI Nulls SDC Management Studio ALTER DATABASE <DB Name> SET ANSI_NULLS ANSI Padding SDC Management Studio ALTER DATABASE <DB Name> SET ANSI_PADDING ANSI Warnings SDC Management Studio ALTER DATABASE <DB Name> SET ANSI_WARNINGS Arithmetic Abort SDC Management Studio ALTER DATABASE <DB Name> SET arithabort Arithmetic Ignore SC - SET ARITHIGNORE Numeric Round Abort SDC Management Studio ALTER DATABASE <DB Name> SET NUMERIC_ROUNDABORT{ON | OFF} Null Concatenation SDC Management Studio ALTER DATABASE <DB Name> SET CONCAT_NULL_YIELDS_NULL Use Quoted Identifier SD Management Studio ALTER DATABASE <DB Name> SET QUOTED_IDENTIFIER * The configuration level refers to Server, Database, or Connection. 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 (non- stated) transactions, then you’re safer setting the implicit transaction connection property at the begin- ning of your batch. For these reasons, I recommend leaving the connection properties at the default val- ues and setting them in code if needed. The SQL ANSI-configuration settings are set at three levels: server, database and connection, as indicated in Table 39-9. The sp_configure system stored procedure has the ‘‘user options’’ setting that allows manipulation of server-wide ANSI settings and it works across databases. The ALTER DATABASE com- mand can be used to set the default database setting for ANSI. Connection-level settings are performed with the SET command and they override the default database setting. In Management Studio, the ANSI settings can be enabled ( ON) at the three levels as follows: ■ Server level: In the Server properties Connections tab (refer to Figure 39-8), check the boxes for the ANSI settings that you want to enable. ■ Database level: In the Database Properties Options tab (refer to Figure 39-2 above), enable the ANSI settings. 924 www.getcoolebook.com Nielsen c39.tex V4 - 07/21/2009 2:17pm Page 925 Configuring SQL Server 39 ■ Connection level: Click the Query menu ➪ Query Options ➪ Execution ➪ ANSI, and then check the boxes for the ANSI settings that you want to enable. For backward compatibility, the sp_dboption stored procedure is also available, but using this procedure is not recommended because it will be removed in future versions of SQL Server. You can change the default ANSI database settings in model system database and then the defaults will be changed 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 provides 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 to ON for the AdventureWorks2008 sample database in code, do the following: ALTER DATABASE AdventureWorks2008 SET ANSI_NULL_DEFAULT ON; If the ANSI_NULL_DEFAULT option is not set at the database level, you can 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 be enabled to allow null values at the connection level: SET ANSI_NULL_DFLT_ON ON; SET ANSI_NULL_DFLT_OFF can be enabled to not allow null values at the connection level: SET ANSI_NULL_DFLT_OFF ON; 925 www.getcoolebook.com Nielsen c39.tex V4 - 07/21/2009 2:17pm Page 926 Part VI Enterprise Data Management To enable ANSI_NULL_DFLT_ON at the server level in code, do the following: EXEC sp_configure ‘user options’, 1024; RECONFIGURE; The following enables ANSI_NULL_DFLT_OFF at the server level: EXEC sp_configure ‘user options’, 2048; RECONFIGURE; You cannot set both the SET ANSI_NULL_DFLT_ON and the SET ANSI_NULL_DFLT_OFF com- mands 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 is used to determine comparison evaluations. When set to ON,all comparisons to a null value will evaluate to UNKNOWN.WhensettoOFF, the comparison to a null value will evaluate to true if both values are NULL. The default database setting for ANSI_NULLS is OFF. The following enables ANSI_NULLS at the connection level: 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 AdventureWorks2008 sample database in code, do the following: ALTER DATABASE AdventureWorks2008 SET ANSI_NULLS ON; The following enables ANSI_NULLS at the server level: EXEC sp_configure ‘user options’, 32; RECONFIGURE; 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 o nly newly created columns. When set to ON,data stored in char, varchar, binary,andvarbinary data types will 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. The following enables ANSI_PADDING in code at the connection level: SET ANSI_PADDING ON; If SET ANSI_PADDING is not specified, then the settings of ANSI_PADDING of the current database apply. The following enables ANSI_PADDING for the AdventureWorks2008 sample database: ALTER DATABASE AdventureWorks2008 SET ANSI_PADDING ON; 926 www.getcoolebook.com Nielsen c39.tex V4 - 07/21/2009 2:17pm Page 927 Configuring SQL Server 39 This enables ANSI_PADDING at the server level in code: EXEC sp_configure ‘user options’, 16; RECONFIGURE; The ANSI_PADDING option is deprecated and will always be ON in a future version of SQL Server . ANSI warnings The ANSI_WARNINGS connection setting is used to 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, the query is aborted and errors are raised when arithmetic overflow errors and divide-by-zero occurs. To set ANSI_WARNINGS in code at the connection level: SET ANSI_WARNINGS ON; If SET ANSI_WARNINGS is not specified, then the settings of ANSI_WARNINGS of the current database apply. The following enables ANSI_WARNINGS for the AdventureWorks2008 sample database: ALTER DATABASE AdventureWorks2008 SET ANSI_WARNINGS ON; Use the following to enable ANSI_WARNINGS at the server level in code: EXEC sp_configure ‘user options’, 8; RECONFIGURE; Arithmetic abort The ARITHABORT connection setting is used to handle query termination if arithmetic errors 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 39-10 explains the behavior based on the values of ANSI_WARNINGS and ARITHABORT. To set ARITHABORT in code at the connection level: SET ARITHABORT ON; If ARITHABORT is not specified, then the settings of the current database apply. To enable ARITHABORT for AdventureWorks2008 sample database in code, do the following: ALTER DATABASE AdventureWorks2008 SET ARITHABORT ON; The following enables ARITHABORT at the server level: EXEC sp_configure ‘user options’, 64; RECONFIGURE; 927 www.getcoolebook.com Nielsen c39.tex V4 - 07/21/2009 2:17pm Page 928 Part VI Enterprise Data Management TABLE 39-10 ANSI_WARNINGS and ARITHABORT Behavior ARITHABORT ANSI_WARNINGS 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 Arithmetic ignore The ARITHIGNORE connection setting is used to control 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.TosetARITHIGNORE in code, use the following: SET ARITHIGNORE ON; The following enables ARITHIGNORE at the server level: EXEC sp_configure ‘user options’, 128; RECONFIGURE; Numeric round abort The NUMERIC_ROUNDABORT connection setting is used to control 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 c olumn or variable with less precision. If ARITHABORT is set to OFF and NUMERIC_ROUNDABORT is set to ON, a warning appears and null is returned. When NUMERIC_ROUNDABORT is set to OFF, the process will proceed without errors or warn- ings, and the result is rounded down to the precision of the object in which the number is being stored. The default database setting for NUMERIC_ROUNDABORT is OFF. To set NUMERIC_ROUNDABORT in code at the connection level: SET NUMERIC_ROUNDABORT ON; If NUMERIC_ROUNDABORT is not specified, then the settings of the current database apply. The follow- ing enables NUMERIC_ROUNDABORT for the AdventureWorks2008 sample database: ALTER DATABASE AdventureWorks2008 SET NUMERIC_ROUNDABORT ON; To enable NUMERIC_ROUNDABORT at the server level in code, do the following: EXEC sp_configure ‘user options’, 8192; RECONFIGURE; 928 www.getcoolebook.com Nielsen c39.tex V4 - 07/21/2009 2:17pm Page 929 Configuring SQL Server 39 Concatenation null yields null The CONCAT_NULL_YIELDS_NULL setting is used to control the behavior of the r esult when con- catenating a string with a null.WhensettoON, any string concatenated with a null will result in a null.WhensettoOFF, any string concatenated with a null will result in the original string, ignoring the null. The default database setting for CONCAT_NULL_YIELDS_NULL is OFF. The following sets CONCAT_NULL_YIELDS_NULL at the connection level: SET CONCAT_NULL_YIELDS_NULL ON; If CONCAT_NULL_YIELDS_NULL is not specified, then the settings of the current database apply. The following enables CONCAT_NULL_YIELDS_NULL for the AdventureWorks2008 sample database: ALTER DATABASE AdventureWorks2008 SET CONCAT_NULL_YIELDS_NULL ON; The following enables CONCAT_NULL_YIELDS_NULL at the server level: 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. This sets QUOTED_IDENTIFIER to ON at the connection l evel: SET QUOTED_IDENTIFIER ON; If QUOTED_IDENTIFIER is not specified, then the settings of the current database apply. To enable QUOTED_IDENTIFIER for the AdventureWorks2008 sample database, use the following: ALTER DATABASE AdventureWorks2008 SET QUOTED_IDENTIFIER ON; This enables QUOTED_IDENTIFIER at the server level: EXEC sp_configure ‘user options’, 256; RECONFIGURE; When dealing with indexes on computed columns and indexed views, four of these defaults ( ANSI_NULLS, ANSI_PADDING, ANSI_WARNINGS,andQUOTED_IDENTIFIER)mustbe set to ON. Trigger configuration properties The trigger configuration properties, shown in Table 39-11, are used to control trigger behavior in SQL Server. Trigger behavior can be set at both the server and database levels. 929 www.getcoolebook.com Nielsen c39.tex V4 - 07/21/2009 2:17pm Page 930 Part VI Enterprise Data Management TABLE 39-11 Trigger Configuration Properties Property Level* Graphic Control Code Option Allow Nested Triggers S Management Studio EXEC sp_configure ‘nested triggers’ Recursive Triggers D Management Studio ALTER DATABASE <DB Name> SET recursive_triggers * The configuration level refers to Server, Database, or Connection. Nested triggers A trigger is a small stored procedure that is executed on an insert, update,ordelete operation on a table. Triggers are nested when a trigger performs an action that initiates another trigger, which can initiate another trigger, and so on. Triggers can be nested up to 32 levels. The nested triggers server configuration option can be used to control whether AFTER triggers can be nested triggers. In Management Studio, the nested trigger option can be set by selecting True (default) or False in the ‘‘Allow Triggers to Fire Others’’ option in the Server Properties Advanced tab (refer to Figure 39-9). To turn nested triggers OFF in code, do the following: EXEC sp_configure ‘nested triggers’, 0; RECONFIGURE; INSTEAD OF triggers can be nested regardless of the setting of this option. Recursive triggers If the code in the trigger inserts, updates, or deletes the same table again, then the trigger causes itself to be executed again. Recursion can also occur if the code in the trigger fires and performs an action that causes a trigger on another table to fire. This second trigger performs an action that causes an update to occur on the original table, which causes the original trigger to fire again. Recursive behavior is enabled or disabled by the recursive trigger database option. By default, the RECURSIVE_TRIGGERS option is set to OFF. In Management Studio, the recursive triggers option can be enabled by selecting True in the ‘‘Recursive Triggers Enabled’’ option in the Database Properties Options tab (refer to Figure 39-2). The following sets the recursive triggers option ON in the AdventureWorks2008 sample database in T-SQL code: ALTER DATABASE AdventureWorks2008 SET RECURSIVE_TRIGGERS ON; The server property nested triggers and the database property recursive triggers are often confused with each other. Refer to Chapter 26, ‘‘Creating DML Triggers,’’ for a com- plete explanation, including coverage of how triggers can call other triggers and how these properties control trigger behavior. 930 www.getcoolebook.com Nielsen c39.tex V4 - 07/21/2009 2:17pm Page 931 Configuring SQL Server 39 Database-state-configuration properties The database-state-configuration properties, shown in Table 39-12, are available in SQL Server. These configurations are mostly used when a DBA is performing maintenance on the database. The state of the database can be set with the ALTER DATABASE command. The sp_dboption com- mand is also available for backward compatibility. However, using the sp_dboption command it is not recommended because it will be removed in future versions of SQL Server. Database-access level The database-access-configuration options are used to set the state of the database. When the database is offline, no access to the database is allowed. To set AdventureWorks2008 sample database to an OFFLINE state in code, do the following: ALTER DATABASE AdventureWorks2008 SET OFFLINE; TABLE 39-12 Database-State-Configuration Properties Property Level* Graphic Control Code Option Database OffLine D Management Studio ALTER DATABASE <DB Name> SET OFFLINE Database OnLine D Management Studio ALTER DATABASE <DB Name> SET ONLINE Emergency D - ALTER DATABASE <DB Name> SET EMERGENCY Read-Only D Management Studio ALTER DATABASE <DB Name> SET READ_ONLY Restricted Access — Members of db_owner, dbcreator, or sysadmin D Management Studio ALTER DATABASE <DB Name> SET RESTRICTED_USER Restricted Access — Single user D Management Studio ALTER DATABASE <DB Name> SET SINGLE_USER Multi User D Management Studio ALTER DATABASE <DB Name> SET MULTI_USER Compatibility Level D Management Studio ALTER DATABASE <DB NAME> SET COMPATIBILITY_LEVEL * The configuration level refers to Server, Database, or Connection. 931 www.getcoolebook.com . 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. behavior in SQL Server. 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. for the AdventureWorks2008 sample database to LOCAL in code, do the following: ALTER DATABASE AdventureWorks2008 SET CURSOR_DEFAULT LOCAL; SQL ANSI–configuration properties The SQL ANSI–configuration