SQL query to check if secondary database is removed from availability group





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







2















I have an AG with one primary replica and one secondary replica.
I have created a script that queries transaction log backups from these database replicas and restores them to a target database.



Recently one user removed one secondary database from the AG and started using it as a standalone database as explained here https://docs.microsoft.com/en-us/sql/database-engine/availability-groups/windows/remove-a-secondary-database-from-an-availability-group-sql-server?view=sql-server-2017. The user also started a backup chain of this database.



The script now fails to restore as the recovery fork guid has changed on the secondary database.



Is there a way to detect if a database has left the AG so that in the script while I query the backupsets, I can skip the database if it is no longer part of the AG?



Thanks!










share|improve this question































    2















    I have an AG with one primary replica and one secondary replica.
    I have created a script that queries transaction log backups from these database replicas and restores them to a target database.



    Recently one user removed one secondary database from the AG and started using it as a standalone database as explained here https://docs.microsoft.com/en-us/sql/database-engine/availability-groups/windows/remove-a-secondary-database-from-an-availability-group-sql-server?view=sql-server-2017. The user also started a backup chain of this database.



    The script now fails to restore as the recovery fork guid has changed on the secondary database.



    Is there a way to detect if a database has left the AG so that in the script while I query the backupsets, I can skip the database if it is no longer part of the AG?



    Thanks!










    share|improve this question



























      2












      2








      2








      I have an AG with one primary replica and one secondary replica.
      I have created a script that queries transaction log backups from these database replicas and restores them to a target database.



      Recently one user removed one secondary database from the AG and started using it as a standalone database as explained here https://docs.microsoft.com/en-us/sql/database-engine/availability-groups/windows/remove-a-secondary-database-from-an-availability-group-sql-server?view=sql-server-2017. The user also started a backup chain of this database.



      The script now fails to restore as the recovery fork guid has changed on the secondary database.



      Is there a way to detect if a database has left the AG so that in the script while I query the backupsets, I can skip the database if it is no longer part of the AG?



      Thanks!










      share|improve this question
















      I have an AG with one primary replica and one secondary replica.
      I have created a script that queries transaction log backups from these database replicas and restores them to a target database.



      Recently one user removed one secondary database from the AG and started using it as a standalone database as explained here https://docs.microsoft.com/en-us/sql/database-engine/availability-groups/windows/remove-a-secondary-database-from-an-availability-group-sql-server?view=sql-server-2017. The user also started a backup chain of this database.



      The script now fails to restore as the recovery fork guid has changed on the secondary database.



      Is there a way to detect if a database has left the AG so that in the script while I query the backupsets, I can skip the database if it is no longer part of the AG?



      Thanks!







      sql-server backup availability-groups






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Feb 15 at 21:13









      Tony Hinkle

      3,0951725




      3,0951725










      asked Feb 15 at 20:40









      S.K.S.K.

      1135




      1135






















          2 Answers
          2






          active

          oldest

          votes


















          2














          You could use exists with the sys.dm_hadr_database_replica_states DMV to see if the DB is in a AG.




          Returns a row for each database that is participating in an Always On
          availability group for which the local instance of SQL Server is
          hosting an availability replica. This dynamic management view exposes
          state information on both the primary and secondary replicas. On a
          secondary replica, this view returns a row for every secondary
          database on the server instance. On the primary replica, this view
          returns a row for each primary database and an additional row for the
          corresponding secondary database




          Here is the query:



          if exists(
          select
          DBName = db_name(s.database_id)
          ,s.is_local
          ,s.synchronization_state_desc
          ,s.synchronization_health_desc
          from sys.dm_hadr_database_replica_states s
          where db_name(s.database_id) = 'your_database'
          )
          begin
          --do something useful
          end
          else
          begin
          --exit, probably without error
          end


          This would need to query against the secondary if you aren't on 2014+. If you are, then you can add s.is_primary_replica = 0 to the query. Or you can use this method or a similar approach.






          share|improve this answer


























          • Thanks. The problem with this query is that NOT_HEALTHY state can be due to a lot of reasons and can be temporary too. A check on it will not address the situation I have described.

            – S.K.
            Feb 15 at 21:03











          • I'm not checking for the state, though. If your DB isn't listed in the DMV it isn't part of an AG. Maybe I missed something?

            – scsimon
            Feb 15 at 21:04











          • It will still be in the AG on the primary if the secondary replica was removed from the AG.

            – Tony Hinkle
            Feb 15 at 21:16











          • Right @TonyHinkle which is why I said you need to run it against the secondary or use the flag if possible.

            – scsimon
            Feb 15 at 21:40






          • 1





            @scsimon Yep--good work. Your query is lighter weight than mine, also.

            – Tony Hinkle
            Feb 15 at 21:50



















          1














          You can use the following query to determine if a given database has a replica on a given server. Replace the placeholders in the WHERE clause to specify the database and server you are checking and run it on the primary.



          SELECT 
          *
          FROM
          sys.dm_hadr_availability_replica_states rs
          JOIN sys.dm_hadr_database_replica_cluster_states dcs ON dcs.replica_id = rs.replica_id
          JOIN sys.dm_hadr_availability_replica_cluster_states acs ON acs.replica_id = rs.replica_id
          WHERE
          dcs.database_name = '<databasename>'
          AND acs.replica_server_name = '<servername>'





          share|improve this answer


























            Your Answer








            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "182"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f229895%2fsql-query-to-check-if-secondary-database-is-removed-from-availability-group%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            2














            You could use exists with the sys.dm_hadr_database_replica_states DMV to see if the DB is in a AG.




            Returns a row for each database that is participating in an Always On
            availability group for which the local instance of SQL Server is
            hosting an availability replica. This dynamic management view exposes
            state information on both the primary and secondary replicas. On a
            secondary replica, this view returns a row for every secondary
            database on the server instance. On the primary replica, this view
            returns a row for each primary database and an additional row for the
            corresponding secondary database




            Here is the query:



            if exists(
            select
            DBName = db_name(s.database_id)
            ,s.is_local
            ,s.synchronization_state_desc
            ,s.synchronization_health_desc
            from sys.dm_hadr_database_replica_states s
            where db_name(s.database_id) = 'your_database'
            )
            begin
            --do something useful
            end
            else
            begin
            --exit, probably without error
            end


            This would need to query against the secondary if you aren't on 2014+. If you are, then you can add s.is_primary_replica = 0 to the query. Or you can use this method or a similar approach.






            share|improve this answer


























            • Thanks. The problem with this query is that NOT_HEALTHY state can be due to a lot of reasons and can be temporary too. A check on it will not address the situation I have described.

              – S.K.
              Feb 15 at 21:03











            • I'm not checking for the state, though. If your DB isn't listed in the DMV it isn't part of an AG. Maybe I missed something?

              – scsimon
              Feb 15 at 21:04











            • It will still be in the AG on the primary if the secondary replica was removed from the AG.

              – Tony Hinkle
              Feb 15 at 21:16











            • Right @TonyHinkle which is why I said you need to run it against the secondary or use the flag if possible.

              – scsimon
              Feb 15 at 21:40






            • 1





              @scsimon Yep--good work. Your query is lighter weight than mine, also.

              – Tony Hinkle
              Feb 15 at 21:50
















            2














            You could use exists with the sys.dm_hadr_database_replica_states DMV to see if the DB is in a AG.




            Returns a row for each database that is participating in an Always On
            availability group for which the local instance of SQL Server is
            hosting an availability replica. This dynamic management view exposes
            state information on both the primary and secondary replicas. On a
            secondary replica, this view returns a row for every secondary
            database on the server instance. On the primary replica, this view
            returns a row for each primary database and an additional row for the
            corresponding secondary database




            Here is the query:



            if exists(
            select
            DBName = db_name(s.database_id)
            ,s.is_local
            ,s.synchronization_state_desc
            ,s.synchronization_health_desc
            from sys.dm_hadr_database_replica_states s
            where db_name(s.database_id) = 'your_database'
            )
            begin
            --do something useful
            end
            else
            begin
            --exit, probably without error
            end


            This would need to query against the secondary if you aren't on 2014+. If you are, then you can add s.is_primary_replica = 0 to the query. Or you can use this method or a similar approach.






            share|improve this answer


























            • Thanks. The problem with this query is that NOT_HEALTHY state can be due to a lot of reasons and can be temporary too. A check on it will not address the situation I have described.

              – S.K.
              Feb 15 at 21:03











            • I'm not checking for the state, though. If your DB isn't listed in the DMV it isn't part of an AG. Maybe I missed something?

              – scsimon
              Feb 15 at 21:04











            • It will still be in the AG on the primary if the secondary replica was removed from the AG.

              – Tony Hinkle
              Feb 15 at 21:16











            • Right @TonyHinkle which is why I said you need to run it against the secondary or use the flag if possible.

              – scsimon
              Feb 15 at 21:40






            • 1





              @scsimon Yep--good work. Your query is lighter weight than mine, also.

              – Tony Hinkle
              Feb 15 at 21:50














            2












            2








            2







            You could use exists with the sys.dm_hadr_database_replica_states DMV to see if the DB is in a AG.




            Returns a row for each database that is participating in an Always On
            availability group for which the local instance of SQL Server is
            hosting an availability replica. This dynamic management view exposes
            state information on both the primary and secondary replicas. On a
            secondary replica, this view returns a row for every secondary
            database on the server instance. On the primary replica, this view
            returns a row for each primary database and an additional row for the
            corresponding secondary database




            Here is the query:



            if exists(
            select
            DBName = db_name(s.database_id)
            ,s.is_local
            ,s.synchronization_state_desc
            ,s.synchronization_health_desc
            from sys.dm_hadr_database_replica_states s
            where db_name(s.database_id) = 'your_database'
            )
            begin
            --do something useful
            end
            else
            begin
            --exit, probably without error
            end


            This would need to query against the secondary if you aren't on 2014+. If you are, then you can add s.is_primary_replica = 0 to the query. Or you can use this method or a similar approach.






            share|improve this answer















            You could use exists with the sys.dm_hadr_database_replica_states DMV to see if the DB is in a AG.




            Returns a row for each database that is participating in an Always On
            availability group for which the local instance of SQL Server is
            hosting an availability replica. This dynamic management view exposes
            state information on both the primary and secondary replicas. On a
            secondary replica, this view returns a row for every secondary
            database on the server instance. On the primary replica, this view
            returns a row for each primary database and an additional row for the
            corresponding secondary database




            Here is the query:



            if exists(
            select
            DBName = db_name(s.database_id)
            ,s.is_local
            ,s.synchronization_state_desc
            ,s.synchronization_health_desc
            from sys.dm_hadr_database_replica_states s
            where db_name(s.database_id) = 'your_database'
            )
            begin
            --do something useful
            end
            else
            begin
            --exit, probably without error
            end


            This would need to query against the secondary if you aren't on 2014+. If you are, then you can add s.is_primary_replica = 0 to the query. Or you can use this method or a similar approach.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Feb 15 at 20:58

























            answered Feb 15 at 20:52









            scsimonscsimon

            1,403415




            1,403415













            • Thanks. The problem with this query is that NOT_HEALTHY state can be due to a lot of reasons and can be temporary too. A check on it will not address the situation I have described.

              – S.K.
              Feb 15 at 21:03











            • I'm not checking for the state, though. If your DB isn't listed in the DMV it isn't part of an AG. Maybe I missed something?

              – scsimon
              Feb 15 at 21:04











            • It will still be in the AG on the primary if the secondary replica was removed from the AG.

              – Tony Hinkle
              Feb 15 at 21:16











            • Right @TonyHinkle which is why I said you need to run it against the secondary or use the flag if possible.

              – scsimon
              Feb 15 at 21:40






            • 1





              @scsimon Yep--good work. Your query is lighter weight than mine, also.

              – Tony Hinkle
              Feb 15 at 21:50



















            • Thanks. The problem with this query is that NOT_HEALTHY state can be due to a lot of reasons and can be temporary too. A check on it will not address the situation I have described.

              – S.K.
              Feb 15 at 21:03











            • I'm not checking for the state, though. If your DB isn't listed in the DMV it isn't part of an AG. Maybe I missed something?

              – scsimon
              Feb 15 at 21:04











            • It will still be in the AG on the primary if the secondary replica was removed from the AG.

              – Tony Hinkle
              Feb 15 at 21:16











            • Right @TonyHinkle which is why I said you need to run it against the secondary or use the flag if possible.

              – scsimon
              Feb 15 at 21:40






            • 1





              @scsimon Yep--good work. Your query is lighter weight than mine, also.

              – Tony Hinkle
              Feb 15 at 21:50

















            Thanks. The problem with this query is that NOT_HEALTHY state can be due to a lot of reasons and can be temporary too. A check on it will not address the situation I have described.

            – S.K.
            Feb 15 at 21:03





            Thanks. The problem with this query is that NOT_HEALTHY state can be due to a lot of reasons and can be temporary too. A check on it will not address the situation I have described.

            – S.K.
            Feb 15 at 21:03













            I'm not checking for the state, though. If your DB isn't listed in the DMV it isn't part of an AG. Maybe I missed something?

            – scsimon
            Feb 15 at 21:04





            I'm not checking for the state, though. If your DB isn't listed in the DMV it isn't part of an AG. Maybe I missed something?

            – scsimon
            Feb 15 at 21:04













            It will still be in the AG on the primary if the secondary replica was removed from the AG.

            – Tony Hinkle
            Feb 15 at 21:16





            It will still be in the AG on the primary if the secondary replica was removed from the AG.

            – Tony Hinkle
            Feb 15 at 21:16













            Right @TonyHinkle which is why I said you need to run it against the secondary or use the flag if possible.

            – scsimon
            Feb 15 at 21:40





            Right @TonyHinkle which is why I said you need to run it against the secondary or use the flag if possible.

            – scsimon
            Feb 15 at 21:40




            1




            1





            @scsimon Yep--good work. Your query is lighter weight than mine, also.

            – Tony Hinkle
            Feb 15 at 21:50





            @scsimon Yep--good work. Your query is lighter weight than mine, also.

            – Tony Hinkle
            Feb 15 at 21:50













            1














            You can use the following query to determine if a given database has a replica on a given server. Replace the placeholders in the WHERE clause to specify the database and server you are checking and run it on the primary.



            SELECT 
            *
            FROM
            sys.dm_hadr_availability_replica_states rs
            JOIN sys.dm_hadr_database_replica_cluster_states dcs ON dcs.replica_id = rs.replica_id
            JOIN sys.dm_hadr_availability_replica_cluster_states acs ON acs.replica_id = rs.replica_id
            WHERE
            dcs.database_name = '<databasename>'
            AND acs.replica_server_name = '<servername>'





            share|improve this answer






























              1














              You can use the following query to determine if a given database has a replica on a given server. Replace the placeholders in the WHERE clause to specify the database and server you are checking and run it on the primary.



              SELECT 
              *
              FROM
              sys.dm_hadr_availability_replica_states rs
              JOIN sys.dm_hadr_database_replica_cluster_states dcs ON dcs.replica_id = rs.replica_id
              JOIN sys.dm_hadr_availability_replica_cluster_states acs ON acs.replica_id = rs.replica_id
              WHERE
              dcs.database_name = '<databasename>'
              AND acs.replica_server_name = '<servername>'





              share|improve this answer




























                1












                1








                1







                You can use the following query to determine if a given database has a replica on a given server. Replace the placeholders in the WHERE clause to specify the database and server you are checking and run it on the primary.



                SELECT 
                *
                FROM
                sys.dm_hadr_availability_replica_states rs
                JOIN sys.dm_hadr_database_replica_cluster_states dcs ON dcs.replica_id = rs.replica_id
                JOIN sys.dm_hadr_availability_replica_cluster_states acs ON acs.replica_id = rs.replica_id
                WHERE
                dcs.database_name = '<databasename>'
                AND acs.replica_server_name = '<servername>'





                share|improve this answer















                You can use the following query to determine if a given database has a replica on a given server. Replace the placeholders in the WHERE clause to specify the database and server you are checking and run it on the primary.



                SELECT 
                *
                FROM
                sys.dm_hadr_availability_replica_states rs
                JOIN sys.dm_hadr_database_replica_cluster_states dcs ON dcs.replica_id = rs.replica_id
                JOIN sys.dm_hadr_availability_replica_cluster_states acs ON acs.replica_id = rs.replica_id
                WHERE
                dcs.database_name = '<databasename>'
                AND acs.replica_server_name = '<servername>'






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Feb 15 at 21:48

























                answered Feb 15 at 21:32









                Tony HinkleTony Hinkle

                3,0951725




                3,0951725






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Database Administrators Stack Exchange!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f229895%2fsql-query-to-check-if-secondary-database-is-removed-from-availability-group%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Human spaceflight

                    Can not write log (Is /dev/pts mounted?) - openpty in Ubuntu-on-Windows?

                    張江高科駅