{"id":227,"date":"2016-01-27T13:07:50","date_gmt":"2016-01-27T19:07:50","guid":{"rendered":"http:\/\/www.sqlkitten.com\/?p=227"},"modified":"2016-01-27T13:07:50","modified_gmt":"2016-01-27T19:07:50","slug":"cannot-drop-the-database-because-it-is-being-used-for-replication-the-right-fix-and-the-long-way","status":"publish","type":"post","link":"http:\/\/www.sqlkitten.com\/?p=227","title":{"rendered":"Cannot drop the database because it is being used for replication &#8211; the Right Fix and the Long Way"},"content":{"rendered":"<p>One of the things I am still trying to get past when it comes to writing about tech is the fact that I can&#8217;t always be completely original. I can write about different experiences and scripts but in many cases someone will have already written about the same thing. Part of tech blogging is realizing that even if someone else has already written about something you want to write about WRITE ABOUT IT ANY WAY. Maybe you had a task that was slightly different from what others have written about or maybe you can embellish on something they left out. Bottom line, you can be creative and reach people.<\/p>\n<p>Another part of tech blogging is the fact that sadly there are people blogging about the <span style=\"text-decoration: line-through;\">wrong<\/span> other ways to resolve certain issues. Your parents told you not to believe everything you read on the internet &#8211; are they liars or is that &#8220;expert&#8221; telling you the best way to do backups is with maintenance plans the one serving up the bull?<\/p>\n<p>I had been asked to detach a database. The person doing it couldn&#8217;t and tossed it to me. Cool. No big deal. Then I ran the script to detach and got the error message\u00a03724 &#8211; Cannot drop the database because it is being used for replication.<\/p>\n<div id=\"attachment_242\" style=\"width: 1239px\" class=\"wp-caption alignnone\"><a href=\"http:\/\/www.sqlkitten.com\/?attachment_id=242\" rel=\"attachment wp-att-242\"><img aria-describedby=\"caption-attachment-242\" loading=\"lazy\" class=\"wp-image-242 size-full\" src=\"http:\/\/www.sqlkitten.com\/wp-content\/uploads\/2016\/01\/detach-1.jpg\" alt=\"Well, isn't that special.\" width=\"1229\" height=\"337\" srcset=\"http:\/\/www.sqlkitten.com\/wp-content\/uploads\/2016\/01\/detach-1.jpg 1229w, http:\/\/www.sqlkitten.com\/wp-content\/uploads\/2016\/01\/detach-1-300x82.jpg 300w, http:\/\/www.sqlkitten.com\/wp-content\/uploads\/2016\/01\/detach-1-768x211.jpg 768w, http:\/\/www.sqlkitten.com\/wp-content\/uploads\/2016\/01\/detach-1-1024x281.jpg 1024w, http:\/\/www.sqlkitten.com\/wp-content\/uploads\/2016\/01\/detach-1-800x219.jpg 800w\" sizes=\"(max-width: 1229px) 100vw, 1229px\" \/><\/a><p id=\"caption-attachment-242\" class=\"wp-caption-text\">Well, isn&#8217;t that special.<\/p><\/div>\n<p>Was there replication? Yes. Were there any publications associated with this database? There used to be but not any more (at least not visible at the publisher server in SSMS). There were some other issues that needed resolving but those had nothing to do with this situation. I pop over to my other screen and start looking for the right scripted solution (I figure I will have to do this again so better that I save it off to a script with some notes).<\/p>\n<p>First, I want to show that the database is enabled for replication &#8211; <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/ms174389.aspx\" target=\"_blank\">sp_helpreplicationdb<\/a> is going to give me this information.<\/p>\n<div id=\"attachment_239\" style=\"width: 745px\" class=\"wp-caption alignnone\"><a href=\"http:\/\/www.sqlkitten.com\/?attachment_id=239\" rel=\"attachment wp-att-239\"><img aria-describedby=\"caption-attachment-239\" loading=\"lazy\" class=\"wp-image-239 size-full\" src=\"http:\/\/www.sqlkitten.com\/wp-content\/uploads\/2016\/01\/help.jpg\" alt=\"help\" width=\"735\" height=\"343\" srcset=\"http:\/\/www.sqlkitten.com\/wp-content\/uploads\/2016\/01\/help.jpg 735w, http:\/\/www.sqlkitten.com\/wp-content\/uploads\/2016\/01\/help-300x140.jpg 300w\" sizes=\"(max-width: 735px) 100vw, 735px\" \/><\/a><p id=\"caption-attachment-239\" class=\"wp-caption-text\">Ok. Now what?<\/p><\/div>\n<p>Based on the results from sp_helpreplicationdb\u00a0, I now have confirmation that my database is at least enabled for replication. The next thing I need to do is turn off replication for this database with <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/ms188734.aspx\" target=\"_blank\">sp_removedbreplication<\/a>.<\/p>\n<div id=\"attachment_240\" style=\"width: 796px\" class=\"wp-caption alignnone\"><a href=\"http:\/\/www.sqlkitten.com\/?attachment_id=240\" rel=\"attachment wp-att-240\"><img aria-describedby=\"caption-attachment-240\" loading=\"lazy\" class=\"wp-image-240 size-full\" src=\"http:\/\/www.sqlkitten.com\/wp-content\/uploads\/2016\/01\/right_answer.jpg\" alt=\"Fixed - WITH ONE LINE OF CODE!!!\" width=\"786\" height=\"150\" srcset=\"http:\/\/www.sqlkitten.com\/wp-content\/uploads\/2016\/01\/right_answer.jpg 786w, http:\/\/www.sqlkitten.com\/wp-content\/uploads\/2016\/01\/right_answer-300x57.jpg 300w, http:\/\/www.sqlkitten.com\/wp-content\/uploads\/2016\/01\/right_answer-768x147.jpg 768w\" sizes=\"(max-width: 786px) 100vw, 786px\" \/><\/a><p id=\"caption-attachment-240\" class=\"wp-caption-text\">Fixed &#8211; WITH ONE LINE OF CODE!!!<\/p><\/div>\n<p>After ran this I was able to detach the database. And all was right with the world. On Saturday night. Did I mention this was on Saturday night? I&#8217;m a party animal.<\/p>\n<p>Anyways, back to the tale of the long way to do this&#8230;after my search I had opened a few of the results in new tabs in my browser. One of them was <a href=\"http:\/\/www.sqlservercentral.com\/Forums\/Topic115333-7-1.aspx\" target=\"_blank\">here<\/a>. After a little more searching based on this link I had what I needed with the right syntax. I resolved the issue but when I was done I still had to go back and close the new tabs I had just opened. It was then that I saw a completely different solution. While this other solution may have allowed you to drop the database, it required far more steps than the one I presented above. Also, in my case, I did not want to drop the database, just detach it. Restoring the database from a backup that contains no data wipes out my database, and was not part of the requirement. So essentially, to be able to detach the database I would have to run sp_helpreplicationdb and sp_removedbreplication OR\u00a0I would have to take a backup, validate that all the data that was needed was in the backup, create the other database, backup that database, copy the backup file, restore the backup with replace over the database I am trying to drop, and then detach or drop the database. In this case, the desired end result was to migrate the database files to a new server and attach them. This would be changed to restoring from the backup of the original database. If you have a case where you are having to do this for multiple databases, this would be multiplied.<\/p>\n<p>Frustrated after finding this, I took to twitter.<\/p>\n<blockquote class=\"twitter-tweet\" lang=\"en\">\n<p dir=\"ltr\" lang=\"en\">Always interesting to find a blog post with a bassakwards, heavy handed answer to a SQL problem with the right solution in the comments.<\/p>\n<p>\u2014 Amy (@texasamy) <a href=\"https:\/\/twitter.com\/texasamy\/status\/691104528523853824\">January 24, 2016<\/a><\/p><\/blockquote>\n<p><script src=\"\/\/platform.twitter.com\/widgets.js\" async=\"\" charset=\"utf-8\"><\/script><br \/>\nI decided I would do the right thing and give the internet one more page with the RIGHT SOLUTION. While we can&#8217;t stop people from posting the longer, less practical solutions to things we can post the right ones and keep posting them.<\/p>\n<div id=\"attachment_230\" style=\"width: 310px\" class=\"wp-caption alignnone\"><a href=\"http:\/\/www.sqlkitten.com\/?attachment_id=230\" rel=\"attachment wp-att-230\"><img aria-describedby=\"caption-attachment-230\" loading=\"lazy\" class=\"wp-image-230 size-medium\" src=\"http:\/\/www.sqlkitten.com\/wp-content\/uploads\/2016\/01\/candy-300x172.jpg\" alt=\"candy\" width=\"300\" height=\"172\" srcset=\"http:\/\/www.sqlkitten.com\/wp-content\/uploads\/2016\/01\/candy-300x172.jpg 300w, http:\/\/www.sqlkitten.com\/wp-content\/uploads\/2016\/01\/candy.jpg 504w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-230\" class=\"wp-caption-text\">They might have a few pieces of candy left.<\/p><\/div>\n<p>Now that we have established the internet lies to you, go call your mother.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the things I am still trying to get past when it comes to writing about tech is the fact that I can&#8217;t always be completely original. I can write about different experiences and scripts but in many cases &hellip; <a href=\"http:\/\/www.sqlkitten.com\/?p=227\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"http:\/\/www.sqlkitten.com\/index.php?rest_route=\/wp\/v2\/posts\/227"}],"collection":[{"href":"http:\/\/www.sqlkitten.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.sqlkitten.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.sqlkitten.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.sqlkitten.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=227"}],"version-history":[{"count":10,"href":"http:\/\/www.sqlkitten.com\/index.php?rest_route=\/wp\/v2\/posts\/227\/revisions"}],"predecessor-version":[{"id":248,"href":"http:\/\/www.sqlkitten.com\/index.php?rest_route=\/wp\/v2\/posts\/227\/revisions\/248"}],"wp:attachment":[{"href":"http:\/\/www.sqlkitten.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=227"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.sqlkitten.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=227"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.sqlkitten.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=227"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}