Solved

SQL 2012: AG: drop database on secondary error: "ALTER DATABASE is not permitted while a database is in the Restoring state."

Posted on 2016-11-08
4
53 Views
Last Modified: 2016-11-21
when I do below process, I get error: ALTER DATABASE is not permitted while a database is in the Restoring state.
I run the process again and it seems to work OK, most of the time.. is there a way to foolproof this?
~~~
--PRIMARY NODE
USE MASTER

IF
(
SELECT COUNT(*) FROM sys.availability_databases_cluster WHERE database_name = 'database_one'
)
=1

ALTER AVAILABILITY GROUP [POS1AG] REMOVE DATABASE [database_one];
GO


 

--SECONDDARY NODE
GO
  WAITFOR DELAY '00:01'
/* WAIT ONE MINUTE for sync */
GO
IF  EXISTS
(SELECT name FROM master.sys.databases WHERE name = 'database_one')
BEGIN
        ALTER DATABASE database_one  SET SINGLE_USER WITH  ROLLBACK IMMEDIATE
        DROP  DATABASE database_one  
END
0
Comment
Question by:25112
  • 2
  • 2
4 Comments
 
LVL 24

Expert Comment

by:Pawan Kumar
ID: 41878887
Try. this...

IF  EXISTS 
(SELECT name FROM master.sys.databases WHERE name = 'database_one')
BEGIN
        USE master


        ALTER DATABASE database_one  SET SINGLE_USER WITH  ROLLBACK IMMEDIATE
        DROP  DATABASE database_one  
END

Open in new window

0
 
LVL 5

Author Comment

by:25112
ID: 41879065
hallo- I see you avoid the WAITFOR and added the USE line.. do you think it will help the time lapse between when we remove database from primary:
ALTER AVAILABILITY GROUP [POS1AG] REMOVE DATABASE

and the time the databases are ready to be dropped in secondary?
0
 
LVL 24

Accepted Solution

by:
Pawan Kumar earned 500 total points
ID: 41879868
Is this the primary or secondary ? In this case it looks like it is not yet removed from  the availability group.

Try increasing the time to 2 minutes and then lets see.
0
 
LVL 5

Author Comment

by:25112
ID: 41880718
I tried twice with 2 minutes.. one time it worked, other time it still gave the error..

can this be done in a while loop, so that as soon as the database is freed up to be dropped it can be? is it possible?
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now