MS SQL AlwaysOn – Is current node the Primary?

I’m not a DBA by any stretch of the imagination. I do, however, need to provide basic infrastructure support to a number of Microsoft SQL servers.

A number of our MSSQL 2012 servers have been configured to use AlwaysOn. This presents a challenge when developers/admins need to run maintenance scripts because you can only run these against the primary database if you need to modify.

Doesn’t sound like much of an issue, but what happens if you run the script against a secondary node? Well, it fails with an error. I’d rather have something a little cleaner than that, and so came up with the below T-SQL script that you can wrap around your maintenance script and fail cleanly.

This entry was posted in Scripting, SQL. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.