{"id":129612,"date":"2009-11-25T03:00:00","date_gmt":"2009-11-25T08:00:00","guid":{"rendered":"tag:blogger.com,1999:blog-1652419620964346731.post-3717345632914950518"},"modified":"2009-11-25T03:00:00","modified_gmt":"2009-11-25T08:00:00","slug":"how-to-change-the-recovery-model-on-all-databases-at-once-in-sql-2005-management-studio","status":"publish","type":"post","link":"https:\/\/mereja.media\/index\/129612","title":{"rendered":"How to Change The Recovery Model On All Databases at Once in SQL 2005 Management Studio"},"content":{"rendered":"<p>In Microsoft SQL there are three ways to set your recovery model. Each method has their own pluses and minus. Each of them have their own reason for using them.<\/p>\n<p>The three recovery models are Full, Bulk-Logged and Simple. The first one, Full, is set by default in SQL 2005. According to <a href=\"http:\/\/www.sql-recovery.com\/database-recovery-models.html\">SQL-Recovery.com<\/a>, Full recovery model is:<\/p>\n<blockquote>\n<p><font color=\"#0000ff\">This is your best guarantee for full data recovery. The SQL Server fully logs all operations, so every row inserted through a bulk copy program (bcp) or BULK INSERT operation is written in its entirety to the transaction log. When data files are lost because of media failure the transaction log can be backed up.<\/font><\/p>\n<\/blockquote>\n<p>Bulk Logged:<\/p>\n<blockquote>\n<p><font color=\"#0000ff\">This model allows for recovery in case of media failure and gives you the best performance using the least log space for certain bulk operations, including BULK INSERT, bcp, CREATE INDEX, WRITETEXT, and UPDATETEXT.<\/font><\/p>\n<\/blockquote>\n<p>Simple:<\/p>\n<blockquote>\n<p><font color=\"#0000ff\">It allows for the fastest bulk operations and the simplest backup-and-restore strategy. Under this model, SQL Server truncates the transaction log at regular intervals, removing committed transactions. Only full database backups and differential backups are allowed.<\/font><\/p>\n<\/blockquote>\n<p>Changing the recovery model is as simple as right clicking on the database in SQL 2005 management Studio, Clicking on Properties, Clicking on Options, and selecting your recovery model from the drop down menu. This can be tedious though if you have multiple databases. Some web servers have 50 plus databases. Do you want to manually change each one? I didn\u2019t think so!<\/p>\n<p>Below is a simple <a href=\"http:\/\/en.wikipedia.org\/wiki\/Transact-SQL\">T-SQL<\/a> script you can run to change all of your databases to the the recovery model of choice!<\/p>\n<blockquote>\n<p><font color=\"#0000ff\" size=\"2\"><strong>USE master <\/strong><\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\"><strong>GO <\/strong><\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\"><strong>&#8212; Declare a variable to store the value [database name] returned by FETCH. <\/strong><\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\"><strong>DECLARE @dbname sysname, @cmd varchar(1000) <\/strong><\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\"><strong>&#8212; Declare a cursor to iterate through the list of databases <\/strong><\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\"><strong>DECLARE db_recovery_cursor CURSOR FOR <\/strong><\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\"><strong>SELECT name from sysdatabases <\/strong><\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\"><strong>&#8212; Open the cursor <\/strong><\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\"><strong>OPEN db_recovery_cursor <\/strong><\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\"><strong>&#8212; Perform the first fetch and store the value in a variable. <\/strong><\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\"><strong>FETCH NEXT FROM db_recovery_cursor INTO @dbname <\/strong><\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\"><strong>&#8212; loop through cursor until no more records fetched <\/strong><\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\"><strong>WHILE @@FETCH_STATUS = 0 <\/strong><\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\"><strong>BEGIN <\/strong><\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\"><strong>IF (SELECT DATABASEPROPERTYEX(@dbname,&#8217;RECOVERY&#8217;)) &lt;&gt; &#8216;&lt;RECOVERYMODEL&gt;&#8217; and @dbName &lt;&gt; &#8216;tempdb&#8217; BEGIN <\/strong><\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\"><strong>&#8212; create the alter database command for each database <\/strong><\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\"><strong>SET @cmd = &#8216;ALTER DATABASE &quot;&#8217; + @dbname + &#8216;&quot; SET RECOVERY &lt;RECOVERYMODEL&gt;&#8217; <\/strong><\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\"><strong>&#8212; alter each dataabase setting the recovery model to &lt;RECOVERYMODEL&gt; <\/strong><\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\"><strong>EXEC(@cmd) <\/strong><\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\"><strong>PRINT @dbname <\/strong><\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\"><strong>end <\/strong><\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\"><strong>FETCH NEXT FROM db_recovery_cursor INTO @dbname <\/strong><\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\"><strong>END <\/strong><\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\"><strong>&#8212; close the cursor and deallocate memory used by cursor <\/strong><\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\"><strong>CLOSE db_recovery_cursor <\/strong><\/font><\/p>\n<p><font color=\"#0000ff\" size=\"2\"><strong>DEALLOCATE db_recovery_cursor<\/strong><\/font><\/p>\n<\/blockquote>\n<p><strong><font color=\"#ff0000\">NOTE:<\/font><\/strong> Replace <strong>&lt;RECOVERYMODEL&gt;<\/strong> with your model of choice (I.E. SIMPLE, BULK-LOGGED, FULL)<\/p>\n<p>What recovery model do you use on your servers in your environment? Why? Hit us up in the comments!<\/p>\n<div style=\"padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px\" id=\"scid:0767317B-992E-4b12-91E0-4F059A8CECA8:da783b0c-0479-4401-a18a-bf4c019623d8\" class=\"wlWriterEditableSmartContent\">Technorati Tags: <a href=\"http:\/\/technorati.com\/tags\/sql+2005\" rel=\"tag\">sql 2005<\/a>,<a href=\"http:\/\/technorati.com\/tags\/how+to\" rel=\"tag\">how to<\/a>,<a href=\"http:\/\/technorati.com\/tags\/change\" rel=\"tag\">change<\/a>,<a href=\"http:\/\/technorati.com\/tags\/recovery+model\" rel=\"tag\">recovery model<\/a>,<a href=\"http:\/\/technorati.com\/tags\/multiple+databases\" rel=\"tag\">multiple databases<\/a>,<a href=\"http:\/\/technorati.com\/tags\/script\" rel=\"tag\">script<\/a><\/div>\n<div class=\"blogger-post-footer\"><Br \/><center><a href=\"http:\/\/www.anrdoezrs.net\/click-3773910-10438541?cm_mmc=CJ-_-2769020-_-3773910-_-Home_468x60-Anim_8-24-2006\" ><br \/>\n<img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/www.ftjcfx.com\/image-3773910-10438541\" width=\"468\" height=\"60\" alt=\"Great Deals @ Geeks.com!\" border=\"0\"\/><\/a><\/center><img width='1' height='1' src='https:\/\/blogger.googleusercontent.com\/tracker\/1652419620964346731-3717345632914950518?l=www.bauer-power.net' alt='' \/><\/div>\n<div class=\"feedflare\">\n<a href=\"http:\/\/feeds.feedburner.com\/~ff\/Bauer-power?a=ALwRd4WWTOI:1KQHg0bEwoY:yIl2AUoC8zA\"><img decoding=\"async\" src=\"http:\/\/feeds.feedburner.com\/~ff\/Bauer-power?d=yIl2AUoC8zA\" border=\"0\"><\/img><\/a> <a href=\"http:\/\/feeds.feedburner.com\/~ff\/Bauer-power?a=ALwRd4WWTOI:1KQHg0bEwoY:dnMXMwOfBR0\"><img decoding=\"async\" src=\"http:\/\/feeds.feedburner.com\/~ff\/Bauer-power?d=dnMXMwOfBR0\" border=\"0\"><\/img><\/a> <a href=\"http:\/\/feeds.feedburner.com\/~ff\/Bauer-power?a=ALwRd4WWTOI:1KQHg0bEwoY:V_sGLiPBpWU\"><img decoding=\"async\" src=\"http:\/\/feeds.feedburner.com\/~ff\/Bauer-power?i=ALwRd4WWTOI:1KQHg0bEwoY:V_sGLiPBpWU\" border=\"0\"><\/img><\/a> <a href=\"http:\/\/feeds.feedburner.com\/~ff\/Bauer-power?a=ALwRd4WWTOI:1KQHg0bEwoY:gIN9vFwOqvQ\"><img decoding=\"async\" src=\"http:\/\/feeds.feedburner.com\/~ff\/Bauer-power?i=ALwRd4WWTOI:1KQHg0bEwoY:gIN9vFwOqvQ\" border=\"0\"><\/img><\/a> <a href=\"http:\/\/feeds.feedburner.com\/~ff\/Bauer-power?a=ALwRd4WWTOI:1KQHg0bEwoY:YwkR-u9nhCs\"><img decoding=\"async\" src=\"http:\/\/feeds.feedburner.com\/~ff\/Bauer-power?d=YwkR-u9nhCs\" border=\"0\"><\/img><\/a> <a href=\"http:\/\/feeds.feedburner.com\/~ff\/Bauer-power?a=ALwRd4WWTOI:1KQHg0bEwoY:F7zBnMyn0Lo\"><img decoding=\"async\" src=\"http:\/\/feeds.feedburner.com\/~ff\/Bauer-power?i=ALwRd4WWTOI:1KQHg0bEwoY:F7zBnMyn0Lo\" border=\"0\"><\/img><\/a> <a href=\"http:\/\/feeds.feedburner.com\/~ff\/Bauer-power?a=ALwRd4WWTOI:1KQHg0bEwoY:wF9xT3WuBAs\"><img decoding=\"async\" src=\"http:\/\/feeds.feedburner.com\/~ff\/Bauer-power?i=ALwRd4WWTOI:1KQHg0bEwoY:wF9xT3WuBAs\" border=\"0\"><\/img><\/a> <a href=\"http:\/\/feeds.feedburner.com\/~ff\/Bauer-power?a=ALwRd4WWTOI:1KQHg0bEwoY:jTJX5pvEcCQ\"><img decoding=\"async\" src=\"http:\/\/feeds.feedburner.com\/~ff\/Bauer-power?i=ALwRd4WWTOI:1KQHg0bEwoY:jTJX5pvEcCQ\" border=\"0\"><\/img><\/a> <a href=\"http:\/\/feeds.feedburner.com\/~ff\/Bauer-power?a=ALwRd4WWTOI:1KQHg0bEwoY:TzevzKxY174\"><img decoding=\"async\" src=\"http:\/\/feeds.feedburner.com\/~ff\/Bauer-power?d=TzevzKxY174\" border=\"0\"><\/img><\/a> <a href=\"http:\/\/feeds.feedburner.com\/~ff\/Bauer-power?a=ALwRd4WWTOI:1KQHg0bEwoY:qj6IDK7rITs\"><img decoding=\"async\" src=\"http:\/\/feeds.feedburner.com\/~ff\/Bauer-power?d=qj6IDK7rITs\" border=\"0\"><\/img><\/a> <a href=\"http:\/\/feeds.feedburner.com\/~ff\/Bauer-power?a=ALwRd4WWTOI:1KQHg0bEwoY:7Q72WNTAKBA\"><img decoding=\"async\" src=\"http:\/\/feeds.feedburner.com\/~ff\/Bauer-power?d=7Q72WNTAKBA\" border=\"0\"><\/img><\/a> <a href=\"http:\/\/feeds.feedburner.com\/~ff\/Bauer-power?a=ALwRd4WWTOI:1KQHg0bEwoY:nHK9nIK91uw\"><img decoding=\"async\" src=\"http:\/\/feeds.feedburner.com\/~ff\/Bauer-power?i=ALwRd4WWTOI:1KQHg0bEwoY:nHK9nIK91uw\" border=\"0\"><\/img><\/a>\n<\/div>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/feeds.feedburner.com\/~r\/Bauer-power\/~4\/ALwRd4WWTOI\" height=\"1\" width=\"1\"\/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Microsoft SQL there are three ways to set your recovery model. Each method has their own pluses and minus. Each of them have their own reason for using them. The three recovery models are Full, Bulk-Logged and Simple. The first one, Full, is set by default in SQL 2005. According to SQL-Recovery.com, Full recovery [&hellip;]<\/p>\n","protected":false},"author":1521,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-129612","post","type-post","status-publish","format-standard","hentry","category-news"],"_links":{"self":[{"href":"https:\/\/mereja.media\/index\/wp-json\/wp\/v2\/posts\/129612","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mereja.media\/index\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mereja.media\/index\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mereja.media\/index\/wp-json\/wp\/v2\/users\/1521"}],"replies":[{"embeddable":true,"href":"https:\/\/mereja.media\/index\/wp-json\/wp\/v2\/comments?post=129612"}],"version-history":[{"count":0,"href":"https:\/\/mereja.media\/index\/wp-json\/wp\/v2\/posts\/129612\/revisions"}],"wp:attachment":[{"href":"https:\/\/mereja.media\/index\/wp-json\/wp\/v2\/media?parent=129612"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mereja.media\/index\/wp-json\/wp\/v2\/categories?post=129612"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mereja.media\/index\/wp-json\/wp\/v2\/tags?post=129612"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}