sys.fn_hadr_is_primary_replica (Transact-SQL)

 

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

Applies to: SQL Server (SQL Server 2014 through current version).

Topic link icon Transact-SQL Syntax Conventions

  
sys.fn_hadr_is_primary_replica ( 'dbname' )  

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

Returns 1 if the database on the current instance is the primary replica. Otherwise returns 0.

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.  
  

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  

AlwaysOn Availability Groups Functions (Transact-SQL)
AlwaysOn Availability Groups (SQL Server)
CREATE AVAILABILITY GROUP (Transact-SQL)
ALTER AVAILABILITY GROUP (Transact-SQL)
AlwaysOn Availability Groups Catalog Views (Transact-SQL)

Community Additions

ADD
Show: