1. Trang chủ
  2. » Công Nghệ Thông Tin

Hướng dẫn học Microsoft SQL Server 2008 part 97 pdf

10 378 0

Đang tải... (xem toàn văn)

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 10
Dung lượng 596,48 KB

Nội dung

TABLE 39-8Cursor-Configuration Properties Cursor Threshold S Management Studio EXEC sp_configure ‘cursor threshold’ Cursor Close on Commit SDC Management Studio ALTER DATABASE SET curso

Trang 1

TABLE 39-8

Cursor-Configuration Properties

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

Thecursor thresholdproperty 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 thecursor 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

thecursor thresholdproperty is set to0

The default of-1causes 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, thecursor thresholdoption 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 toON If it is set to

OFF(the default), then cursors remain open across transactions until aclose cursorstatement is

issued

Trang 2

TheCURSOR_CLOSE_ON_COMMIToption can be set from Management Studio and code at the server,

database, and connection level In Management Studio, theCURSOR_CLOSE_ON_COMMIToption 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: SelectTruefor 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 theSET CURSOR_CLOSE_ON_COMMITcheck box

To set this property for all future connections, click the Tools menu➪ Options ➪ Query

Execution➪ ANSI and check theSET CURSOR_CLOSE_ON_COMMITcheck box

To setCURSOR_CLOSE_ON_COMMITin code, do the following:

■ Server level

EXEC sp_configure ‘user options’, 4;

RECONFIGURE;

