- Docs
- /
06 Jun 2022 38173 views 0 minutes to read Contributors
T-SQLTransact-SQL
1234567891011121314151617 DECLARE @role_desc varchar(20)IF( HAS_PERMS_BY_NAME ('sys.dm_hadr_availability_replica_states', 'OBJECT', 'execute') = 1)BEGIN-- if this is not an AG server then return 'PRIMARY'IF NOT EXISTS( SELECT 1 FROM sys.DATABASES d INNER JOIN sys.dm_hadr_availability_replica_states hars ON d.replica_id = hars.replica_id)SELECT @role_desc = 'PRIMARY'ELSE-- else return if there is AN PRIMARY availability group PRIMARY else 'SECONDARYIF EXISTS( SELECT hars.role_desc FROM sys.DATABASES d INNER JOIN sys.dm_hadr_availability_replica_states hars ON d.replica_id = hars.replica_id WHERE hars.role_desc = 'PRIMARY' )SELECT @role_desc = 'PRIMARY'ELSESELECT @role_desc = 'SECONDARY'ENDELSESELECT @role_desc = 'PRIMARY'SELECT @role_desc
In this article
Please choose a monitoring environment to connect to:
Enter your email address to recover your password.
Download link successfully sent. Check your email for details.
Reset password link successfully sent to . Check your email for details.
An email with a link for creating your password is sent to
Share link successfully sent to .
Your reply is send.
Download link successfully sent to . Check your email for details.
Download link successfully sent to . Check your email for details.
Download link successfully sent to . Check your email for details.
Download link successfully sent to . Check your email for details.
We received your request. We will contact you shortly