Muokkaa

Jaa


sys.fn_hadr_is_primary_replica (Transact-SQL)

Applies to: SQL Server

Used to determine if the current replica is the primary replica.

Transact-SQL syntax conventions

Syntax

sys.fn_hadr_is_primary_replica ( 'dbname' )  

Arguments

'dbname'
Is the name of the database. dbname is type sysname.

Returns

Returns data type bit: 1 if the database on the current instance is the primary replica, otherwise 0.

Remarks

Use this function to conveniently determine whether the local instance is hosting the primary replica of the specified availability database. Sample code could be similar to the following.

If sys.fn_hadr_is_primary_replica ( @dbname ) <> 1   
BEGIN  
-- If this is not the primary replica, exit (probably without error).  
END  
-- If this is the primary replica, continue to do the backup.  

Examples

A. Using sys.fn_hadr_is_primary_replica

The following example returns 1 if the specified database on the local instance is the primary replica.

SELECT sys.fn_hadr_is_primary_replica ('TestDB');  
GO  

Security

Permissions

Requires VIEW SERVER STATE permission on the server.

See Also

Always On Availability Groups Functions (Transact-SQL)
sys.dm_hadr_database_replica_states (Transact-SQL) Always On Availability Groups (SQL Server)
CREATE AVAILABILITY GROUP (Transact-SQL)
ALTER AVAILABILITY GROUP (Transact-SQL)
Always On Availability Groups Catalog Views (Transact-SQL)