Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Getting Started with SQL Server 2014 Administration

You're reading from   Getting Started with SQL Server 2014 Administration Optimize your database server to be fast, efficient, and highly secure using the brand new features of SQL Server 2014

Arrow left icon
Product type Paperback
Published in Apr 2014
Publisher Packt
ISBN-13 9781782172413
Length 106 pages
Edition Edition
Arrow right icon
Author (1):
Arrow left icon
Gethyn Ellis Gethyn Ellis
Author Profile Icon Gethyn Ellis
Gethyn Ellis
Arrow right icon
View More author details
Toc

Troubleshooting Availability Groups


A new system function has been added that will help you identify if the database and instance you are currently working on is the primary replica. The function is called sys.fn_hadr_is_primary_replica and the syntax for the is function is as follows:

sys.fn_hadr_is_primary_replica ( 'dbname' )

Here, 'dbname' is the name of the database you want to check. The procedure returns a 1, if it evaluates to true, that is, it is the primary replica. You can combine this with some logical code to determine which section of the code to run depending on whether the replica is currently the primary replica.

The following script will back up your database if the current instance that it runs is the primary instance:

declare @dbname varchar(30)
declare @backuplocation varchar(80)

set @dbname = 'DB1'
Set @backuplocation = 'c:\BACKUP\'
set @backuplocation = @backuplocation + @dbname
If sys.fn_hadr_is_primary_replica ( @dbname ) <> 1 
begin 
SELECT 'Nothing to backup...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image