sys.fn_hadr_is_primary_replica (Transact-SQL)

THIS TOPIC APPLIES TO:yesSQL Server (starting with 2014)noAzure SQL DatabasenoAzure SQL Data Warehouse noParallel Data Warehouse

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

Syntax


sys.fn_hadr_is_primary_replica ( 'dbname' )  

Arguments

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

Returns

Returns 1 if the database on the current instance is the primary replica. Otherwise returns 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  

See Also

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)