ALTER DATABASE (Transact-SQL) SET HADR

**APPLIES TO:** ![yes](media/yes.png)SQL Server (starting with 2012) ![no](media/no.png)Azure SQL Database ![no](media/no.png)Azure SQL Data Warehouse ![no](media/no.png)Parallel Data Warehouse

This topic contains the ALTER DATABASE syntax for setting Always On availability groups options on a secondary database. Only one SET HADR option is permitted per ALTER DATABASE statement. These options are supported only on secondary replicas.

Topic link icon Transact-SQL Syntax Conventions

Syntax

  
ALTER DATABASE database_name  
   SET HADR   
   {  
        { AVAILABILITY GROUP = group_name | OFF }  
   | { SUSPEND | RESUME }  
   }  
[;]  

Arguments

database_name
Is the name of the secondary database to be modified.

SET HADR
Executes the specified Always On availability groups command on the specified database.

{ AVAILABILITY GROUP =group_name | OFF }
Joins or removes the availability database from the specified availability group, as follows:

group_name
Joins the specified database on the secondary replica that is hosted by the server instance on which you execute the command to the availability group specified by group_name.

The prerequisites for this operation are as follows:

For more information, see Join a Secondary Database to an Availability Group (SQL Server).

OFF
Removes the specified secondary database from the availability group.

Removing a secondary database can be useful if it has fallen far behind the primary database, and you do not want to wait for the secondary database to catch up. After removing the secondary database, you can update it by restoring a sequence of backups ending with a recent log backup (using RESTORE … WITH NORECOVERY).

[!IMPORTANT]
To completely remove an availability database from an availability group, connect to the server instance that hosts the primary replica, and use the ALTER AVAILABILITY GROUPgroup_name REMOVE DATABASE availability_database_name statement. For more information, see Remove a Primary Database from an Availability Group (SQL Server).

SUSPEND
Suspends data movement on a secondary database. A SUSPEND command returns as soon as it has been accepted by the replica that hosts the target database, but actually suspending the database occurs asynchronously.

The scope of the impact depends on where you execute the ALTER DATABASE statement:

When a database on a secondary replica is suspended, both the database and replica become unsynchronized and are marked as NOT SYNCHRONIZED.

[!IMPORTANT]
While a secondary database is suspended, the send queue of the corresponding primary database will accumulate unsent transaction log records. Connections to the secondary replica return data that was available at the time the data movement was suspended.

[!NOTE]
Suspending and resuming an Always On secondary database does not directly affect the availability of the primary database, though suspending a secondary database can impact redundancy and failover capabilities for the primary database, until the suspended secondary database is resumed. This is in contrast to database mirroring, where the mirroring state is suspended on both the mirror database and the principal database until mirroring is resumed. Suspending an Always On primary database suspends data movement on all the corresponding secondary databases, and redundancy and failover capabilities cease for that database until the primary database is resumed.

For more information, see Suspend an Availability Database (SQL Server).

RESUME
Resumes suspended data movement on the specified secondary database. A RESUME command returns as soon as it has been accepted by the replica that hosts the target database, but actually resuming the database occurs asynchronously.

The scope of the impact depends on where you execute the ALTER DATABASE statement:

Database States

When a secondary database is joined to an availability group, the local secondary replica changes the state of that secondary database from RESTORING to ONLINE. If a secondary database is removed from the availability group, it is set back to the RESTORING state by the local secondary replica. This allows you to apply subsequent log backups from the primary database to that secondary database.

Restrictions

Execute ALTER DATABASE statements outside of both transactions and batches.

Security

Permissions

Requires ALTER permission on the database. Joining a database to an availability group requires membership in the db_owner fixed database role.

Examples

The following example joins the secondary database, AccountsDb1, to the local secondary replica of the AccountsAG availability group.

ALTER DATABASE AccountsDb1 SET HADR AVAILABILITY GROUP = AccountsAG;  

[!NOTE]
To see this Transact\-SQL statement used in context, see Create an Availability Group (Transact-SQL).

See Also

ALTER DATABASE (Transact-SQL)
ALTER AVAILABILITY GROUP (Transact-SQL)
CREATE AVAILABILITY GROUP (Transact-SQL)
Overview of AlwaysOn Availability Groups (SQL Server) Troubleshoot AlwaysOn Availability Groups Configuration (SQL Server)