UNDERCOVER TOOLBOX: sp_WhatsMyAG

Johnny_Crow's_Garden_-_Till_the_Hippopotami_Said_-__Ask_no_further_'What_am_I_'_

Here’s a quick one for you.  If you happen to be managing SQL Servers with a large number of databases and availability groups, it can sometimes be difficult to keep track of which database belongs to which availability group.

sp_WhatsMyAG will tell you just that.  You can either provide it with the database name and it’ll tell you the AG that your specified database belongs to or you can leave the parameter NULL and you’ll get the AG of the database whose context you’re currently in.

The script is also available on our GitHub site.


 /******************************************************************
Author: David Fowler
Revision date: 07/03/2018
Version: 1
Description: Return the name of the AG that a specified database belongs to

© www.sqlundercover.com 

MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
 subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
******************************************************************/

USE [master]
GO

IF EXISTS (SELECT 1 FROM sys.procedures WHERE name = 'sp_WhatsMyAG')
DROP PROC [sp_WhatsmyAG]
GO

CREATE PROCEDURE [dbo].[sp_WhatsMyAG]
(
@DBname NVARCHAR(128) = NULL
)
AS
BEGIN
SELECT cluster.database_name,ag.name AS AGname
FROM master.sys.availability_groups ag
INNER JOIN sys.availability_databases_cluster cluster ON ag.group_id = cluster.group_id
WHERE cluster.database_name = ISNULL(@DBname, db_name())
END

GO

 

One thought on “UNDERCOVER TOOLBOX: sp_WhatsMyAG

Add yours

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

Create a website or blog at WordPress.com

Up ↑

%d bloggers like this: