SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 

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

 

SQL Undercover

David Fowler and Adrian Buckman, two database nerds who love nothing more than to spend their time, reading about, researching and sharing all things SQL Server. David is a DBA with over 15 years production experience of SQL Server, from version 6.5 through to 2016. He has worked in a number of different settings and is currently the technical lead at one of the largest software companies in the UK. After working in the motor trade for over 11 years Adrian decided to give it all up to persue a dream of working in I.T. Adrian has over 3 years of experience working with SQL server and loves all things SQL, Adrian currently works as a Database Administrator for one of the UK’s Largest Software Companies.

Comments

Leave a comment on the original post [sqlundercover.com, opens in a new window]

Loading comments...