• SQL Server
  • Log Shipping Tricks Demo
  • SQLCruise Alaska 2012 Pics
SQLSoldier News From the Frontlines

T-SQL Tuesday #21 – Bringing Ugly Back

August 10, 2011 11:57 am / Leave a Comment / SQLSoldier

T-SQL Tuesday #21 – Bringing Ugly Back

T-SQL Tuesday #21 - I'm Bringing Ugly Back


This blog entry is participating in T-SQL Tuesday #21, hosted this month by Adam Machanic (Blog|@AdamMachanic). You are invited to visit his blog to join the blog party. You are welcome to write your own participating blog post for the party or just to read more blogs participating in this month’s theme: Your Crap Code.

The truth is, we all write crap code from time to time. Over time, we get better (hopefully). We learn that certain things are almost always bad (Select *, RBAR — Row By Agonizing Row) while other things are okay in certain situations but not good for everyday use (clustering on GUIDs, cursors). Or sometimes we write good code that does bad things. Today, I want to talk about a piece of good code that I wrote that does something bad. In fact, this code was even included in my Mirroring book. Even though the code does a bad thing, at the time that I wrote it, it was a very necessary thing to address a particularly heinous bug in database mirroring. I wrote and promoted a stored procedure to …….. routinely shrink the database log file.

Bringing Ugly Back

I'm Bringing Ugly Back

Bringing Ugly Back


The bug I mentioned is documented under KB article KB937531. In this situation, if your log file grew too large, and both partners were rebooted, the initial synchronization check would fail and both partners would be taken offline. In this scenario, the only way to get a partner online again is to drop mirroring and bring the principal online. This bug was fixed in CU6 for SQL Server 2005 SP2, so this is no longer needed, but at the time, it was needed.

Below is the offensive code snippet. The full procedure can be downloaded here: dba_ShrinkMirroredDatabases.sql (4.5 KB).

IF @NewFileSize < @FileSize 
              BEGIN 
                SET @SQL = 'Alter Database ' + QUOTENAME(@DBName) + 
                           ' Modify File (name = ' + @FileName + 
                           ', size = ' + 
                           CAST(@NewFileSize AS NVARCHAR) + 'KB);' 

                IF @Debug = 0 
                  BEGIN 
                    EXEC sp_executesql @SQL 
                  END 
                ELSE 
                  BEGIN 
                    PRINT @SQL 
                  END 
                END 
              END 

      SET @CurrID = @CurrID + 1 
    END 
  END 
Posted in: SQL Server / Tagged: Database Mirroring, T-SQL, T-SQL Tuesday, Troubleshooting

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Post Navigation

← Previous Post
Next Post →
<

Remote DBA Services
- serious SQL Server expertise for less than a full-time DBA
My Articles
 
My Book
Check out my interview on

Extreme Data Recovery (with Argenis Fernandez)
10 Things all BI System Administrators Should Know
Upcoming Events
    All events shown in Pacific Time

    No events to show

RSS My SQL Server Magazine Articles

  • Database Mirroring for Disaster Recovery September 16, 2011
  • Comparative Review: Database Schema Comparison Tools August 24, 2011
  • 3 Log Shipping Techniques June 22, 2011
  • Hardening SQL Server June 20, 2011
  • Review: ScriptLogic Security Explorer for SQL Server February 8, 2011

Tags

31 Days of Disaster Recovery Architecture Automation CDC & Change Tracking Data Architecture VC Database Mirroring DBCC Denali Disaster Recovery Dynamic Management Views Extended Events Gamers & Geeks General Discussion High Availability How do I ... ? Humor Idera ACE Program Internals MCM Meme Monday Performance & Optimization PowerShell Professional Development Replication Security SQLBits SQL PASS SQL PASS Summit SQLRally SQL Saturday SQL Server Magazine SQL University SSAS & BI SSIS SSMS SSRS T-SQL T-SQL Tuesday tempDB Tips & Tricks Travel Troubleshooting Undocumented Stuff Whitepapers XML in SQL

News

Download my Powershell Scripts

The following scripts can be downloaded as text files. You will need to change the file extension to .ps1 in order to execute them.

Backup a database
Restore a database
Scan a server to find a free port
Query DNS to get the FQDN of a server


To see some examples of my other forms of writing, please visit my page on WritersCafe.org. It is almost exclusively horror fiction, but I sometimes throw other things in there too from time to time. There's one science fiction story, a couple of poems, and quite a few humor pieces as well.


Look for me in the SQL Q&A section of the August, 2007 issue of TechNet Magazine.
August issue of TechNet Magazine's SQL Q&A column

Protect our Heroes

© Copyright 2012 - Robert L Davis
Infinity Theme by DesignCoral / WordPress

Twitter Twitter 
LinkedIn LinkedIn 
TLF TLF RSS RSS 
WritersCafe WritersCafe 
SQLPASS SQLPASS 
Facebook Facebook
grab this