 




<?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>SQL Server with Mr. Denny &#187; Extended Stored Procedure</title>
	<atom:link href="http://itknowledgeexchange.techtarget.com/sql-server/tag/extended-stored-procedure/feed/" rel="self" type="application/rss+xml" />
	<link>http://itknowledgeexchange.techtarget.com/sql-server</link>
	<description></description>
	<lastBuildDate>Fri, 17 May 2013 17:04:01 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Backup Databases on Creation</title>
		<link>http://itknowledgeexchange.techtarget.com/sql-server/backup-databases-on-creation/</link>
		<comments>http://itknowledgeexchange.techtarget.com/sql-server/backup-databases-on-creation/#comments</comments>
		<pubDate>Sat, 17 Nov 2012 14:00:56 +0000</pubDate>
		<dc:creator>Denny Cherry</dc:creator>
				<category><![CDATA[Backup & recovery]]></category>
		<category><![CDATA[BACKUP DATABASE]]></category>
		<category><![CDATA[CREATE TRIGGER]]></category>
		<category><![CDATA[Data Loss]]></category>
		<category><![CDATA[Database]]></category>
		<category><![CDATA[Database Administration]]></category>
		<category><![CDATA[DDL Trigger]]></category>
		<category><![CDATA[Recovery]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[SQL Server 2008 R2]]></category>
		<category><![CDATA[SQL Server 2012]]></category>
		<category><![CDATA[T/SQL]]></category>
		<category><![CDATA[Transactions]]></category>
		<category><![CDATA[Trigger]]></category>
		<category><![CDATA[xp_create_subdir]]></category>

		<guid isPermaLink="false">http://itknowledgeexchange.techtarget.com/sql-server/?p=2194</guid>
		<description><![CDATA[One of the companies which I work with has the occasion to create new databases when they do releases of their software.  Normally this isn&#8217;t a problem, except that they are setup to use maintenance plans to handle the backup and pruning of their transaction logs.  As all the new databases are created in the [...]]]></description>
				<content:encoded><![CDATA[<p>One of the companies which I work with has the occasion to create new databases when they do releases of their software.  Normally this isn&#8217;t a problem, except that they are setup to use maintenance plans to handle the backup and pruning of their transaction logs.  As all the new databases are created in the full recovery model this can end up causing some problems for them as within 12 minutes they start getting emails saying that the transaction log backup job has failed.  And these emails will keep coming in, possibly for hours until the full backup job kicks in later that night.</p>
<p>To solve this problem, I added a DDL trigger to the server which will cause the new database to be backed up as soon as the database is created.  The trigger itself is rather simple.  Most of the trigger is setting variables.  Then I make sure that the database isn&#8217;t a database snapshot, as database snapshots can&#8217;t be backed up.  If it isn&#8217;t a snapshot we continue with everything else.</p>
<p>Then I create a subfolder for the backups to be put into (the backups for each database go into their own folder, so as this is a new database the folder needs to be created).  Then I commit the transaction, as database backups can&#8217;t be taken within a transaction.  Then we do the actual database backup.  I then throw a message to the end user using the RAISERROR statement telling them that they can ignore the other error that they are going to get about the transaction being closed before the trigger was complete.  This is just an annoyance of my needing to commit the transaction before taking the backup.  Sure I could have setup a job which takes the backup and emails if there was a failure, but that just seems to complex for something so simple.  The code for my trigger is below.</p>
<blockquote><p>CREATE TRIGGER BackupNewDatabase<br />
ON ALL SERVER<br />
FOR CREATE_DATABASE<br />
AS<br />
declare @database sysname, @event_data XML = EVENTDATA(), @folder nvarchar(4000), @file nvarchar(4000)</p>
<p>SET @database = @event_data.value(&#8216;(/EVENT_INSTANCE/DatabaseName)[1]&#8216;, &#8216;sysname&#8217;)</p>
<p>set @folder = &#8216;X:\Backups\&#8217; + @database</p>
<p>set @file = @folder + &#8216;\&#8217; + @database + &#8216;.bak&#8217;</p>
<p>if exists (select * from sys.databases where name = @database and source_database_id is null)<br />
BEGIN<br />
EXEC master.dbo.xp_create_subdir @folder</p>
<p>COMMIT</p>
<p>BACKUP DATABASE @database to disk=@file</p>
<p>raiserror( &#8216;You can ignore the error message which says that the transaction ended within the trigger.&#8217;, 16,1)<br />
END<br />
GO</p></blockquote>
<p>Hopefully you find this solution helpful if you get into a situation like this,<br />
Denny</p>
<!-- wpms-network-global-inserts -->]]></content:encoded>
			<wfw:commentRss>http://itknowledgeexchange.techtarget.com/sql-server/backup-databases-on-creation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>South Bay .NET User Group, thanks for having me over.</title>
		<link>http://itknowledgeexchange.techtarget.com/sql-server/south-bay-net-user-group-thanks-for-having-me-over/</link>
		<comments>http://itknowledgeexchange.techtarget.com/sql-server/south-bay-net-user-group-thanks-for-having-me-over/#comments</comments>
		<pubDate>Fri, 11 Sep 2009 07:43:14 +0000</pubDate>
		<dc:creator>Denny Cherry</dc:creator>
				<category><![CDATA[Extended Stored Procedure]]></category>
		<category><![CDATA[In Person Events]]></category>
		<category><![CDATA[Indexing]]></category>
		<category><![CDATA[South Bay .NET User Group]]></category>
		<category><![CDATA[Stored Procedures]]></category>

		<guid isPermaLink="false">http://itknowledgeexchange.techtarget.com/sql-server/?p=716</guid>
		<description><![CDATA[Tonight I spoke at the South Bay .NET User Group.  I had a blast talking with them.  They are a great group of programmers. I gave two presentations tonight. First up was &#8220;SQL Server Indexing for the Client Developer&#8221; and the second was &#8220;What do all these undocumented stored procedures do?&#8220;.  There were lots of [...]]]></description>
				<content:encoded><![CDATA[<p>Tonight I spoke at the South Bay .NET User Group.  I had a blast talking with them.  They are a great group of programmers.<span id="more-716"></span></p>
<p>I gave two presentations tonight. First up was &#8220;<a href="http://www.mrdenny.com/downloads/2009.09.10_south_bay_.net_user_group/SQL Server Indexing for the Client Developer.zip" target="_blank">SQL Server Indexing for the Client Developer</a>&#8221; and the second was &#8220;<a href="http://www.mrdenny.com/downloads/2009.09.10_south_bay_.net_user_group/What Do All These Undocumented Stored Procedures Do.zip" target="_blank">What do all these undocumented stored procedures do?</a>&#8220;.  There were lots of great questions from the group during both presentations which always makes giving the presentations much more fun.</p>
<p>I actually remembered to take a few pictures this time.  I posted them up on <a href="http://www.flickr.com/photos/mrdenny/sets/72157622335822670/show/" target="_blank">flickr</a> for all to see.</p>
<p>I hope everyone that attended enjoyed the presentations.  If you were there please remember to rate my presentations on <a href="http://speakerrate.com/mrdenny" target="_blank">SpeakerRate</a>.  All feedback (good and bad) is appreciated.</p>
<p>If you messed today&#8217;s presentations I&#8217;ll be speaking this November at PASS, the San Gabrial Valley .NET User Group, and to SoCal Code Camp.  Hopefully I&#8217;ll see you there.</p>
<p>Thanks,</p>
<p>Denny</p>
<!-- wpms-network-global-inserts -->]]></content:encoded>
			<wfw:commentRss>http://itknowledgeexchange.techtarget.com/sql-server/south-bay-net-user-group-thanks-for-having-me-over/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Viewing the domain groups that are used to get into SQL</title>
		<link>http://itknowledgeexchange.techtarget.com/sql-server/viewing-the-domain-groups-that-are-used-to-get-into-sql/</link>
		<comments>http://itknowledgeexchange.techtarget.com/sql-server/viewing-the-domain-groups-that-are-used-to-get-into-sql/#comments</comments>
		<pubDate>Sun, 29 Mar 2009 00:05:24 +0000</pubDate>
		<dc:creator>Denny Cherry</dc:creator>
				<category><![CDATA[Extended Stored Procedure]]></category>
		<category><![CDATA[Linchi Shea]]></category>
		<category><![CDATA[T/SQL]]></category>
		<category><![CDATA[xp_logininfo]]></category>

		<guid isPermaLink="false">http://itknowledgeexchange.techtarget.com/sql-server/viewing-the-domain-groups-that-are-used-to-get-into-sql/</guid>
		<description><![CDATA[Last week Linchi Shea wrote a posted a blog entry entitled &#8220;How does that AD user account get access to the database?&#8220;.  In it Linchi shows a method of finding out the domain groups that a SQL Server user uses to access the database. There is however an easier, method you can use. There is [...]]]></description>
				<content:encoded><![CDATA[<p>Last week <a href="http://sqlblog.com/blogs/linchi_shea/" target="_blank">Linchi Shea</a> wrote a posted a blog entry entitled &#8220;<a href="http://sqlblog.com/blogs/linchi_shea/archive/2009/03/26/how-does-that-ad-user-account-get-access-to-the-database.aspx" target="_blank">How does that AD user account get access to the database?</a>&#8220;.  In it Linchi shows a method of finding out the domain groups that a SQL Server user uses to access the database.</p>
<p>There is however an easier, method you can use.</p>
<p>There is a system extended stored procedure called <a href="http://msdn.microsoft.com/en-us/library/ms190369.aspx" target="_blank">xp_logininfo</a>.  Microsoft was even kind enough to document the procedure for us.  You can use this procedure to see what group a use belongs to, or what users are in a domain group, all from T/SQL.</p>
<p>For example, on my sandbox instance if I run</p>
<pre class="brush: sql; title: ; notranslate">EXEC xp_logininfo 'CORP\dcherry'</pre>
<p>I get a result set which says that I gained my access to the SQL Server via the &#8220;BUILTIN\Administrators&#8221; group.</p>
<p>This is a nice quick and easy method to see what domain group a user used to access the SQL Server.</p>
<p>Denny</p>
<!-- wpms-network-global-inserts -->]]></content:encoded>
			<wfw:commentRss>http://itknowledgeexchange.techtarget.com/sql-server/viewing-the-domain-groups-that-are-used-to-get-into-sql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to setup a server to read log files nightly.</title>
		<link>http://itknowledgeexchange.techtarget.com/sql-server/how-to-setup-a-server-to-read-log-files-nightly/</link>
		<comments>http://itknowledgeexchange.techtarget.com/sql-server/how-to-setup-a-server-to-read-log-files-nightly/#comments</comments>
		<pubDate>Mon, 25 Aug 2008 11:00:12 +0000</pubDate>
		<dc:creator>Denny Cherry</dc:creator>
				<category><![CDATA[Backup & recovery]]></category>
		<category><![CDATA[KILL]]></category>
		<category><![CDATA[Log Shipping]]></category>
		<category><![CDATA[Recovery]]></category>
		<category><![CDATA[RESTORE LOG]]></category>
		<category><![CDATA[SQL Server 2005]]></category>
		<category><![CDATA[xp_delete_file]]></category>
		<category><![CDATA[xp_dirtree]]></category>

		<guid isPermaLink="false">http://itknowledgeexchange.techtarget.com/sql-server/how-to-setup-a-server-to-read-log-files-nightly/</guid>
		<description><![CDATA[So you&#8217;ve been tasked with setting up a quick and dirty reporting server.  The goal is to restore the log files from the production server to the reporting server nightly.  The backups are simple, use the SQL Maintenance plan to backup the logs, and then copy them to the remote machine.  But how do you [...]]]></description>
				<content:encoded><![CDATA[<p>So you&#8217;ve been tasked with setting up a quick and dirty reporting server.  The goal is to restore the log files from the production server to the reporting server nightly.</p>
<p> The backups are simple, use the SQL Maintenance plan to backup the logs, and then copy them to the remote machine.  But how do you restore the logs to the reporting server nightly.</p>
<p> Well I&#8217;ve got a two step SQL job which should help you out.</p>
<p>Step 1 kills all current sessions in the database, and step 2 does the actual restores.</p>
<p>The code for step 1 is: </p>
<p><code>declare @spid varchar(20)<br />
declare cur CURSOR FOR<br />
select spid<br />
from sys.sysprocesses<br />
where dbid = db_id('Your Database Name Here') /*&lt;---Put your database name here*/<br />
and spid &gt; 50<br />
open cur<br />
fetch next from cur into @spid<br />
while @@FETCH_STATUS = 0<br />
BEGIN<br />
exec ('kill ' + @spid)<br />
fetch next from cur into @spid<br />
END<br />
close cur<br />
deallocate cur</code></p>
<p>The code for step 2 is:</p>
<p><code>create table #Files<br />
(FileName nvarchar(4000),<br />
Depth int,<br />
IsFile bit)</code><code>insert into #Files<br />
exec xp_dirtree 'd:\', 1, 1</p>
<p>delete from #Files<br />
where IsFile = 0</p>
<p>declare @FileName nvarchar(4000)<br />
declare cur CURSOR FOR SELECT FileName from #Files<br />
open cur<br />
fetch next from cur into @FileName<br />
WHILE @@FETCH_STATUS = 0<br />
BEGIN<br />
SET @FileName = 'D:\Path\To\Your\Log\Backups\' + @FileName<br />
RESTORE LOG YourDatabaseNameHere FROM <a href="mailto:DISK=@FileName">DISK=@FileName</a> WITH STANDBY='D:\Path\To\Your\StandBy\File.standby'<br />
IF @@ERROR &lt;&gt; 0<br />
exec xp_delete_file @FileName<br />
fetch next from cur into @FileName<br />
END<br />
close cur<br />
deallocate cur</p>
<p></code>I hope this makes your process easier. Now this code only works on SQL Server 2005 and up as the system stored procedures which I use were not included until SQL Server 2005.</p>
<p>Denny</p>
<!-- wpms-network-global-inserts -->]]></content:encoded>
			<wfw:commentRss>http://itknowledgeexchange.techtarget.com/sql-server/how-to-setup-a-server-to-read-log-files-nightly/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Checking free disk space from T/SQL</title>
		<link>http://itknowledgeexchange.techtarget.com/sql-server/checking-free-disk-space-from-tsql/</link>
		<comments>http://itknowledgeexchange.techtarget.com/sql-server/checking-free-disk-space-from-tsql/#comments</comments>
		<pubDate>Mon, 28 Apr 2008 09:00:34 +0000</pubDate>
		<dc:creator>Denny Cherry</dc:creator>
				<category><![CDATA[SQL]]></category>
		<category><![CDATA[T/SQL]]></category>
		<category><![CDATA[xp_fixeddrives]]></category>

		<guid isPermaLink="false">http://itknowledgeexchange.techtarget.com/sql-server/checking-free-disk-space-from-tsql/</guid>
		<description><![CDATA[I've seen some very creative ways to see how much free disk space SQL Servers have.  Most make use of xp_cmdshell which some very complex dos commands or vbscripts.  Here is a much, much simpler one.]]></description>
				<content:encoded><![CDATA[<p>I&#8217;ve seen some very creative ways to see how much free disk space SQL Servers have.  Most make use of xp_cmdshell which some very complex dos commands or vbscripts.</p>
<p>However there is a much easier way.  Microsoft has included the system extended stored procedure xp_fixeddrives.  It returns the all the fixed drives and the amount of free space in megs.</p>
<p>Denny</p>
<!-- wpms-network-global-inserts -->]]></content:encoded>
			<wfw:commentRss>http://itknowledgeexchange.techtarget.com/sql-server/checking-free-disk-space-from-tsql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
