ALTER DATABASE SET Options (Transact-SQL)
This topic contains the ALTER DATABASE syntax that is related to setting database options. For other ALTER DATABASE syntax, see ALTER DATABASE (Transact-SQL). Database mirroring and compatibility levels are SET options but are described in separate topics because of their length. For more information, see ALTER DATABASE Database Mirroring (Transact-SQL) and ALTER DATABASE Compatibility Level (Transact-SQL).
ALTER DATABASE database_name
SET
{
{ <optionspec> [ ,...n ] [ WITH <termination> ] }
}
<optionspec>::=
{
<auto_option>
| <change_tracking_option>
| <cursor_option>
| <database_mirroring_option>
| <date_correlation_optimization_option>
| <db_encryption_option>
| <db_state_option>
| <db_update_option>
| <db_user_access_option>
| <external_access_option>
| <parameterization_option>
| <recovery_option>
| <service_broker_option>
| <snapshot_option>
| <sql_option>
}
<auto_option> ::=
{
AUTO_CLOSE { ON | OFF }
| AUTO_CREATE_STATISTICS { ON | OFF }
| AUTO_SHRINK { ON | OFF }
| AUTO_UPDATE_STATISTICS { ON | OFF }
| AUTO_UPDATE_STATISTICS_ASYNC { ON | OFF }
}
<change_tracking_option> ::=
{
CHANGE_TRACKING {
= ON [ <change_tracking_option_list > ] |
<change_tracking_option_list> |
= OFF
}
}
<change_tracking_option_list> ::=
{
( <change_tracking_option> | <change_tracking_option_list> ,
<change_tracking_option> )
}
<change_tracking_option> ::=
{
AUTO_CLEANUP = { ON | OFF }
| CHANGE_RETENTION = { retention_period { DAYS | HOURS | MINUTES } ]
}
<cursor_option> ::=
{
CURSOR_CLOSE_ON_COMMIT { ON | OFF }
| CURSOR_DEFAULT { LOCAL | GLOBAL }
}
<database_mirroring_option>ALTER DATABASE Database Mirroring<date_correlation_optimization_option> ::=
{
DATE_CORRELATION_OPTIMIZATION { ON | OFF }
}
<db_encryption_option> ::=
ENCRYPTION { ON | OFF }
<db_state_option> ::=
{ ONLINE | OFFLINE | EMERGENCY }
<db_update_option> ::=
{ READ_ONLY | READ_WRITE }
<db_user_access_option> ::=
{ SINGLE_USER | RESTRICTED_USER | MULTI_USER }
<external_access_option> ::=
{
DB_CHAINING { ON | OFF }
| TRUSTWORTHY { ON | OFF }
}
<parameterization_option> ::=
{
PARAMETERIZATION { SIMPLE | FORCED }
}
<recovery_option> ::=
{
RECOVERY { FULL | BULK_LOGGED | SIMPLE }
| TORN_PAGE_DETECTION { ON | OFF }
| PAGE_VERIFY { CHECKSUM | TORN_PAGE_DETECTION | NONE }
}
<service_broker_option> ::=
{
ENABLE_BROKER
| DISABLE_BROKER
| NEW_BROKER
| ERROR_BROKER_CONVERSATIONS
| HONOR_BROKER_PRIORITY { ON | OFF}
}
<snapshot_option> ::=
{
ALLOW_SNAPSHOT_ISOLATION { ON | OFF }
| READ_COMMITTED_SNAPSHOT {ON | OFF }
}
<sql_option> ::=
{
ANSI_NULL_DEFAULT { ON | OFF }
| ANSI_NULLS { ON | OFF }
| ANSI_PADDING { ON | OFF }
| ANSI_WARNINGS { ON | OFF }
| ARITHABORT { ON | OFF }
| COMPATIBILITY_LEVEL = { 80 | 90 | 100 }
| CONCAT_NULL_YIELDS_NULL { ON | OFF }
| NUMERIC_ROUNDABORT { ON | OFF }
| QUOTED_IDENTIFIER { ON | OFF }
| RECURSIVE_TRIGGERS { ON | OFF }
}
<termination>::=
{
ROLLBACK AFTER integer [ SECONDS ]
| ROLLBACK IMMEDIATE
| NO_WAIT
}
<auto_option>::=
Controls automatic options.
<change_tracking_option>::=
Controls change tracking options. You can enable change tracking, set options, change options, and disable change tracking. For examples, see the Examples section later in this topic.
<cursor_option>::=
Controls cursor options.
<database_mirroring>
For the argument descriptions, see ALTER DATABASE Database Mirroring (Transact-SQL).
<date_correlation_optimization_option> ::=
Controls the date_correlation_optimization option.
<db_encryption_option>::=
Controls the database encryption state.
When encryption is enabled at the database level all filegroups will be encrypted. Any new filegroups will inherit the encrypted property. If any filegroups in the database are set to READ ONLY, the database encryption operation will fail.
You can see the encryption state of the database by using the sys.dm_database_encryption_keys dynamic management view.
<db_state_option>::=
Controls the state of the database.
The status of this option can be determined by examining the state and state_desc columns in the sys.databases catalog view or the Status property of the DATABASEPROPERTYEX function. For more information, see Database States.
A database marked as RESTORING cannot be set to OFFLINE, ONLINE, or EMERGENCY. A database may be in the RESTORING state during an active restore operation or when a restore operation of a database or log file fails because of a corrupted backup file. For more information, see Responding to SQL Server Restore Errors Caused by Damaged Backups.
<db_update_option>::=
Controls whether updates are allowed on the database.
To change this state, you must have exclusive access to the database. For more information, see the SINGLE_USER clause.
<db_user_access_option> ::=
Controls user access to the database.
The status of this option can be determined by examining the user_access column in the sys.databases catalog view or the UserAccess property of the DATABASEPROPERTYEX function.
<external_access_option>::=
Controls whether the database can be accessed by external resources, such as objects from another database.
<parameterization_option> ::=
Controls the parameterization option.
<recovery_option> ::=
Controls database recovery options and disk I/O error checking.
The default recovery model is determined by the recovery model of the model database. For more information about selecting the appropriate recovery model, see Choosing the Recovery Model for a Database.
The status of this option can be determined by examining the recovery_model and recovery_model_desc columns in the sys.databases catalog view or the Recovery property of the DATABASEPROPERTYEX function.
<service_broker_option>::=
Controls the following Service Broker options: enables or disables message delivery, sets a new Service Broker identifier, or sets conversation priorities to ON or OFF. For more information about message deliver and Service Broker identifiers, see Managing Service Broker Identities. For more information about conversation priority levels, see Conversation Priorities. For examples that show how to use the HONOR_BROKER_PRIORITY option, see Managing Conversation Priorities.
<snapshot_option>::=
Determines the transaction isolation level.
<sql_option>::=
Controls the ANSI compliance options at the database level.
WITH <termination>::=
Specifies when to roll back incomplete transactions when the database is transitioned from one state to another. If the termination clause is omitted, the ALTER DATABASE statement waits indefinitely if there is any lock on the database. Only one termination clause can be specified, and it follows the SET clauses.
Note |
|---|
Not all database options use the WITH <termination> clause. For more information, see the table under "Setting Options" in the Remarks section. |
Setting Options
To retrieve current settings for database options, use the sys.databases catalog view or DATABASEPROPERTYEX. For a list of default values assigned to the database when it is first created, see Setting Database Options.
After you set a database option, the modification takes effect immediately.
To change the default values for any one of the database options for all newly created databases, change the appropriate database option in the model database.
Not all database options use the WITH <termination> clause or can be specified in combination with other options. The following table lists these options and their option and termination status.
Options category | Can be specified with other options | Can use the WITH <termination> clause |
|---|---|---|
<db_state_option> | Yes | Yes |
<db_user_access_option> | Yes | Yes |
<db_update_option> | Yes | Yes |
<external_access_option> | Yes | No |
<cursor_option> | Yes | No |
<auto_option> | Yes | No |
<sql_option> | Yes | No |
<recovery_option> | Yes | No |
<database_mirroring_option> | No | No |
ALLOW_SNAPSHOT_ISOLATION | No | No |
READ_COMMITTED_SNAPSHOT | No | Yes |
<service_broker_option> | Yes | No |
DATE_CORRELATION_OPTIMIZATION | Yes | Yes |
<parameterization_option> | Yes | Yes |
<change_tracking_option> | Yes | Yes |
<db_encryption> | Yes | No |
The plan cache for the instance of SQL Server is cleared by setting one of the following options:
OFFLINE | READ_WRITE |
ONLINE | MODIFY FILEGROUP DEFAULT |
MODIFY_NAME | MODIFY FILEGROUP READ_WRITE |
COLLATE | MODIFY FILEGROUP READ_ONLY |
READ_ONLY |
|
Clearing the plan cache causes a recompilation of all subsequent execution plans and can cause a sudden, temporary decrease in query performance. For each cleared cachestore in the plan cache, the SQL Server error log contains the following informational message: "SQL Server has encountered %d occurrence(s) of cachestore flush for the '%s' cachestore (part of plan cache) due to some database maintenance or reconfigure operations". This message is logged every five minutes as long as the cache is flushed within that time interval.
A. Setting options on a database
The following example sets the recovery model and data page verification options for the AdventureWorks sample database.
USE master; GO ALTER DATABASE AdventureWorks SET RECOVERY FULL, PAGE_VERIFY CHECKSUM; GO
B. Setting the database to READ_ONLY
Changing the state of a database or filegroup to READ_ONLY or READ_WRITE requires exclusive access to the database. The following example sets the database to SINGLE_USER mode to obtain exclusive access. The example then sets the state of the AdventureWorks database to READ_ONLY and returns access to the database to all users.
Note |
|---|
This example uses the termination option WITH ROLLBACK IMMEDIATE in the first ALTER DATABASE statement. All incomplete transactions will be rolled back and any other connections to the AdventureWorks sample database will be immediately disconnected. |
USE master; GO ALTER DATABASE AdventureWorks SET SINGLE_USER WITH ROLLBACK IMMEDIATE; GO ALTER DATABASE AdventureWorks SET READ_ONLY; GO ALTER DATABASE AdventureWorks SET MULTI_USER; GO
C. Enabling snapshot isolation on a database
The following example enables the snapshot isolation framework option for the AdventureWorks database.
USE AdventureWorks;
GO
-- Check the state of the snapshot_isolation_framework
-- in the database.
SELECT name, snapshot_isolation_state,
snapshot_isolation_state_desc AS description
FROM sys.databases
WHERE name = N'AdventureWorks';
GO
USE master;
GO
ALTER DATABASE AdventureWorks
SET ALLOW_SNAPSHOT_ISOLATION ON;
GO
-- Check again.
SELECT name, snapshot_isolation_state,
snapshot_isolation_state_desc AS description
FROM sys.databases
WHERE name = N'AdventureWorks';
GO
The result set shows that the snapshot isolation framework is enabled.
name snapshot_isolation_state description --------------- ------------------------ ----------- AdventureWorks 1 ON
D. Enabling, modifying, and disabling change tracking
The following example enables change tracking for the AdventureWorks database and sets the retention period to 4 days.
ALTER DATABASE AdventureWorks SET CHANGE_TRACKING = ON (AUTO_CLEANUP = ON, CHANGE_RETENTION = 2 DAYS);
The following example shows how to change the retention period to 3 days.
ALTER DATABASE AdventureWorks SET CHANGE_TRACKING (CHANGE_RETENTION = 3 DAYS);
The following example shows how to disable change tracking for the AdventureWorks database.
ALTER DATABASE AdventureWorks SET CHANGE_TRACKING = OFF;
