Skip to content

Latest commit

 

History

History
79 lines (63 loc) · 3.01 KB

File metadata and controls

79 lines (63 loc) · 3.01 KB
description sys.fn_hadr_is_primary_replica (Transact-SQL)
title sys.fn_hadr_is_primary_replica (Transact-SQL) | Microsoft Docs
ms.custom
ms.date 03/17/2017
ms.prod sql
ms.prod_service database-engine
ms.reviewer
ms.technology system-objects
ms.topic reference
f1_keywords
sys.fn_hadr_is_primary_replica
fn_hadr_is_primary_replica_TSQL
fn_hadr_is_primary_replica
sys.fn_hadr_is_primary_replica_TSQL
dev_langs
TSQL
helpviewer_keywords
fn_hadr_is_primary_replica
sys.fn_hadr_is_primary_replica
ms.assetid c9b1969f-be1d-4dfb-a33d-551f380b9e27
author MikeRayMSFT
ms.author mikeray

sys.fn_hadr_is_primary_replica (Transact-SQL)

[!INCLUDEsqlserver]

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

Topic link icon 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)