Skip to content

Latest commit

 

History

History
75 lines (59 loc) · 2.88 KB

sys-fn-hadr-is-primary-replica-transact-sql.md

File metadata and controls

75 lines (59 loc) · 2.88 KB
title description author ms.author ms.date ms.service ms.subservice ms.topic f1_keywords helpviewer_keywords dev_langs
sys.fn_hadr_is_primary_replica (Transact-SQL)
sys.fn_hadr_is_primary_replica (Transact-SQL)
MikeRayMSFT
mikeray
03/17/2017
sql
system-objects
reference
sys.fn_hadr_is_primary_replica
fn_hadr_is_primary_replica_TSQL
fn_hadr_is_primary_replica
sys.fn_hadr_is_primary_replica_TSQL
fn_hadr_is_primary_replica
sys.fn_hadr_is_primary_replica
TSQL

sys.fn_hadr_is_primary_replica (Transact-SQL)

[!INCLUDEsqlserver]

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

:::image type="icon" source="../../includes/media/topic-link-icon.svg" border="false"::: 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)