<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Of Ones and Zeros &#187; SQL</title>
	<atom:link href="http://www.ofonesandzeros.com/category/technology/sql/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ofonesandzeros.com</link>
	<description>a discussion of software and technology hosted by bryan napier</description>
	<lastBuildDate>Wed, 07 Apr 2010 23:14:59 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Truncating Transaction Logs in SQL 2008</title>
		<link>http://www.ofonesandzeros.com/2010/01/29/truncating-transaction-logs-in-sql-2008/</link>
		<comments>http://www.ofonesandzeros.com/2010/01/29/truncating-transaction-logs-in-sql-2008/#comments</comments>
		<pubDate>Fri, 29 Jan 2010 15:45:20 +0000</pubDate>
		<dc:creator>bryan</dc:creator>
				<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.ofonesandzeros.com/?p=182</guid>
		<description><![CDATA[One of my clients is beginning to migrate to SQL 2008 for their SharePoint farms.&#160; In the past, we have used scripts to truncate the transaction logs and shrink the DBs in the non-production environments using the “BACKUP LOG [dbname] WITH TRUNCATE_ONLY” command.&#160; As you may be aware, the “WITH TRUNCATE_ONLY” option has been deprecated [...]]]></description>
			<content:encoded><![CDATA[<p>One of my clients is beginning to migrate to SQL 2008 for their SharePoint farms.&#160; In the past, we have used scripts to truncate the transaction logs and shrink the DBs in the non-production environments using the “BACKUP LOG [dbname] WITH TRUNCATE_ONLY” command.&#160; As you may be aware, the “WITH TRUNCATE_ONLY” option has been deprecated in SQL 2008.&#160; In SQL 2008 instead you need to change the recovery mode of the database to SIMPLE and then back to FULL in order to truncate the log (or perform a backup of the log of course).&#160; In production environments obviously backing up the transaction log often is the best strategy, however in non-production environments that isn’t always feasible.&#160; I wrote the following script for this client to truncate the log all databases other than the system ones.&#160; </p>
<p>Enjoy!</p>
<pre class="brush: sql">
DECLARE @DatabaseNames TABLE
(
	name varchar(255)
)

INSERT INTO @DatabaseNames
SELECT name FROM sys.databases WHERE
name NOT IN ( &#039;master&#039;, &#039;model&#039;, &#039;msdb&#039;, &#039;tempdb&#039; )

DECLARE DatabaseCursor CURSOR FOR
SELECT * FROM @DatabaseNames

OPEN DatabaseCursor

DECLARE @DatabaseName varchar(255)
DECLARE @LogFileName varchar(255)
DECLARE @SqlStatement varchar(2000)

FETCH NEXT FROM DatabaseCursor INTO @DatabaseName

WHILE @@FETCH_STATUS=0
BEGIN
	PRINT &#039;----------------------------------------------------------&#039;
	PRINT &#039;Processing database &#039; + @DatabaseName
	PRINT &#039;----------------------------------------------------------&#039;

	SET @SqlStatement = &#039;ALTER DATABASE [&#039; + @DatabaseName +
		&#039;] SET RECOVERY SIMPLE&#039;

	PRINT @SqlStatement
	EXEC (@SqlStatement)

	SET @LogFileName =
		(
			SELECT b.name
			FROM sys.databases a
			INNER JOIN sys.master_files b ON
				a.database_id = b.database_id
			WHERE
				a.name = @DatabaseName
				AND b.name LIKE &#039;%log&#039;)

	PRINT @LogFileName

	SET @SqlStatement = &#039;USE [&#039; + @DatabaseName +
		&#039;] DBCC SHRINKFILE(N&#039;&#039;&#039; + @LogFileName + &#039;&#039;&#039;, 1)&#039;

	PRINT @SqlStatement
	EXEC (@SqlStatement)

	SET @SqlStatement = &#039;ALTER DATABASE [&#039; + @DatabaseName +
		&#039;] SET RECOVERY FULL&#039;

	PRINT @SqlStatement
	EXEC (@SqlStatement)

	FETCH NEXT FROM DatabaseCursor INTO @DatabaseName
END

CLOSE DatabaseCursor
DEALLOCATE DatabaseCursor
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.ofonesandzeros.com/2010/01/29/truncating-transaction-logs-in-sql-2008/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
