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 |
|
|
|
[!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
sys.fn_hadr_is_primary_replica ( 'dbname' )
'dbname'
Is the name of the database. dbname is type sysname.
Returns data type bit: 1 if the database on the current instance is the primary replica, otherwise 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.
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
Requires VIEW SERVER STATE permission on the server.
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)