■ Database level (example to set the option on for theAdventureWorks2008sample 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 tolocal When it is

set toglobal(the default), the cursor’s scope can be extended outside the object that created it

In Management Studio, theCURSOR_DEFAULToption 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

theAdventureWorks2008sample database toLOCALin 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

Trang 3

TABLE 39-9

SQL ANSI–Configuration Properties

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 abegin transactionto start a logical unit of work Oracle assumes

abegin transactionis 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 Thesp_configuresystem stored procedure has the ‘‘user options’’ setting that allows

manipulation of server-wide ANSI settings and it works across databases TheALTER DATABASE

com-mand can be used to set the default database setting for ANSI Connection-level settings are performed

with theSETcommand 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

Trang 4

■ 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 theSET ANSI_DEFAULTScommand to manage a group of SQL Server settings

WhenSET ANSI_DEFAULTSis 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 setANSI_DEFAULTSin code, do the following:

SET ANSI_DEFAULTS ON;

ANSI null default

TheANSI_NULL_DEFAULTsetting controls the default nullability This setting is used when a

NULLorNOT_NULLis not explicitly specified when creating a table The default database setting for

ANSI_NULL_DEFAULTisOFF

To set theANSI_NULL_DEFAULToption toONfor theAdventureWorks2008sample database in

code, do the following:

ALTER DATABASE AdventureWorks2008 SET ANSI_NULL_DEFAULT ON;

If theANSI_NULL_DEFAULToption is not set at the database level, you can set the nullability of new

columns using theSET ANSI_NULL_DFLT_ONandSET ANSI_NULL_DFLT_OFFcommands.SET

ANSI_NULL_DFLT_ONcan be enabled to allow null values at the connection level:

SET ANSI_NULL_DFLT_ON ON;

SET ANSI_NULL_DFLT_OFFcan be enabled to not allow null values at the connection level:

SET ANSI_NULL_DFLT_OFF ON;

Trang 5

To enableANSI_NULL_DFLT_ONat the server level in code, do the following:

EXEC sp_configure ‘user options’, 1024;

RECONFIGURE;

The following enablesANSI_NULL_DFLT_OFFat 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

TheANSI_NULLSconnection setting is used to determine comparison evaluations When set toON, all

comparisons to a null value will evaluate toUNKNOWN When set toOFF, the comparison to a null value

will evaluate totrueif both values areNULL The default database setting forANSI_NULLSisOFF

The following enablesANSI_NULLSat the connection level:

SET ANSI_NULLS ON;

IfSET ANSI_NULLSis not specified, then the settings ofANSI_NULLSof the current database apply

To enableANSI_NULLSfor theAdventureWorks2008sample database in code, do the following:

ALTER DATABASE AdventureWorks2008 SET ANSI_NULLS ON;

The following enablesANSI_NULLSat 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

TheANSI_PADDINGconnection setting affects only newly created columns When set toON, data

stored inchar,varchar,binary, andvarbinarydata 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 toOFF, all leading and trailing blanks and zeros are trimmed The default database setting for

ANSI_PADDINGisOFF

The following enablesANSI_PADDINGin code at the connection level:

SET ANSI_PADDING ON;

IfSET ANSI_PADDINGis not specified, then the settings ofANSI_PADDINGof the current database

apply The following enablesANSI_PADDINGfor theAdventureWorks2008sample database:

ALTER DATABASE AdventureWorks2008 SET ANSI_PADDING ON;

Trang 6

This enablesANSI_PADDINGat 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

TheANSI_WARNINGSconnection 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

forANSI_WARNINGSisOFF When this setting isOFF, 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 isON, the query is aborted and errors are raised when arithmetic overflow

errors and divide-by-zero occurs

To setANSI_WARNINGSin code at the connection level:

SET ANSI_WARNINGS ON;

IfSET ANSI_WARNINGSis not specified, then the settings ofANSI_WARNINGSof the current database

apply The following enablesANSI_WARNINGSfor theAdventureWorks2008sample database:

ALTER DATABASE AdventureWorks2008 SET ANSI_WARNINGS ON;

Use the following to enableANSI_WARNINGSat the server level in code:

EXEC sp_configure ‘user options’, 8;

RECONFIGURE;

Arithmetic abort

TheARITHABORTconnection setting is used to handle query termination if arithmetic errors such as

data overflow or divide-by-zero occurs The default database setting forARITHABORTisOFF

What exactly is terminated also depends on theANSI_WARNINGSsetting Table 39-10 explains the

behavior based on the values ofANSI_WARNINGSandARITHABORT

To setARITHABORTin code at the connection level:

SET ARITHABORT ON;

IfARITHABORTis not specified, then the settings of the current database apply To enable

ARITHABORTforAdventureWorks2008sample database in code, do the following:

ALTER DATABASE AdventureWorks2008 SET ARITHABORT ON;

The following enablesARITHABORTat the server level:

EXEC sp_configure ‘user options’, 64;

RECONFIGURE;

Trang 7

TABLE 39-10

ANSI_WARNINGS and ARITHABORT Behavior

ON OFF Batch is aborted or transaction is rolled back

OFF OFF No warning is raised and null is returned

Arithmetic ignore

TheARITHIGNOREconnection 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 theARITHABORT

setting BothARITHABORTandARITHIGNOREcan be set toONbutARITHABORTtakes precedence

overARITHIGNORE To setARITHIGNOREin code, use the following:

SET ARITHIGNORE ON;

The following enablesARITHIGNOREat the server level:

EXEC sp_configure ‘user options’, 128;

RECONFIGURE;

Numeric round abort

TheNUMERIC_ROUNDABORTconnection setting is used to control the behavior of numeric

decimal-precision-rounding errors in process WhenNUMERIC_ROUNDABORTis set toONandARITHABORT

is set toON, 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

IfARITHABORTis set toOFFandNUMERIC_ROUNDABORTis set toON, a warning appears and null is

returned WhenNUMERIC_ROUNDABORTis set toOFF, 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 forNUMERIC_ROUNDABORTis OFF

To setNUMERIC_ROUNDABORTin code at the connection level:

SET NUMERIC_ROUNDABORT ON;

IfNUMERIC_ROUNDABORTis not specified, then the settings of the current database apply The

follow-ing enablesNUMERIC_ROUNDABORTfor theAdventureWorks2008sample database:

ALTER DATABASE AdventureWorks2008 SET NUMERIC_ROUNDABORT ON;

To enableNUMERIC_ROUNDABORTat the server level in code, do the following:

EXEC sp_configure ‘user options’, 8192;

RECONFIGURE;

Trang 8

Concatenation null yields null

TheCONCAT_NULL_YIELDS_NULLsetting is used to control the behavior of the result when

con-catenating a string with anull When set toON, any string concatenated with anullwill result in a

null When set toOFF, any string concatenated with anullwill result in the original string, ignoring

thenull The default database setting forCONCAT_NULL_YIELDS_NULLisOFF

The following setsCONCAT_NULL_YIELDS_NULLat the connection level:

SET CONCAT_NULL_YIELDS_NULL ON;

IfCONCAT_NULL_YIELDS_NULLis not specified, then the settings of the current database apply The

following enablesCONCAT_NULL_YIELDS_NULLfor theAdventureWorks2008sample database:

ALTER DATABASE AdventureWorks2008 SET CONCAT_NULL_YIELDS_NULL ON;

The following enablesCONCAT_NULL_YIELDS_NULLat the server level:

EXEC sp_configure ‘user options’, 4096;

RECONFIGURE;

Use quoted identifier

TheQUOTED_IDENTIFIERsetting enables you to refer to an identifier, such as a column name, by

enclosing it within double quotes When set toON, identifiers can be delimited by double quotation

marks When set toOFF, identifiers cannot be placed in quotation marks and must not be keywords

The default database setting forQUOTED_IDENTIFIERisOFF

This setsQUOTED_IDENTIFIERtoONat the connection level:

SET QUOTED_IDENTIFIER ON;

IfQUOTED_IDENTIFIERis not specified, then the settings of the current database apply To enable

QUOTED_IDENTIFIERfor theAdventureWorks2008sample database, use the following:

ALTER DATABASE AdventureWorks2008 SET QUOTED_IDENTIFIER ON;

This enablesQUOTED_IDENTIFIERat 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 , and QUOTED_IDENTIFIER ) must be

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

Trang 9

TABLE 39-11

Trigger Configuration Properties

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 aninsert,update, ordeleteoperation

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 Thenested triggers

server configuration option can be used to control whetherAFTERtriggers can be nested triggers

In Management Studio, thenested triggeroption 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 turnnested triggers OFFin 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, theRECURSIVE_TRIGGERSoption is

set toOFF

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 optionONin theAdventureWorks2008sample 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.

Trang 10

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 theALTER DATABASEcommand Thesp_dboption

com-mand is also available for backward compatibility However, using thesp_dboptioncommand 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 setAdventureWorks2008sample database to anOFFLINEstate in code, do the following:

ALTER DATABASE AdventureWorks2008 SET OFFLINE;

TABLE 39-12

Database-State-Configuration Properties

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.

Ngày đăng: 04/07/2014, 09:20

TỪ KHÓA LIÊN QUAN

w