{"id":102,"date":"2015-10-28T15:31:08","date_gmt":"2015-10-28T07:31:08","guid":{"rendered":"http:\/\/kylemcdonald.com.au\/?p=102"},"modified":"2018-08-14T00:36:04","modified_gmt":"2018-08-13T16:36:04","slug":"ms-sql-alwayson-current-node-primary","status":"publish","type":"post","link":"https:\/\/kylemcdonald.com.au\/2015\/10\/28\/ms-sql-alwayson-current-node-primary\/","title":{"rendered":"MS SQL AlwaysOn – Is current node the Primary?"},"content":{"rendered":"

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.<\/p>\n

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. <\/p>\n

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.<\/p>\n

<\/p>\n

\r\n-- Useful when running scripts so that it only runs on the primary node.\r\nIF (SELECT SERVERPROPERTY('MachineName')) = (SELECT primary_replica FROM sys.dm_hadr_availability_group_states)\r\n   PRINT 'This is the Primary Node. Running...'\r\n   -- Insert Your Script or Code Here\r\nELSE\r\n   PRINT 'This is not the Primary Node. Stopping.'\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"

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 Continue reading MS SQL AlwaysOn – Is current node the Primary?<\/span>→<\/span><\/a><\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[30,25],"tags":[],"yoast_head":"\nMS SQL AlwaysOn - Is current node the Primary? - Kyle McDonald<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/kylemcdonald.com.au\/2015\/10\/28\/ms-sql-alwayson-current-node-primary\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"MS SQL AlwaysOn - Is current node the Primary? - Kyle McDonald\" \/>\n<meta property=\"og:description\" content=\"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 Continue reading MS SQL AlwaysOn – Is current node the Primary?→\" \/>\n<meta property=\"og:url\" content=\"https:\/\/kylemcdonald.com.au\/2015\/10\/28\/ms-sql-alwayson-current-node-primary\/\" \/>\n<meta property=\"og:site_name\" content=\"Kyle McDonald\" \/>\n<meta property=\"article:published_time\" content=\"2015-10-28T07:31:08+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2018-08-13T16:36:04+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary\" \/>\n<meta name=\"twitter:creator\" content=\"@KarmicIT\" \/>\n<meta name=\"twitter:site\" content=\"@KarmicIT\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/kylemcdonald.com.au\/#website\",\"url\":\"https:\/\/kylemcdonald.com.au\/\",\"name\":\"Kyle McDonald\",\"description\":\"Perth-based IT enthusiast\",\"publisher\":{\"@id\":\"https:\/\/kylemcdonald.com.au\/#\/schema\/person\/f3fe27d0e0f57ef43e2444fbe8989906\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/kylemcdonald.com.au\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-AU\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/kylemcdonald.com.au\/2015\/10\/28\/ms-sql-alwayson-current-node-primary\/#webpage\",\"url\":\"https:\/\/kylemcdonald.com.au\/2015\/10\/28\/ms-sql-alwayson-current-node-primary\/\",\"name\":\"MS SQL AlwaysOn - Is current node the Primary? - Kyle McDonald\",\"isPartOf\":{\"@id\":\"https:\/\/kylemcdonald.com.au\/#website\"},\"datePublished\":\"2015-10-28T07:31:08+00:00\",\"dateModified\":\"2018-08-13T16:36:04+00:00\",\"inLanguage\":\"en-AU\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/kylemcdonald.com.au\/2015\/10\/28\/ms-sql-alwayson-current-node-primary\/\"]}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/kylemcdonald.com.au\/2015\/10\/28\/ms-sql-alwayson-current-node-primary\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/kylemcdonald.com.au\/2015\/10\/28\/ms-sql-alwayson-current-node-primary\/#webpage\"},\"author\":{\"@id\":\"https:\/\/kylemcdonald.com.au\/#\/schema\/person\/f3fe27d0e0f57ef43e2444fbe8989906\"},\"headline\":\"MS SQL AlwaysOn – Is current node the Primary?\",\"datePublished\":\"2015-10-28T07:31:08+00:00\",\"dateModified\":\"2018-08-13T16:36:04+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/kylemcdonald.com.au\/2015\/10\/28\/ms-sql-alwayson-current-node-primary\/#webpage\"},\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/kylemcdonald.com.au\/#\/schema\/person\/f3fe27d0e0f57ef43e2444fbe8989906\"},\"articleSection\":\"Scripting,SQL\",\"inLanguage\":\"en-AU\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/kylemcdonald.com.au\/2015\/10\/28\/ms-sql-alwayson-current-node-primary\/#respond\"]}]},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/kylemcdonald.com.au\/#\/schema\/person\/f3fe27d0e0f57ef43e2444fbe8989906\",\"name\":\"Kyle McDonald\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/kylemcdonald.com.au\/#personlogo\",\"inLanguage\":\"en-AU\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/74b24af47c7a561be463563db3fa821c?s=96&d=mm&r=g\",\"caption\":\"Kyle McDonald\"},\"logo\":{\"@id\":\"https:\/\/kylemcdonald.com.au\/#personlogo\"},\"sameAs\":[\"http:\/\/au.linkedin.com\/in\/karmicit\",\"https:\/\/twitter.com\/KarmicIT\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/kylemcdonald.com.au\/wp-json\/wp\/v2\/posts\/102"}],"collection":[{"href":"https:\/\/kylemcdonald.com.au\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kylemcdonald.com.au\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kylemcdonald.com.au\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/kylemcdonald.com.au\/wp-json\/wp\/v2\/comments?post=102"}],"version-history":[{"count":9,"href":"https:\/\/kylemcdonald.com.au\/wp-json\/wp\/v2\/posts\/102\/revisions"}],"predecessor-version":[{"id":246,"href":"https:\/\/kylemcdonald.com.au\/wp-json\/wp\/v2\/posts\/102\/revisions\/246"}],"wp:attachment":[{"href":"https:\/\/kylemcdonald.com.au\/wp-json\/wp\/v2\/media?parent=102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kylemcdonald.com.au\/wp-json\/wp\/v2\/categories?post=102"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kylemcdonald.com.au\/wp-json\/wp\/v2\/tags?post=102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}