<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>DBA and SysAdmin World</title>
	<atom:link href="http://solihinho.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://solihinho.wordpress.com</link>
	<description>Database Administrator and System Administrator Articles</description>
	<lastBuildDate>Tue, 27 Apr 2010 18:10:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='solihinho.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>DBA and SysAdmin World</title>
		<link>http://solihinho.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://solihinho.wordpress.com/osd.xml" title="DBA and SysAdmin World" />
	<atom:link rel='hub' href='http://solihinho.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Moving to My New Blog</title>
		<link>http://solihinho.wordpress.com/2010/04/28/moving-to-my-new-blog/</link>
		<comments>http://solihinho.wordpress.com/2010/04/28/moving-to-my-new-blog/#comments</comments>
		<pubDate>Tue, 27 Apr 2010 18:10:33 +0000</pubDate>
		<dc:creator>solihinho</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://solihinho.wordpress.com/?p=837</guid>
		<description><![CDATA[Sorry for not updating this blog for long time.  I will move my blog to http://blog.bimbelbee.com. Thanks<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=solihinho.wordpress.com&amp;blog=4251551&amp;post=837&amp;subd=solihinho&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Sorry for not updating this blog for long time.  I will move my blog to http://blog.bimbelbee.com.</p>
<p>Thanks</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/solihinho.wordpress.com/837/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/solihinho.wordpress.com/837/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/solihinho.wordpress.com/837/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/solihinho.wordpress.com/837/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/solihinho.wordpress.com/837/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/solihinho.wordpress.com/837/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/solihinho.wordpress.com/837/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/solihinho.wordpress.com/837/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/solihinho.wordpress.com/837/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/solihinho.wordpress.com/837/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/solihinho.wordpress.com/837/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/solihinho.wordpress.com/837/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/solihinho.wordpress.com/837/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/solihinho.wordpress.com/837/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=solihinho.wordpress.com&amp;blog=4251551&amp;post=837&amp;subd=solihinho&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://solihinho.wordpress.com/2010/04/28/moving-to-my-new-blog/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">solihinho</media:title>
		</media:content>
	</item>
		<item>
		<title>SharePoint 2003 &quot;Alert Me&quot; does not work</title>
		<link>http://solihinho.wordpress.com/2009/05/23/sharepoint-2003-alert-me-does-not-work/</link>
		<comments>http://solihinho.wordpress.com/2009/05/23/sharepoint-2003-alert-me-does-not-work/#comments</comments>
		<pubDate>Sat, 23 May 2009 16:02:23 +0000</pubDate>
		<dc:creator>solihinho</dc:creator>
				<category><![CDATA[Problem]]></category>
		<category><![CDATA[System Administrator]]></category>
		<category><![CDATA[Alert Me]]></category>
		<category><![CDATA[Sharepoint 2003]]></category>
		<category><![CDATA[WSS 2.0]]></category>

		<guid isPermaLink="false">http://solihinho.wordpress.com/2009/05/23/sharepoint-2003-alert-me-doesnt-work/</guid>
		<description><![CDATA[Issue Our user suddenly raised an issue that she never got an alert anymore from her department portal if any changes happened on the document list. We&#8217;ve checked the SMTP etc and looks like it was working fine. Honestly we as SharePoint Administrator did not know how the way this alert works. So the last [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=solihinho.wordpress.com&amp;blog=4251551&amp;post=822&amp;subd=solihinho&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div style="text-align:justify;font-family:lucida sans unicode;color:black;font-size:12px;"><img style="border-width:0;margin:10px 10px 10px 0;" border="0" alt="SharePoint 2003" align="left" src="http://solihinho.files.wordpress.com/2009/05/sharepoint-2003.jpg?w=123&#038;h=123" width="123" height="123"> <span style="font-family:lucida handwriting;color:#004466;font-size:16px;font-weight:bold;">Issue </span><br />Our user suddenly raised an issue that she never got an alert anymore from her department portal if any changes happened on the document list. We&#8217;ve checked the SMTP etc and looks like it was working fine. Honestly we as SharePoint Administrator did not know how the way this alert works. So the last way we coulddo was asking for help from Microsoft support <img alt="Big Grin" src="http://us.i1.yimg.com/us.yimg.com/i/mesg/emoticons7/4.gif">. </p>
<p><span style="font-family:lucida handwriting;color:#004466;font-size:16px;font-weight:bold;">Things need to be checked</span><br />Microsoft support asked us to do several things for investigating this issue. From this we also knew how SharePoint&#8217;s alert actually works. <br /> <br />
<table style="margin-top:10px;margin-bottom:10px;margin-left:30px;" width="80%">
<tbody>
<tr>
<td style="background-color:#ffffcc;font-family:courier new;color:black;font-size:12px;border-style:double;padding:5px 5px 5px 10px;">
<pre>1. Check whether we can get the first notification mail from SharePoint when we subscription “Alert me”.

2. Check whether we will get a new record in content database.
    · Actually, once we done some modification in library or list, the system will insert
      a new record in database for "Mail Alert".
    · We can run the following command to check whether this record has be inserted to DB :
        <font color="#0000ff">SELECT</font> * <font color="#0000ff">FROM</font> dbo.EventCache <font color="#0000ff">ORDER BY</font> eventtime <font color="#0000ff">DESC</font>

3. Then, SharePoint timer job service will read the data from DB and send mail out.
    · NOTE : The timer job service will run every 5 minutes by default.
    · We also can run the following command to check it.
        Stsadm –o getproperty –propertyname job-immediate-alerts

4. After that, the record will be deleted from content database.
</pre>
</td>
</tr>
</tbody>
</table>
<p><span style="font-family:lucida handwriting;color:#004466;font-size:16px;font-weight:bold;">Problem Solve </span><br />From that guidance, we just realized that our SharePoint Timer service was off. After turned it on, the alert back to normal. If the guidance doesn&#8217;t help on your problem. It should be another component that you need to check such as the SMTP server etc. </div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/solihinho.wordpress.com/822/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/solihinho.wordpress.com/822/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/solihinho.wordpress.com/822/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/solihinho.wordpress.com/822/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/solihinho.wordpress.com/822/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/solihinho.wordpress.com/822/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/solihinho.wordpress.com/822/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/solihinho.wordpress.com/822/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/solihinho.wordpress.com/822/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/solihinho.wordpress.com/822/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/solihinho.wordpress.com/822/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/solihinho.wordpress.com/822/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/solihinho.wordpress.com/822/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/solihinho.wordpress.com/822/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=solihinho.wordpress.com&amp;blog=4251551&amp;post=822&amp;subd=solihinho&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://solihinho.wordpress.com/2009/05/23/sharepoint-2003-alert-me-does-not-work/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">solihinho</media:title>
		</media:content>

		<media:content url="http://solihinho.files.wordpress.com/2009/05/sharepoint-2003.jpg" medium="image">
			<media:title type="html">SharePoint 2003</media:title>
		</media:content>

		<media:content url="http://us.i1.yimg.com/us.yimg.com/i/mesg/emoticons7/4.gif" medium="image">
			<media:title type="html">Big Grin</media:title>
		</media:content>
	</item>
		<item>
		<title>Disable / Enable SQL Jobs by T-SQL</title>
		<link>http://solihinho.wordpress.com/2009/04/27/disable-enable-sql-jobs-by-t-sql/</link>
		<comments>http://solihinho.wordpress.com/2009/04/27/disable-enable-sql-jobs-by-t-sql/#comments</comments>
		<pubDate>Sun, 26 Apr 2009 17:23:38 +0000</pubDate>
		<dc:creator>solihinho</dc:creator>
				<category><![CDATA[Script]]></category>
		<category><![CDATA[Job Schedule]]></category>
		<category><![CDATA[SQL Server]]></category>

		<guid isPermaLink="false">http://solihinho.wordpress.com/2009/04/27/disable-enable-sql-jobs-by-t-sql/</guid>
		<description><![CDATA[Objective Sometimes on certain situation e.g.:month end closing, we need to disable all our SQL Server jobs then enable it back after that process was finished. If we have tens of jobs even more, I believe it is quite take time if we do it through SQL Server Management Studio / Enterprise Manager.&#160; SQL Server [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=solihinho.wordpress.com&amp;blog=4251551&amp;post=817&amp;subd=solihinho&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div style="text-align:justify;font-family:lucida sans unicode;color:black;font-size:12px;"><span style="font-family:lucida handwriting;color:#004466;font-size:16px;font-weight:bold;"><img style="border-width:0;margin:10px 10px 5px 0;" border="0" alt="coding2" align="left" src="http://solihinho.files.wordpress.com/2009/04/coding2.jpg?w=112&#038;h=150" width="112" height="150"> Objective </span><br />Sometimes on certain situation e.g.:month end closing, we need to disable all our SQL Server jobs then enable it back after that process was finished. If we have tens of jobs even more, I believe it is quite take time if we do it through SQL Server Management Studio / Enterprise Manager.&nbsp; SQL Server already provide a stored procedure &#8220;sp_update_job&#8221; on msdb database to change the status of the job (see SQL Server BOL for further information). We just provide the job_id / job_name and the new status (1 = enable, 0 = disable). </p>
<p><span style="font-family:lucida handwriting;color:#004466;font-size:16px;font-weight:bold;">Usage </span><br />Just run below script to get the TSQL script. You can review the result first before run the result to disable/ enable SQL jobs. FYI, when trying to disable the jobs the script only execute the jobs that status is enable. To enable it back, just keep the result and replace&nbsp; <strong>@enable</strong> = 0 become <strong>@enable = 1</strong>. I just prevent not to enable job which status is disable before the script was executed. </p>
<p><span style="font-family:lucida handwriting;color:#004466;font-size:16px;font-weight:bold;">Script </span><br /> <br />
<table style="margin-top:10px;margin-bottom:10px;margin-left:30px;" width="80%">
<tbody>
<tr>
<td style="background-color:#ffffcc;font-family:courier new;color:black;font-size:12px;border-style:double;padding:5px 5px 5px 10px;">
<pre><font color="#0000ff">SET NOCOUNT ON</font>

<font color="#0000ff">DECLARE</font> @job_id  <font color="#0000ff">UNIQUEIDENTIFIER</font>
<font color="#0000ff">DECLARE</font> @disable_job <font color="#0000ff">CHAR(1)</font>

<font color="#0000ff">SET</font> @disable_job = <font color="#ff0000">'Y'</font> <font color="#008080">-- Fill 'Y' to disable job .. 'N' to enable job</font>

<font color="#000080">IF</font> <font color="#ff00ff">OBJECT_ID</font>(<font color="#ff0000">'tempdb..#sysjobs'</font>) <font color="#0000ff">IS NOT NULL</font>
   DROP TABLE #sysjobs

<font color="#0000ff">SELECT</font> job_id INTO #sysjobs
<font color="#0000ff">FROM</font> msdb..sysjobs
<font color="#0000ff">WHERE</font> enabled = <font color="#0000ff">CASE</font> @disable_job <font color="#0000ff">WHEN</font> <font color="#ff0000">'Y'</font> <font color="#0000ff">THEN</font> 1 <font color="#0000ff">ELSE</font> 0 <font color="#0000ff">END</font>

<font color="#0000ff">WHILE</font> (1 = 1)
<font color="#0000ff">BEGIN</font>
   <font color="#0000ff">SELECT</font> @job_id = job_id <font color="#0000ff">FROM</font> #sysjobs

   IF <font color="#ff00ff">@@rowcount</font> = 0
      <font color="#0000ff">GOTO</font> _EXIT

   PRINT <font color="#ff0000">'exec msdb..sp_update_job @job_id = '''</font> + CAST(@job_id AS VARCHAR(36))+ <font color="#ff0000">''', @enabled = '</font>
         + CASE @disable_job WHEN <font color="#ff0000">'Y' </font>THEN <font color="#ff0000">'0' </font>ELSE <font color="#ff0000">'1'</font> END

   PRINT <font color="#ff0000">'GO'  </font> 

   <font color="#0000ff">DELETE FROM</font> #sysjobs <font color="#0000ff">WHERE</font> job_id = @job_id
END

_EXIT:
   <font color="#0000ff">DROP TABLE</font> #sysjobs

<font color="#0000ff">SET NOCOUNT OFF</font>
</pre>
</td>
</tr>
</tbody>
</table>
</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/solihinho.wordpress.com/817/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/solihinho.wordpress.com/817/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/solihinho.wordpress.com/817/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/solihinho.wordpress.com/817/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/solihinho.wordpress.com/817/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/solihinho.wordpress.com/817/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/solihinho.wordpress.com/817/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/solihinho.wordpress.com/817/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/solihinho.wordpress.com/817/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/solihinho.wordpress.com/817/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/solihinho.wordpress.com/817/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/solihinho.wordpress.com/817/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/solihinho.wordpress.com/817/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/solihinho.wordpress.com/817/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=solihinho.wordpress.com&amp;blog=4251551&amp;post=817&amp;subd=solihinho&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://solihinho.wordpress.com/2009/04/27/disable-enable-sql-jobs-by-t-sql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">solihinho</media:title>
		</media:content>

		<media:content url="http://solihinho.files.wordpress.com/2009/04/coding2.jpg" medium="image">
			<media:title type="html">coding2</media:title>
		</media:content>
	</item>
		<item>
		<title>Moving tempdb database to another disk</title>
		<link>http://solihinho.wordpress.com/2009/03/28/moving-tempdb-database-to-another-disk/</link>
		<comments>http://solihinho.wordpress.com/2009/03/28/moving-tempdb-database-to-another-disk/#comments</comments>
		<pubDate>Fri, 27 Mar 2009 19:22:51 +0000</pubDate>
		<dc:creator>solihinho</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[tempdb]]></category>

		<guid isPermaLink="false">http://solihinho.wordpress.com/2009/03/28/moving-tempdb-database-to-another-disk/</guid>
		<description><![CDATA[IssueThese weeks errors occurred several times on my SQL Server event viewer &#8220;I/O error (bad page ID) detected during read at offset 0x000000XXXXX in file &#8220;C:\Program Files\Microsoft SQL Server\MSSQL\data\tempdb.mdf&#8221;. This error may be related to our hardware. We tried to change our disk on drive C but the error still occurred. To escalate this error [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=solihinho.wordpress.com&amp;blog=4251551&amp;post=807&amp;subd=solihinho&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div style="text-align:justify;font-family:lucida sans unicode;color:black;font-size:12px;"><img style="border-width:0;margin:10px 10px 10px 0;" border="0" alt="move" align="left" src="http://solihinho.files.wordpress.com/2009/03/move.jpg?w=124&#038;h=88" width="124" height="88"> <span style="font-family:lucida handwriting;color:#004466;font-size:16px;font-weight:bold;">Issue</span><br />These weeks errors occurred several times on my SQL Server event viewer &#8220;<strong><em>I/O error (bad page ID) detected during read at offset 0x000000XXXXX in file &#8220;C:\Program Files\Microsoft SQL Server\MSSQL\data\tempdb.mdf&#8221;. </em></strong>This error may be related to our hardware. We tried to change our disk on drive C but the error still occurred. To escalate this error we tried to move tempdb data file on drive C to another disk which is have different disk controller.<br /><span style="font-family:lucida handwriting;color:#004466;font-size:16px;font-weight:bold;"><br />Step by step </span></p>
<p>1. Determine logical name of file that we want to move<br />
<table style="margin-top:5px;margin-bottom:0;margin-left:30px;" width="50%">
<tbody>
<tr>
<td style="text-align:justify;background-color:#cccccc;font-family:lucida sans unicode;color:black;font-size:12px;border-color:red;border-style:dotted;padding:5px 10px 0;">
<pre>USE tempdb
GO
sp_helpfile
</pre>
</td>
</tr>
</tbody>
</table>
<p>On below sample, the logical name are tempdev and templog. </p>
<p>&nbsp;<img style="border-width:0;" border="0" alt="image" src="http://solihinho.files.wordpress.com/2009/03/image.png?w=539&#038;h=135" width="539" height="135">&nbsp; </p>
<p>2. Move the data file using ALTER DATABASE statement </p>
<table style="margin-top:5px;margin-bottom:0;margin-left:30px;" width="50%">
<tbody>
<tr>
<td style="text-align:justify;background-color:#cccccc;font-family:lucida sans unicode;color:black;font-size:12px;border-color:red;border-style:dotted;padding:5px 10px 0;">
<pre>USE master
GO
ALTER DATABASE tempdb modify file (name = [logical name], filename = '[new location of file]')</pre>
</td>
</tr>
</tbody>
</table>
<p>e.g.: 
<pre>ALTER DATABASE tempdb modify file (name = tempdev, filename = 'D:\SQLDATA\tempdb.mdf')
or
ALTER DATABASE tempdb modify file (name = templog , filename = 'D:\SQLLOG\tempdb_log.ldf')</pre>
<p>3. This alteration will confirm after we restart the SQL Server service. Stop than restart the SQL Server service. You should delete&nbsp; the old files manually. </p>
<p><span style="font-family:lucida handwriting;color:#004466;font-size:16px;font-weight:bold;">Result </span><br />After this action, the error does not occur any more <img alt="Happy" src="http://us.i1.yimg.com/us.yimg.com/i/mesg/emoticons7/1.gif">. Meanwhile we still investigate why the error occurred. We also open ticket to Microsoft to help us investigate this issue.
<pre></pre>
</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/solihinho.wordpress.com/807/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/solihinho.wordpress.com/807/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/solihinho.wordpress.com/807/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/solihinho.wordpress.com/807/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/solihinho.wordpress.com/807/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/solihinho.wordpress.com/807/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/solihinho.wordpress.com/807/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/solihinho.wordpress.com/807/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/solihinho.wordpress.com/807/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/solihinho.wordpress.com/807/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/solihinho.wordpress.com/807/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/solihinho.wordpress.com/807/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/solihinho.wordpress.com/807/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/solihinho.wordpress.com/807/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=solihinho.wordpress.com&amp;blog=4251551&amp;post=807&amp;subd=solihinho&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://solihinho.wordpress.com/2009/03/28/moving-tempdb-database-to-another-disk/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">solihinho</media:title>
		</media:content>

		<media:content url="http://solihinho.files.wordpress.com/2009/03/move.jpg" medium="image">
			<media:title type="html">move</media:title>
		</media:content>

		<media:content url="http://solihinho.files.wordpress.com/2009/03/image.png" medium="image">
			<media:title type="html">image</media:title>
		</media:content>

		<media:content url="http://us.i1.yimg.com/us.yimg.com/i/mesg/emoticons7/1.gif" medium="image">
			<media:title type="html">Happy</media:title>
		</media:content>
	</item>
		<item>
		<title>Starting SQL Server in Single Mode User</title>
		<link>http://solihinho.wordpress.com/2009/02/28/starting-sql-server-in-single-mode-user/</link>
		<comments>http://solihinho.wordpress.com/2009/02/28/starting-sql-server-in-single-mode-user/#comments</comments>
		<pubDate>Sat, 28 Feb 2009 01:07:50 +0000</pubDate>
		<dc:creator>solihinho</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Single Mode]]></category>

		<guid isPermaLink="false">http://solihinho.wordpress.com/2009/02/28/starting-sql-server-in-single-mode-user/</guid>
		<description><![CDATA[Background Last time I posted how to set database in single mode. This time I faced a problem with DBCC CHECKDB. Sometimes it takes almost 1 hour to examine 1 database but sometimes only take 10 minutes. I suspected the problem is in the disk. There are many transactions make the disk IO high. I [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=solihinho.wordpress.com&amp;blog=4251551&amp;post=792&amp;subd=solihinho&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div style="text-align:justify;font-family:lucida sans unicode;color:black;font-size:12px;"><img style="border-width:0;margin:5px 10px 10px 0;" border="0" alt="Single user" align="left" src="http://solihinho.files.wordpress.com/2009/02/single-user.jpg?w=170&#038;h=116" width="170" height="116"> <span style="font-family:lucida handwriting;color:#004466;font-size:16px;font-weight:bold;">Background </span><br />Last time I posted <a href="http://solihinho.wordpress.com/2008/12/14/how-to-set-database-into-single-user-mode/" target="_blank">how to set database in single mode</a>. This time I faced a problem with DBCC CHECKDB. Sometimes it takes almost 1 hour to examine 1 database but sometimes only take 10 minutes. I suspected the problem is in the disk. There are many transactions make the disk IO high. I needed to try run DBCC CHECKDB while there are not another transactions allowed to process. Rather than I have to shut down another application servers, I prefer to make the SQL Server in single mode so only one user allowed to connect to the SQL Server. </p>
<p><span style="font-family:lucida handwriting;color:#004466;font-size:16px;font-weight:bold;">Step by Step </span><br />1. Stop SQL Server service<br />2. Enter console mode by type <strong><em>cmd</em></strong> from start -&gt; run<br />3. Go to folder where <strong>sqlservr.exe</strong> located e.g.: C:\Program Files\Microsoft SQL Server\MSSQL\Binn<br />4. Type <strong>sqlservr.exe -m</strong> for SQL Server is not use instance name) or <strong>sqlservr.exe -m -s[Instance Name]</strong> for SQL Server that use instance name. <br />5. Connect to the SQL Server use user with <strong>system administrator</strong> role<br />6. At the end, type <strong>Ctrl+C</strong> in console screen, type <strong>&#8220;Y&#8221;</strong> on &#8220;Do you wish to shutdown SQL Server (Y/N)?&#8221; <br />7. Start SQL Server service.</p>
<table style="margin-top:10px;margin-bottom:10px;margin-left:30px;" width="70%">
<tbody>
<tr>
<td style="text-align:justify;background-color:#cccccc;font-family:lucida sans unicode;color:black;font-size:10px;border-color:red;border-style:dotted;padding:5px 10px;"><img alt="Time out" src="http://us.i1.yimg.com/us.yimg.com/i/mesg/emoticons7/104.gif"> If your application use SQL Server user with role as system administrator like &#8220;sa&#8221;, you may face problem when try to connect to SQL Server in Single Mode User because there is possibility the application already connect to the SQL Server before you do. </td>
</tr>
</tbody>
</table>
</div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/solihinho.wordpress.com/792/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/solihinho.wordpress.com/792/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/solihinho.wordpress.com/792/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/solihinho.wordpress.com/792/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/solihinho.wordpress.com/792/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/solihinho.wordpress.com/792/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/solihinho.wordpress.com/792/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/solihinho.wordpress.com/792/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/solihinho.wordpress.com/792/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/solihinho.wordpress.com/792/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/solihinho.wordpress.com/792/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/solihinho.wordpress.com/792/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/solihinho.wordpress.com/792/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/solihinho.wordpress.com/792/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=solihinho.wordpress.com&amp;blog=4251551&amp;post=792&amp;subd=solihinho&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://solihinho.wordpress.com/2009/02/28/starting-sql-server-in-single-mode-user/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">solihinho</media:title>
		</media:content>

		<media:content url="http://solihinho.files.wordpress.com/2009/02/single-user.jpg" medium="image">
			<media:title type="html">Single user</media:title>
		</media:content>

		<media:content url="http://us.i1.yimg.com/us.yimg.com/i/mesg/emoticons7/104.gif" medium="image">
			<media:title type="html">Time out</media:title>
		</media:content>
	</item>
		<item>
		<title>Review Fill Factor of The Indexes</title>
		<link>http://solihinho.wordpress.com/2009/02/10/review-fill-factor-of-the-indexes/</link>
		<comments>http://solihinho.wordpress.com/2009/02/10/review-fill-factor-of-the-indexes/#comments</comments>
		<pubDate>Mon, 09 Feb 2009 17:53:24 +0000</pubDate>
		<dc:creator>solihinho</dc:creator>
				<category><![CDATA[Script]]></category>
		<category><![CDATA[Index]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false">http://solihinho.wordpress.com/2009/02/10/review-fill-factor-of-the-indexes/</guid>
		<description><![CDATA[BackgroundWhen I worked as DBA in my office, It has already hundreds of databases. I have to know the functionality of each databases. How often the tables of the database are accessed, inserted and updated by users. Once you already know, it is easier for DBA to decide the fill factor for the index of [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=solihinho.wordpress.com&amp;blog=4251551&amp;post=784&amp;subd=solihinho&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div style="font-size:12px;color:black;font-family:lucida sans unicode;text-align:justify;"><img style="border-width:0;margin:5px 10px 10px 0;" height="172" alt="analyze" src="http://solihinho.files.wordpress.com/2009/02/analyze.jpg?w=179&#038;h=172" width="179" align="left" border="0"> <span style="font-weight:bold;font-size:16px;color:#004466;font-family:lucida handwriting;">Background</span><br />When I worked as DBA in my office, It has already hundreds of databases. I have to know the functionality of each databases. How often the tables of the database are accessed, inserted and updated by users. Once you already know, it is easier for DBA to decide the fill factor for the index of the tables. </p>
<p>The right value of the fill factor will upgrade the performance of the application. The fill factor value is a percentage between 0 to 100. This percentage specifies how much to fill the data pages after the index is created. A value of 100 means the pages will be full and will take the least amount of storage space. This setting should be used only when there will be no changes to the data, for example, on a read-only table. A lower value leaves more empty space on the data pages, which reduces the need to split data pages as indexes grow but requires more storage space. This setting is more appropriate when there will be changes to the data in the table. </p>
<p>The fill factor is implemented only when the index is created; it is not maintained after the index is created as data is added, deleted, or updated in the table. Trying to maintain the extra space on the data pages would defeat the purpose of originally using the fill factor because SQL Server would have to perform page splits to maintain the percentage of free space, specified by the fill factor, on each page as data is entered. Therefore, if the data in the table is significantly modified and new data added, the empty space in the data pages can fill. In this situation, the index can be re-created and the fill factor specified again to redistribute the data (<em>SQL Server BOL 2000</em>). </p>
<p>Therefore I created script for listing all indexes&#8217;s fill factor. There are 2 types of script. One for SQL 2000 and other for SQL 2005. For SQL 2005 also list value of the index fragmentation. </p>
<p><span style="font-weight:bold;font-size:16px;color:#004466;font-family:lucida handwriting;">Script </span><br /><em>SQL Server 2000<br /></em><br />
<table style="margin-top:10px;margin-bottom:10px;margin-left:30px;" width="80%">
<tbody>
<tr>
<td style="font-size:12px;color:black;font-family:courier new;background-color:#ffffcc;border-style:double;padding:5px 5px 5px 10px;">
<pre><font color="#008080">/*
  Created by : Solihin ho - http://solihinho.wordpress.com

  Compatibility : SQL 2000

*/</font>

<font color="#0000ff">IF</font> <font color="#ff00ff">object_id</font>(<font color="#ff0000">'tempdb..#result'</font>) IS <font color="#808080">NOT NULL</font>
   <font color="#0000ff">DROP TABLE</font> #result

<font color="#0000ff">CREATE TABLE</font> #result
(
   DBName       <font color="#0000ff">sysname</font>,
   TableName    <font color="#0000ff">sysname</font>,
   IndexName    <font color="#0000ff">sysname</font>,
   [Rows]       <font color="#0000ff">int</font>,
   [FillFactor] <font color="#0000ff">tinyint</font>,
   [TimeStamp]  <font color="#0000ff">datetime</font>
)

GO

<font color="#800000">sp_msforeachdb</font> <font color="#ff0000">'USE ?
INSERT #result (DbName, TableName, IndexName, [Rows], [FillFactor], [TimeStamp])
SELECT db_name() as DbName
,o.name as TableName
,i.name as IndexName
,i.rows as RowsCount
,i.OrigFillFactor
,GetDate() as [TimeStamp]
FROM sysindexes i
INNER JOIN sysobjects o ON i.id = o.id
WHERE i.indid &gt; 0 and i.indid &lt; 255
AND i.name NOT LIKE ''_WA_Sys_%'''</font>

<font color="#0000ff">SELECT</font> * <font color="#0000ff">FROM</font> #Result</pre>
</td>
</tr>
</tbody>
</table>
<p><em>SQL Server 2005 and Next Version</em> </p>
<table style="margin-top:10px;margin-bottom:10px;margin-left:30px;" width="80%">
<tbody>
<tr>
<td style="font-size:12px;color:black;font-family:courier new;background-color:#ffffcc;border-style:double;padding:5px 5px 5px 10px;">
<pre><font color="#008080">/*
  Created by : Solihin ho - http://solihinho.wordpress.com

  Compatibility : SQL 2005 and next version

*/</font>

<font color="#0000ff">IF</font> <font color="#ff00ff">object_id</font>(<font color="#ff0000">'tempdb..#result'</font>) <font color="#0000ff">IS</font> <font color="#808080">NOT NULL</font>
   <font color="#0000ff">DROP TABLE</font> #result

<font color="#0000ff">CREATE TABLE</font> #result
(
   DBName       <font color="#0000ff">sysname</font>,
   TableName    <font color="#0000ff">sysname</font>,
   IndexName    <font color="#0000ff">sysname</font>,
   [Rows]       <font color="#0000ff">int</font>,
   [FillFactor] <font color="#0000ff">tinyint</font>,
   Index_Fragmentation <font color="#0000ff">float</font>,
   page_count   <font color="#0000ff">int</font>,
   [TimeStamp]  <font color="#0000ff">datetime</font>
)

GO

<font color="#800000">sp_msforeachdb</font> <font color="#ff0000">'USE ?
INSERT INTO #Result (DBName, TableName, IndexName
    , [FillFactor], [Rows], Index_Fragmentation
    , page_count, [TimeStamp])
SELECT
  db_name() AS DbName
, B.name AS TableName
, C.name AS IndexName
, C.fill_factor AS IndexFillFactor
, D.rows AS RowsCount
, A.avg_fragmentation_in_percent
, A.page_count
, GetDate() as [TimeStamp]
FROM sys.dm_db_index_physical_stats(DB_ID(),NULL,NULL,NULL,NULL) A
INNER JOIN sys.objects B
   ON A.object_id = B.object_id
INNER JOIN sys.indexes C
   ON B.object_id = C.object_id AND A.index_id = C.index_id
INNER JOIN sys.partitions D
   ON B.object_id = D.object_id AND A.index_id = D.index_id
WHERE C.index_id &gt; 0'</font>

<font color="#0000ff">SELECT</font> * <font color="#0000ff">FROM</font> #Result</pre>
</td>
</tr>
</tbody>
</table>
<p><span style="font-weight:bold;font-size:16px;color:#004466;font-family:lucida handwriting;">What next? </span><br />You can schedule above script and keep the result in a table for reviewing next time. You can review the row count to see whether the table grow or not. Just decide the value of the table&#8217;s fill factor e.g.: 100% for read only table. For SQL 2005 script, you also review of the index fragmentation. You can review how fast the index become fragmented. You can set the fill factor until you get a best value for the index. </div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/solihinho.wordpress.com/784/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/solihinho.wordpress.com/784/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/solihinho.wordpress.com/784/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/solihinho.wordpress.com/784/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/solihinho.wordpress.com/784/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/solihinho.wordpress.com/784/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/solihinho.wordpress.com/784/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/solihinho.wordpress.com/784/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/solihinho.wordpress.com/784/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/solihinho.wordpress.com/784/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/solihinho.wordpress.com/784/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/solihinho.wordpress.com/784/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/solihinho.wordpress.com/784/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/solihinho.wordpress.com/784/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=solihinho.wordpress.com&amp;blog=4251551&amp;post=784&amp;subd=solihinho&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://solihinho.wordpress.com/2009/02/10/review-fill-factor-of-the-indexes/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">solihinho</media:title>
		</media:content>

		<media:content url="http://solihinho.files.wordpress.com/2009/02/analyze.jpg" medium="image">
			<media:title type="html">analyze</media:title>
		</media:content>
	</item>
		<item>
		<title>Missing an Index? Check for it ..</title>
		<link>http://solihinho.wordpress.com/2009/01/31/missing-an-index-check-for-it/</link>
		<comments>http://solihinho.wordpress.com/2009/01/31/missing-an-index-check-for-it/#comments</comments>
		<pubDate>Fri, 30 Jan 2009 17:14:52 +0000</pubDate>
		<dc:creator>solihinho</dc:creator>
				<category><![CDATA[Script]]></category>
		<category><![CDATA[Index]]></category>
		<category><![CDATA[SQL Server 2005]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false">http://solihinho.wordpress.com/2009/01/31/missing-an-index-check-for-it/</guid>
		<description><![CDATA[Behind the sceneThere was a story when I created this script. There was an issue on our production server. After investigated, there was no index on one table but the table has an index on testing environment. Looks like when published the table, the DBA missed publish the index. After this issue, my supervisor asked [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=solihinho.wordpress.com&amp;blog=4251551&amp;post=780&amp;subd=solihinho&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div style="font-size:12px;color:black;font-family:lucida sans unicode;text-align:justify;"><img style="border-width:0;margin:10px 10px 10px 0;" height="152" alt="SqlServer2005" src="http://solihinho.files.wordpress.com/2009/01/sqlserver2005.jpg?w=120&#038;h=152" width="120" align="left" border="0"> <br /><span style="font-weight:bold;font-size:16px;color:#004466;font-family:lucida handwriting;">Behind the scene</span><br />There was a story when I created this script. There was an issue on our production server. After investigated, there was no index on one table but the table has an index on testing environment. Looks like when published the table, the DBA missed publish the index. After this issue, my supervisor asked me to create a script for listing all tables that do not have an index. Every table should be have an index obviously. Below script could not run on SQL Server 2000 and previous version. </p>
<p><span style="font-weight:bold;font-size:16px;color:#004466;font-family:lucida handwriting;">Script </span><br />Below the script for list index availability of the tables. </p>
<p><em>Result of the script</em><br /> <br />
<table style="margin-top:10px;margin-bottom:10px;margin-left:50px;font-family:tahoma;background-color:#ffffcc;text-align:left;" cellspacing="0" cellpadding="5" width="75%" border="1">
<tbody>
<tr style="font-weight:bold;font-size:14px;color:#ffffff;background-color:#000000;">
<td>Column Name</td>
<td>Description</td>
</tr>
<tr>
<td>TableName</td>
<td>Name of the table</td>
</tr>
<tr>
<td>SchemaName</td>
<td>Name of the schema</td>
</tr>
<tr>
<td>HasIndex</td>
<td>Yes if the table has an index vice versa</td>
</tr>
<tr>
<td>IndexName</td>
<td>Name of the index</td>
</tr>
<tr>
<td>IndexKeys</td>
<td>Keys of the index</td>
</tr>
<tr>
<td>IsPrimaryKey</td>
<td>Is this index a primary key?</td>
</tr>
<tr>
<td>IndexType</td>
<td>Type of the index whether clustered or non clustered</td>
</tr>
</tbody>
</table>
<table style="margin-top:10px;margin-bottom:10px;margin-left:30px;" width="80%">
<tbody>
<tr>
<td style="font-size:12px;color:black;font-family:courier new;background-color:#ffffcc;border-style:double;padding:5px 5px 5px 10px;">
<pre><font color="#008000">/*
Created by : Solihin ho - http://solihinho.wordpress.com

Compatibility : SQL Server 2005 and next
*/</font>

<font color="#0000ff">DECLARE</font> @ObjectID <font color="#0000ff">INT</font>, @IndexID <font color="#0000ff">INT</font>
<font color="#0000ff">DECLARE</font> @ObjectName <font color="#0000ff">SYSNAME</font>, @SchemaName <font color="#0000ff">SYSNAME</font>
<font color="#0000ff">DECLARE</font> @ColumnName <font color="#0000ff">SYSNAME</font>
<font color="#0000ff">DECLARE</font> @IndexKeys  <font color="#0000ff">NVARCHAR</font>(1000)

<font color="#0000ff">IF</font> <font color="#ff00ff">OBJECT_ID</font> (<font color="#ff0000">'tempdb..#Result'</font>) <font color="#808080">IS NOT NULL</font>
  <font color="#0000ff">DROP TABLE</font> #Result

<font color="#0000ff">CREATE TABLE</font> #Result
(
   ObjectID     <font color="#0000ff">INT</font>,
   ObjectName   <font color="#0000ff">SYSNAME</font>,
   SchemaName   <font color="#0000ff">SYSNAME</font>,
   IndexId      <font color="#0000ff">INT</font>,
   IndexName    <font color="#0000ff">SYSNAME</font> NULL,
   IndexType    <font color="#0000ff">NVARCHAR</font>(60),
   IndexKeys    <font color="#0000ff">NVARCHAR</font>(1000),
   HasIndex     <font color="#0000ff">VARCHAR</font>(3),
   IsPrimaryKey <font color="#0000ff">BIT</font>
)

<font color="#0000ff">INSERT INTO</font> #Result (ObjectID, ObjectName, SchemaName, IndexId
           , IndexName, IndexType, HasIndex, IsPrimaryKey)
<font color="#0000ff">SELECT</font> o.object_id, o.name <font color="#0000ff">AS</font> ObjectName
  , s.name as SchemaName
  , i.index_id, i.name <font color="#0000ff">AS</font> IndexName
  , i.type_desc
  , <font color="#ff0000">'No'</font>
  , i.is_primary_key
<font color="#0000ff">FROM</font> sys.objects o
<font color="#0000ff">INNER JOIN</font> sys.schemas s <font color="#0000ff">ON</font> o.schema_id = s.schema_id
<font color="#0000ff">INNER JOIN</font> sys.indexes i <font color="#0000ff">ON</font> o.object_id = i.object_id
<font color="#0000ff">WHERE</font> o.type = <font color="#ff0000">'U'</font>

<font color="#008000">--Index Keys</font>
<font color="#0000ff">IF</font> (<font color="#ff00ff">OBJECT_ID</font>(<font color="#ff0000">'tempdb..#keys_temp'</font>) <font color="#808080">IS NOT NULL</font>)
   <font color="#0000ff">DROP TABLE</font> #keys_temp

<font color="#0000ff">SELECT</font> r.ObjectID, r.IndexId, c.name <font color="#0000ff">AS</font> ColumnName
<font color="#0000ff">INTO</font> #keys_temp
<font color="#0000ff">FROM</font> #Result r
<font color="#0000ff">LEFT OUTER JOIN</font> sys.index_columns ic
   <font color="#0000ff">ON</font> r.ObjectID = ic.object_id <font color="#0000ff">AND</font> r.IndexID = ic.index_id
<font color="#0000ff">LEFT OUTER JOIN</font> sys.columns c
   <font color="#0000ff">ON</font> c.column_id = ic.column_id <font color="#0000ff">AND</font> r.ObjectID = c.object_id
<font color="#0000ff">WHERE</font> r.IndexID &gt; 0

<font color="#0000ff">WHILE</font> (1 = 1)
<font color="#0000ff">BEGIN</font>

<font color="#0000ff">SET</font> @IndexKeys = ''
<font color="#0000ff">SET</font> @ObjectID = NULL
<font color="#0000ff">SET</font> @IndexID = NULL

<font color="#0000ff">SELECT TOP</font> 1 @ObjectID = ObjectID, @IndexID = IndexID
<font color="#0000ff">FROM</font> #keys_temp
<font color="#0000ff">ORDER BY</font> ObjectID, IndexID

<font color="#0000ff">IF</font> @ObjectID IS NULL
   <font color="#0000ff">GOTO</font> _Loop1

<font color="#0000ff">WHILE</font> (1 = 1)
<font color="#0000ff">BEGIN</font>

   <font color="#0000ff">SET</font> @ColumnName = <font color="#808080">NULL</font>

   <font color="#0000ff">SELECT TOP 1</font> @ColumnName = ColumnName
   <font color="#0000ff">FROM</font> #keys_temp
   <font color="#0000ff">WHERE</font> ObjectID = @ObjectID <font color="#0000ff">AND</font> IndexID = @IndexID
   <font color="#0000ff">ORDER BY</font> ColumnName

   <font color="#0000ff">IF</font> @ColumnName <font color="#808080">IS NULL</font>
     <font color="#0000ff">GOTO</font> _Loop2

   <font color="#0000ff">SET</font> @IndexKeys = @IndexKeys + @ColumnName + <font color="#ff0000">', '</font>

   <font color="#0000ff">DELETE FROM</font> #keys_temp
   <font color="#0000ff">WHERE</font> ObjectID = @ObjectID <font color="#0000ff">AND</font> IndexID = @IndexID
     <font color="#0000ff">AND</font> ColumnName = @ColumnName
<font color="#0000ff">END</font>

_Loop2:
   <font color="#0000ff">UPDATE</font> #Result SET IndexKeys = <font color="#0000ff">LEFT</font>(@IndexKeys, LEN(@IndexKeys)-1)
   <font color="#0000ff">WHERE</font> ObjectID = @ObjectID <font color="#0000ff">AND</font> IndexID = @IndexID

<font color="#0000ff">END</font>
_Loop1:
   <font color="#0000ff">DROP TABLE</font> #keys_temp

   <font color="#0000ff">IF</font> <font color="#ff00ff">OBJECT_ID</font>(<font color="#ff0000">'tempdb..#object_temp'</font>) <font color="#808080">IS NOT NULL</font>
      <font color="#0000ff">DROP TABLE</font> #object_temp

   <font color="#0000ff">SELECT DISTINCT</font> ObjectName, SchemaName <font color="#0000ff">INTO</font> #object_temp
   FROM #Result

   <font color="#0000ff">WHILE</font> (1 = 1)
   <font color="#0000ff">BEGIN</font>
      <font color="#0000ff">SET</font> @ObjectName = <font color="#808080">NULL</font>

      <font color="#0000ff">SELECT TOP 1</font> @ObjectName = ObjectName, @SchemaName = SchemaName
      <font color="#0000ff">FROM</font> #object_temp
      <font color="#0000ff">ORDER BY</font> ObjectName, SchemaName

      <font color="#0000ff">IF</font> @ObjectName <font color="#808080">IS NULL</font>
         <font color="#0000ff">GOTO</font> _Loop3

      <font color="#0000ff">IF EXISTS</font> (<font color="#0000ff">SELECT</font> * <font color="#0000ff">FROM</font> #Result <font color="#0000ff">WHERE</font> ObjectName = @ObjectName
                        <font color="#0000ff">AND</font> SchemaName = @SchemaName
                        <font color="#0000ff">AND</font> IndexID &gt; 0)
      <font color="#0000ff">BEGIN</font>
         <font color="#0000ff">UPDATE</font> #Result <font color="#0000ff">SET</font> HasIndex = <font color="#ff0000">'Yes'</font>
         <font color="#0000ff">WHERE</font> ObjectName = @ObjectName <font color="#0000ff">AND</font> SchemaName = @SchemaName
      <font color="#0000ff">END</font> 

      <font color="#0000ff">DELETE FROM</font> #object_temp <font color="#0000ff">WHERE</font> ObjectName = @ObjectName
               <font color="#0000ff">AND</font> SchemaName = @SchemaName
   <font color="#0000ff">END</font>

_Loop3:

   <font color="#0000ff">SELECT</font> ObjectName <font color="#0000ff">AS</font> TableName
   , SchemaName
   , HasIndex
   , IndexName
   , IndexKeys
   , <font color="#0000ff">CASE WHEN</font> IsPrimaryKey = 1 Then <font color="#ff0000">'Yes'</font> ELSE <font color="#ff0000">'No'</font> <font color="#0000ff">END AS</font> IsPrimaryKey
   , IndexType
   <font color="#0000ff">FROM</font> #Result
   <font color="#0000ff">ORDER BY</font> ObjectName, SchemaName
</pre>
</td>
</tr>
</tbody>
</table>
<p>This is the result when I ran the script on AdventureWorks database.<br /><img style="border-width:0;" height="482" alt="result" src="http://solihinho.files.wordpress.com/2009/01/result2.jpg?w=827&#038;h=482" width="827" border="0"> </div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/solihinho.wordpress.com/780/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/solihinho.wordpress.com/780/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/solihinho.wordpress.com/780/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/solihinho.wordpress.com/780/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/solihinho.wordpress.com/780/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/solihinho.wordpress.com/780/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/solihinho.wordpress.com/780/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/solihinho.wordpress.com/780/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/solihinho.wordpress.com/780/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/solihinho.wordpress.com/780/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/solihinho.wordpress.com/780/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/solihinho.wordpress.com/780/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/solihinho.wordpress.com/780/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/solihinho.wordpress.com/780/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=solihinho.wordpress.com&amp;blog=4251551&amp;post=780&amp;subd=solihinho&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://solihinho.wordpress.com/2009/01/31/missing-an-index-check-for-it/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">solihinho</media:title>
		</media:content>

		<media:content url="http://solihinho.files.wordpress.com/2009/01/sqlserver2005.jpg" medium="image">
			<media:title type="html">SqlServer2005</media:title>
		</media:content>

		<media:content url="http://solihinho.files.wordpress.com/2009/01/result2.jpg" medium="image">
			<media:title type="html">result</media:title>
		</media:content>
	</item>
		<item>
		<title>Backup Log with NO_LOG, Use It Smartly</title>
		<link>http://solihinho.wordpress.com/2009/01/16/backup-log-with-no_log-use-it-smartly/</link>
		<comments>http://solihinho.wordpress.com/2009/01/16/backup-log-with-no_log-use-it-smartly/#comments</comments>
		<pubDate>Thu, 15 Jan 2009 18:30:52 +0000</pubDate>
		<dc:creator>solihinho</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Transaction Log]]></category>

		<guid isPermaLink="false">http://solihinho.wordpress.com/2009/01/16/backup-log-with-no_log-use-it-smartly/</guid>
		<description><![CDATA[Background On my prior posting, I&#8217;ve explain about how to restore data using transaction log. I&#8217;ve experience when I tried to restore my detrimental database, in the middle of restored the transaction log it was failed. Seems the chain of the transaction log was broken. So I could not restore the data as much as [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=solihinho.wordpress.com&amp;blog=4251551&amp;post=769&amp;subd=solihinho&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div style="font-size:12px;color:black;font-family:lucida sans unicode;text-align:justify;"><img style="border-width:0;margin:10px 10px 10px 0;" height="139" alt="danger" src="http://solihinho.files.wordpress.com/2009/01/danger.jpg?w=196&#038;h=139" width="196" align="left" border="0"> <span style="font-weight:bold;font-size:16px;color:#004466;font-family:lucida handwriting;">Background </span><br />On my prior <a href="http://solihinho.wordpress.com/2009/01/10/restore-database-to-the-point-in-time-using-transaction-log/" target="_blank">posting</a>, I&#8217;ve explain about how to restore data using transaction log. I&#8217;ve experience when I tried to restore my detrimental database, in the middle of restored the transaction log it was failed. Seems the chain of the transaction log was broken. So I could not restore the data as much as I could. After investigation, there is a SQL job that backup databases log using NO_LOG or TRUNCATE_ONLY option and shrink the log files. Just FYI I use SQL Server 2000. We&#8217;ll see an enhancement from next version of SQL Server later. </p>
<p>I reckoned this job created by the earlier DBA because in our database server since we have an issue with disk space that using for database log. But it still little strange because there is another job that already backup the log to another disk. So I guess the DBA didn&#8217;t know the consequence of this action and so did I before this issue came up. I thought shrink log file and backup log with no_log is one whole packet. We could not shrink the log since the log space should be truncate first. It will truncate the log space if we backup the database or backup the log either we backup it to another device or backup log with no_log. <br /> <br />
<table style="margin-top:10px;margin-bottom:10px;margin-left:30px;" width="70%">
<tbody>
<tr>
<td style="font-size:10px;color:black;font-family:lucida sans unicode;background-color:#cccccc;text-align:justify;border-color:red;border-style:dotted;padding:5px 10px;">To see log size and percentage the usage use command <strong>DBCC SQLPERF (LOGSPACE) . </strong>We only can shrink free space of the log. </td>
</tr>
</tbody>
</table>
<p><span style="font-weight:bold;font-size:16px;color:#004466;font-family:lucida handwriting;">Scenario </span><br />Let&#8217;s see below scenario. 1st I will run it on SQL Server 2000</p>
<table style="margin-top:10px;margin-bottom:10px;margin-left:30px;" width="80%">
<tbody>
<tr>
<td style="font-size:12px;color:black;font-family:courier new;background-color:#ffffcc;border-style:double;padding:5px 5px 5px 10px;">
<pre><font color="#008080">--1. Create TestLog Database
</font><font color="#0000ff">CREATE DATABASE</font> TestDB
GO

<font color="#008080">--2. Active on TestLog database and set for FULL recovery model</font>
use TestDB
GO

<font color="#008080">--Recovery model should be FULL</font>
<font color="#0000ff">ALTER DATABASE</font> TestDB <font color="#0000ff">SET RECOVERY FULL</font>
GO

<font color="#008080">--3. Create Table</font>
<font color="#0000ff">CREATE TABLE</font> TestTbl
(
   ID           <font color="#0000ff">INT</font>,
   Description  <font color="#0000ff">CHAR</font>(100)
)
GO

<font color="#008080">--4. Insert Data</font>
<font color="#0000ff">INSERT INTO</font> TestTbl <font color="#0000ff">VALUES</font>(1, <font color="#ff0000">'This is first record'</font>)
GO

<font color="#008080">--5. Backup Full Database</font>
<font color="#0000ff">BACKUP DATABASE</font> TestDB <font color="#0000ff">TO DISK</font> =<font color="#ff0000"> 'C:\temp\BK_FULL.bak'</font>
GO

<font color="#008080">--6. Insert Another Data</font>
<font color="#0000ff">INSERT INTO</font> TestTbl <font color="#0000ff">VALUES</font>(2, <font color="#ff0000">'This is 2nd record'</font>)
GO

<font color="#008080">--7. Backup log to disk #1</font>
<font color="#0000ff">BACKUP LOG</font> TestDB <font color="#0000ff">TO DISK</font> = <font color="#ff0000">'C:\temp\BK_LOG.bak'</font>
GO

<font color="#008080">--8. Insert Data again</font>
<font color="#0000ff">INSERT INTO</font> TestTbl <font color="#0000ff">VALUES</font> (3, <font color="#ff0000">'This is 3rd record'</font>)
GO

<font color="#008080">--9. Backup log to disk #2</font>
<font color="#0000ff">BACKUP LOG</font> TestDB <font color="#0000ff">TO DISK</font> = <font color="#ff0000">'C:\temp\BK_LOG.bak'</font>
GO

<font color="#008080">--10. Insert Data again</font>
<font color="#0000ff">INSERT INTO</font> TestTbl <font color="#0000ff">VALUES</font> (4, <font color="#ff0000">'This is 4rd record'</font>)
GO

<font color="#008080">--11. Upss disk nearly full, DBA do shrink with no_log and shrink the file</font>
<font color="#0000ff">BACKUP LOG</font> TestDB <font color="#0000ff">WITH NO_LOG</font>
GO
<font color="#0000ff">DBCC SHRINKFILE</font>(<font color="#ff0000">'TestDB_Log'</font>)
GO

<font color="#008080">--12. Insert Data again</font>
<font color="#0000ff">INSERT INTO</font> TestTbl <font color="#0000ff">VALUES</font> (5, <font color="#ff0000">'This is 5th record'</font>)
GO

<font color="#008080">--13. Backup log to disk #3</font>
<font color="#0000ff">BACKUP LOG</font> TestDB <font color="#0000ff">TO DISK</font> = <font color="#ff0000">'C:\temp\BK_LOG.bak'</font>
GO

<font color="#008080">--14. Another Upss .. DBA delete all data</font>
<font color="#0000ff">DELETE FROM</font> TestTbl
GO

<font color="#008080">--15. Let's analyze the backup file</font>
<font color="#0000ff">RESTORE HEADERONLY FROM DISK</font> = <font color="#ff0000">'C:\temp\BK_FULL.bak'</font>
<font color="#0000ff">RESTORE HEADERONLY FROM DISK</font> = <font color="#ff0000">'C:\temp\BK_LOG.bak'</font>
</pre>
</td>
</tr>
</tbody>
</table>
<p>And Here is the result.<br />Let&#8217;s analyze the highlight result. #1 LastLsn is similar with #2 FirstLsn but #2 LastLsn is not similar with #3 FirstLsn, It is caused by Backup Log with NO_LOG. It cause the chain of the transaction log is broken and we cannot restore&nbsp; transaction log #3 and the following files. </p>
<p>&nbsp;<img style="border-width:0;" height="451" alt="result" src="http://solihinho.files.wordpress.com/2009/01/result.jpg?w=644&#038;h=451" width="644" border="0"> </p>
<p>Now, I&#8217;ll try to run this scenario in SQL Server 2005 and here is the result. As we see, 1st and 2nd backup log is successful. But when trying backup log for the 3rd times it is failed because before run the 3rd backup log we have already run backup log with no_log. SQL Server 2005 does not allow the transaction log file broken. It is really useful enhancement from SQL Server in order to make sure that our transaction log is always is one chain and we are as DBA know if there is an issue in our backup transaction log.<br /><a href="http://solihinho.files.wordpress.com/2009/01/result1.jpg"><img style="border-width:0;margin:10px 0 10px 10px;" height="472" alt="result" src="http://solihinho.files.wordpress.com/2009/01/result-thumb.jpg?w=644&#038;h=472" width="644" border="0"></a> </div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/solihinho.wordpress.com/769/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/solihinho.wordpress.com/769/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/solihinho.wordpress.com/769/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/solihinho.wordpress.com/769/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/solihinho.wordpress.com/769/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/solihinho.wordpress.com/769/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/solihinho.wordpress.com/769/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/solihinho.wordpress.com/769/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/solihinho.wordpress.com/769/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/solihinho.wordpress.com/769/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/solihinho.wordpress.com/769/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/solihinho.wordpress.com/769/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/solihinho.wordpress.com/769/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/solihinho.wordpress.com/769/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=solihinho.wordpress.com&amp;blog=4251551&amp;post=769&amp;subd=solihinho&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://solihinho.wordpress.com/2009/01/16/backup-log-with-no_log-use-it-smartly/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">solihinho</media:title>
		</media:content>

		<media:content url="http://solihinho.files.wordpress.com/2009/01/danger.jpg" medium="image">
			<media:title type="html">danger</media:title>
		</media:content>

		<media:content url="http://solihinho.files.wordpress.com/2009/01/result.jpg" medium="image">
			<media:title type="html">result</media:title>
		</media:content>

		<media:content url="http://solihinho.files.wordpress.com/2009/01/result-thumb.jpg" medium="image">
			<media:title type="html">result</media:title>
		</media:content>
	</item>
		<item>
		<title>Restore Database to the Point in Time Using Transaction Log</title>
		<link>http://solihinho.wordpress.com/2009/01/10/restore-database-to-the-point-in-time-using-transaction-log/</link>
		<comments>http://solihinho.wordpress.com/2009/01/10/restore-database-to-the-point-in-time-using-transaction-log/#comments</comments>
		<pubDate>Sat, 10 Jan 2009 12:12:37 +0000</pubDate>
		<dc:creator>solihinho</dc:creator>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[Disaster Recovery]]></category>
		<category><![CDATA[Transaction Log]]></category>

		<guid isPermaLink="false">http://solihinho.wordpress.com/2009/01/10/restore-database-to-the-point-in-time-using-transaction-log/</guid>
		<description><![CDATA[Real World As a DBA, it is very important to be able recover a damaged database that caused with many factors. It can be caused by hardware (hard disk) failure, electricity problem, software bugs, and human error. Rather than hardware error, human error is #1 factor that could make database damage e.g: delete data accidentally, [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=solihinho.wordpress.com&amp;blog=4251551&amp;post=715&amp;subd=solihinho&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div style="font-size:12px;color:black;font-family:lucida sans unicode;text-align:justify;">
<div class="wlWriterSmartContent" id="scid:8747F07C-CDE8-481f-B0DF-C6CFD074BF67:0c01ea61-7434-44ce-9c9f-500ecacb1641" style="display:inline;float:left;margin:0;padding:0;"><img border="0" src="http://solihinho.files.wordpress.com/2009/01/stupid-recovery1.jpg" /></div>
<p><span style="font-weight:bold;font-size:16px;color:#004466;font-family:lucida handwriting;">Real World </span><br />As a DBA, it is very important to be able recover a damaged database that caused with many factors. It can be caused by hardware (hard disk) failure, electricity problem, software bugs, and human error. Rather than hardware error, human error is #1 factor that could make database damage e.g: delete data accidentally, drop database, wrong update data etc.</p>
<p>Some situations need DBA could recovery the data until specified time before the database was damaged. There is some technics to make it possible but this time what we discuss is using database transaction log. What we need to do for the recovery is restore database from full database backup + differential database backup + transaction log backup.</p>
<p>DBA should review the DR (Disaster Recovery) strategy periodically. The nice DR strategy is when the disaster occurred, the recovery succeed recover data as much as possible. We need to try simulate the disaster and try to recover the data. It is important to make sure that our DR strategy procedure is already correct and not miss something. </p>
<p><span style="font-weight:bold;font-size:16px;color:#004466;font-family:lucida handwriting;">Backup Strategy </span><br />Before I explain about the recovery, it is better that we know the backup strategy. </p>
<p>1. Full backup every week (usually on weekend)<br />
<table style="margin-top:0;margin-bottom:0;margin-left:20px;" width="70%">
<tbody>
<tr>
<td style="font-size:10px;color:black;font-family:lucida sans unicode;background-color:#cccccc;text-align:justify;border-color:red;border-style:dotted;padding:5px 10px;">BACKUP DATABASE &lt;database name&gt; TO &lt;backup device&gt; </td>
</tr>
</tbody>
</table>
<p>2. Differential backup every day<br />
<table style="margin-top:0;margin-bottom:0;margin-left:20px;" width="70%">
<tbody>
<tr>
<td style="font-size:10px;color:black;font-family:lucida sans unicode;background-color:#cccccc;text-align:justify;border-color:red;border-style:dotted;padding:5px 10px;">BACKUP DATABASE &lt;database name&gt; TO &lt;backup device&gt; WITH DIFFERENTIAL</td>
</tr>
</tbody>
</table>
<p>3. Transaction Log backup every x hours / x minutes<br />
<table style="margin-top:0;margin-bottom:0;margin-left:20px;" width="70%">
<tbody>
<tr>
<td style="font-size:10px;color:black;font-family:lucida sans unicode;background-color:#cccccc;text-align:justify;border-color:red;border-style:dotted;padding:5px 10px;">BACKUP LOG &lt;database name&gt; TO &lt;backup device&gt; </td>
</tr>
</tbody>
</table>
<p><span style="font-weight:bold;font-size:16px;color:#004466;font-family:lucida handwriting;">Recovery Strategy </span><br />1. Restore Full Backup <br />2. Restore latest Differential Backup which is running after the full backup on step 1<br />&nbsp;&nbsp;&nbsp; e.g. : if full backup on Sunday Night and Diff backup everyday night , when the disaster occur on Thursday we just use the differential backup on Wednesday. <br />3. Restore Transaction Backup which is running after latest any database backup model&nbsp;&nbsp; <br />&nbsp;&nbsp;&nbsp; Restore log little bit difficult than just full and diff. We have to find the continuous transaction log with the latest backup. BACKUP LOG with option NO_LOG or TRUNCATE_ONLY should break the chain.<br />
<table style="margin-top:5px;margin-bottom:5px;margin-left:20px;" width="70%">
<tbody>
<tr>
<td style="font-size:10px;color:black;font-family:lucida sans unicode;background-color:#cccccc;text-align:justify;border-color:red;border-style:dotted;padding:5px 10px;">
<pre><em>Statement for Restore from FULL and DIFFERENTIAL backup:</em>
<strong>RESTORE DATABASE &lt;database_name&gt; FROM &lt;backup_device&gt; </strong>

<em>Statement for restore TRANSACTION LOG backup:</em>
<strong>RESTORE LOG &lt;database_name&gt; FROM &lt;backup_device&gt;</strong>

For further option, read SQL Server BOL.</pre>
</td>
</tr>
</tbody>
</table>
<p>Note to RECOVERY / NORECOVERY option, use NORECOVERY option if there is another backup file that should be restored. Use RECOVERY if the final file backup have restored.</p>
<p><span style="font-weight:bold;font-size:16px;color:#004466;font-family:lucida handwriting;">Requisite </span><br />Database recovery model should not be in SIMPLE mode. You can use ALTER DATABASE to change the recovery model. If you try to backup log database with SIMPLE recovery model, error should be occurred. Note if you use BULK_LOGGED recovery model, you cannot restore to the point in time if there are any bulk logged operations in transaction log.&nbsp;&nbsp;<br />
<blockquote>
<p><font color="#ff0000" size="1"><em>Msg 4208, Level 16, State 1, Line 1<br />The statement BACKUP LOG is not allowed while the recovery model is SIMPLE. Use BACKUP DATABASE or change the recovery model using ALTER DATABASE.<br />Msg 3013, Level 16, State 1, Line 1<br />BACKUP LOG is terminating abnormally.</em></font></p>
</blockquote>
<p><span style="font-weight:bold;font-size:16px;color:#004466;font-family:lucida handwriting;">Disaster Recovery Scenario</span><br />As mention on backup strategy, we full backup every weekend,&nbsp; differential backup every night on weekdays and backup log every hours. Below script is illustration about the disaster where the DBA delete all data accidentally. DBA have to recovery the database with minimal loss data. We should recover the data to the point in time before the disaster happened. </p>
<p><strong>Disaster Scenario</strong> </p>
<table style="margin-top:10px;margin-bottom:10px;margin-left:20px;" width="80%">
<tbody>
<tr>
<td style="font-size:12px;color:black;font-family:courier new;background-color:#ffffcc;border-style:double;padding:5px 5px 5px 10px;">
<pre><font color="#008000">--1. Create Database DR Testing</font>
<font color="#0000ff">CREATE DATABASE</font> TestDR;
GO

<font color="#008000">--2. Active on TestDR database</font>
<font color="#0000ff">USE</font> TestDR
GO
<font color="#008000">--Recovery model should be FULL</font>
<font color="#0000ff">ALTER DATABASE</font> TestDR <font color="#0000ff">SET RECOVERY FULL</font>
GO

<font color="#008000">--3. Create Table</font>
<font color="#0000ff">CREATE TABLE</font> Orders
(
    Code       <font color="#0000ff">INT PRIMARY KEY</font>,
    Timestamp  <font color="#0000ff">DATETIME</font>
)
GO

<font color="#008000">--4. Assume there are data already exists in this table</font>
<font color="#0000ff">INSERT INTO</font> dbo.Orders <font color="#0000ff">VALUES</font> (1, <font color="#ff0000">'3 Jan 2009 09:00:00.000'</font>)
<font color="#0000ff">INSERT INTO</font> dbo.Orders <font color="#0000ff">VALUES</font> (2, <font color="#ff0000">'3 Jan 2009 12:00:00.000'</font>)
<font color="#0000ff">INSERT INTO</font> dbo.Orders <font color="#0000ff">VALUES</font> (3, <font color="#ff0000">'3 Jan 2009 16:00:00.000'</font>)
GO

<font color="#008000">--5. Full backup run every sunday on 4 Jan 2009 to C:\temp\TestDR_Full.bak</font>
<font color="#0000ff">BACKUP DATABASE</font> TestDR <font color="#0000ff">TO DISK</font> = <font color="#ff0000">'C:\temp\BK_Full_090104.bak'</font>
   <font color="#0000ff">WITH DESCRIPTION</font> = <font color="#ff0000">'Full Backup ON 4 Jan 2009'</font>, <font color="#0000ff">NAME</font> = <font color="#ff0000">'Full Backup'</font>
GO

<font color="#008000">--6. Transactions on 5 Jan 2009 and BACKUP LOG every hours</font>
<font color="#0000ff">INSERT INTO</font> dbo.Orders <font color="#0000ff">VALUES</font> (4, <font color="#ff0000">'5 Jan 2009 07:45:00.000'</font>)
GO
<font color="#0000ff">BACKUP LOG</font> TestDR <font color="#0000ff">TO DISK</font> = <font color="#ff0000">'C:\temp\BK_Log_090105.bak'</font>
    <font color="#0000ff">WITH DESCRIPTION</font> = <font color="#ff0000">'Log Backup ON 5 Jan 2009 08:00:00.000'</font>, <font color="#0000ff">NAME</font> = <font color="#ff0000">'Log Backup'</font>
GO
<font color="#0000ff">INSERT INTO</font> dbo.Orders <font color="#0000ff">VALUES</font> (5, <font color="#ff0000">'5 Jan 2009 11:30:00.000'</font>)
GO
<font color="#0000ff">BACKUP LOG</font> TestDR <font color="#0000ff">TO DISK</font> = <font color="#ff0000">'C:\temp\BK_Log_090105.bak'</font>
   <font color="#0000ff">WITH DESCRIPTION</font> = <font color="#ff0000">'Log Backup ON 5 Jan 2009 12:00:00.000'</font>, <font color="#0000ff">NAME</font> = <font color="#ff0000">'Log Backup'</font>
GO

<font color="#008000">--7. Backup Diff after office hours</font>
<font color="#0000ff">BACKUP DATABASE</font> TestDR <font color="#0000ff">TO DISK</font> = <font color="#ff0000">'C:\temp\BK_DIFF_090105.bak'</font>
    <font color="#0000ff">WITH DIFFERENTIAL</font>, <font color="#0000ff">DESCRIPTION</font> = <font color="#ff0000">'Diff Backup ON 5 Jan 2009 22:00:00.000'</font>
   ,<font color="#0000ff">NAME</font> = <font color="#ff0000">'Diff Backup'</font>

<font color="#008000">--8. Transactions on 6 Jan 2009 and BACKUP LOG every hours</font>
<font color="#0000ff">INSERT INTO</font> dbo.Orders <font color="#0000ff">VALUES</font> (6, <font color="#ff0000">'6 Jan 2009 09:35:00.000'</font>)
GO
<font color="#0000ff">BACKUP LOG</font> TestDR <font color="#0000ff">TO DISK</font> = <font color="#ff0000">'C:\temp\BK_Log_090106.bak'</font>
    <font color="#0000ff">WITH DESCRIPTION</font> = <font color="#ff0000">'Log Backup ON 6 Jan 2009 10:00:00.000'</font>, <font color="#0000ff">NAME</font> = <font color="#ff0000">'Log Backup'</font>
GO
<font color="#0000ff">INSERT INTO</font> dbo.Orders <font color="#0000ff">VALUES</font> (7, <font color="#ff0000">'6 Jan 2009 13:10:00.000'</font>)
GO
<font color="#0000ff">BACKUP LOG</font> TestDR <font color="#0000ff">TO DISK</font> = <font color="#0000ff">'C:\temp\BK_Log_090106.bak'</font>
    <font color="#0000ff">WITH DESCRIPTION</font> = <font color="#ff0000">'Log Backup ON 6 Jan 2009 14:00:00.000'</font>, <font color="#0000ff">NAME</font> = <font color="#ff0000">'Log Backup'</font>
GO
<font color="#0000ff">INSERT INTO</font> dbo.Orders <font color="#0000ff">VALUES</font> (7, <font color="#ff0000">'6 Jan 2009 14:23:00.000'</font>)
GO

<font color="#008000">--9. 14.35 AM, DBA delete all data accidentally.</font>
<font color="#0000ff">DELETE FROM</font> dbo.Orders
</pre>
</td>
</tr>
</tbody>
</table>
<p><strong>Recovery Scenario</strong><br />Before we restore the database, awhile we review what we have and need to do for recovery. <br />1. We need latest full backup. We have full backup at Jan 4th, 2009 on BK_Full_090104.bak<br />2. Since disaster occur on Tuesday Jan 6th, 2009 we have to apply latest differential backup which is on Jan 5th, 2009 on BK_DIFF_090105.bak <br />3.&nbsp; Last but not least is we also need apply the transaction log backup which is on Jan 6th, 2009 on BK_LOG_090106.bak. Since we&nbsp; backup the transaction log twice, we need to apply every file that we backup before we set the database to RECOVERY mode. If the transaction log backup file not in the chain, it should be show the error message like below </p>
<blockquote>
<p><em><font color="#ff0000"><font size="1">Msg 4326, Level 16, State 1, Line 1<br />The log in this backup set terminates at LSN 27000000017500001, which is too early to apply to the database. <br />A more recent log backup that includes LSN 27000000019300001 can be restored.<br />Msg 3013, Level 16, State 1, Line 1<br />RESTORE LOG is terminating abnormally</font>.</font></em> </p>
</blockquote>
<table style="margin-top:10px;margin-bottom:10px;margin-left:20px;" width="80%">
<tbody>
<tr>
<td style="font-size:12px;color:black;font-family:courier new;background-color:#ffffcc;border-style:double;padding:5px 5px 5px 10px;">
<pre><font color="#008000">--1. Restore from Full Backup With NORECOVERY option</font>
<font color="#0000ff">RESTORE DATABASE</font> TestDR
  <font color="#0000ff">FROM DISK</font> = <font color="#ff0000">'C:\Temp\BK_FULL_090104.bak'</font>
  <font color="#0000ff">WITH NORECOVERY</font>
GO

<font color="#008000">--2. Restore from latest Diff Backup WITH NORECOVERY option</font>
<font color="#0000ff">RESTORE DATABASE</font> TestDR
  <font color="#0000ff">FROM DISK</font> = <font color="#ff0000">'C:\Temp\BK_DIFF_090105.bak'</font>
  <font color="#0000ff">WITH NORECOVERY</font>
GO

<font color="#008000">--3. Restore from Transaction Log File 1 and File 2</font>
<font color="#0000ff">RESTORE LOG</font> TestDR
  <font color="#0000ff">FROM DISK</font> = <font color="#ff0000">'C:\Temp\BK_LOG_090106.bak'</font>
  <font color="#0000ff">WITH NORECOVERY</font>, <font color="#0000ff">File</font> = 1
GO

<font color="#0000ff">RESTORE LOG</font> TestDR
  <font color="#0000ff">FROM DISK</font> = <font color="#ff0000">'C:\Temp\BK_LOG_090106.bak'</font>
  <font color="#0000ff">WITH NORECOVERY</font>, <font color="#0000ff">File</font> = 2
GO

<font color="#008000">--4. Use RECOVERY option so that the database is available again</font>
<font color="#0000ff">RESTORE DATABASE</font> TestDR <font color="#0000ff">WITH RECOVERY</font>
GO
</pre>
</td>
</tr>
</tbody>
</table>
<table style="margin-top:10px;margin-bottom:10px;margin-left:40px;" width="80%">
<tbody>
<tr>
<td style="font-size:10px;color:black;font-family:lucida sans unicode;background-color:#cccccc;text-align:justify;border-color:red;border-style:dotted;padding:5px 10px;"><img alt="Star" src="http://us.i1.yimg.com/us.yimg.com/i/mesg/emoticons7/79.gif"> Recovery scenario above assume TestDR database is not exist anymore in the server (DROP DATABASE). If you want restore as another database, use MOVE option since we need change the filename for the restored database. Using RESTORE FILELISTONLY to get information about the physical and logical name of the backup device,</td>
</tr>
</tbody>
</table>
<p><span style="font-weight:bold;font-size:16px;color:#004466;font-family:lucida handwriting;">Restore HeaderOnly</span> <br />If you see the recovery scenario is quite simple, isn&#8217;t it? But if in the real world I believe the scenario more complex. To analyze it is possible or not to restore until the specified time, we can use RESTORE HEADERONLY statement. Restore HeaderOnly returns a result set containing all the backup header information for all backup sets on a particular backup device. With this information, you can decide the file # that you need to be restored. There is information about what time the backup&nbsp; was performed. This information is needed by DBA that want restore the data until specified time. </p>
<table style="margin-top:0;margin-bottom:5px;margin-left:20px;" width="50%">
<tbody>
<tr>
<td style="font-size:10px;color:black;font-family:lucida sans unicode;background-color:#cccccc;text-align:justify;border-color:red;border-style:dotted;padding:5px 10px;">RESTORE HEADERONLY FROM &lt;backup device&gt;</td>
</tr>
</tbody>
</table>
<p>Let&#8217;s examine the FirstLSN, LastLSN, and DatabaseBackupLSN column from above scenario.<br /><a href="http://solihinho.files.wordpress.com/2009/01/restore-headeronly.jpg"><img style="border-width:0;margin:10px 5px 5px 10px;" height="309" alt="restore headeronly" src="http://solihinho.files.wordpress.com/2009/01/restore-headeronly-thumb.jpg?w=710&#038;h=309" width="710" border="0"></a> <br />DatabaseBackupLSN contains LSN (Log Sequence Number) of the most recent full database backup. In above picture, we can see that Differential backup and Transaction Log backup contain value FirstLSN of full database backup. It means our differential backup and transaction log backup in the same chain with the full database backup. So BK_DIFF_090105.bak could be restored after we restore the full database backup.</p>
<p>Now we check the transaction log backup file. Note the LastLSN of the differential backup. The value should be in the range between FirstLSN and LastLSN on transaction log. We can see 27000000019300001 in the range between 27000000017600001 and 27000000019400001 in transaction log file #1. So file #1 could be restored. How about file #2. Note the FirstLSN of file #2 should be as same as LastLSN of file #1. So file #2 of transaction log could be restored also. </p>
<p>BACKUP LOG WITH NO_LOG could make the chain of the transaction log break. The value between files in transaction log won&#8217;t be continuous (FirstLSN file N should be same with LastLSN file N-1 where N &gt; 1).&nbsp;&nbsp; </p></div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/solihinho.wordpress.com/715/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/solihinho.wordpress.com/715/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/solihinho.wordpress.com/715/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/solihinho.wordpress.com/715/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/solihinho.wordpress.com/715/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/solihinho.wordpress.com/715/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/solihinho.wordpress.com/715/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/solihinho.wordpress.com/715/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/solihinho.wordpress.com/715/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/solihinho.wordpress.com/715/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/solihinho.wordpress.com/715/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/solihinho.wordpress.com/715/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/solihinho.wordpress.com/715/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/solihinho.wordpress.com/715/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=solihinho.wordpress.com&amp;blog=4251551&amp;post=715&amp;subd=solihinho&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://solihinho.wordpress.com/2009/01/10/restore-database-to-the-point-in-time-using-transaction-log/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">solihinho</media:title>
		</media:content>

		<media:content url="http://solihinho.files.wordpress.com/2009/01/stupid-recovery1.jpg" medium="image" />

		<media:content url="http://us.i1.yimg.com/us.yimg.com/i/mesg/emoticons7/79.gif" medium="image">
			<media:title type="html">Star</media:title>
		</media:content>

		<media:content url="http://solihinho.files.wordpress.com/2009/01/restore-headeronly-thumb.jpg" medium="image">
			<media:title type="html">restore headeronly</media:title>
		</media:content>
	</item>
		<item>
		<title>Query For Listing SQL Server Job Schedule</title>
		<link>http://solihinho.wordpress.com/2009/01/01/query-for-listing-sql-server-job-schedule/</link>
		<comments>http://solihinho.wordpress.com/2009/01/01/query-for-listing-sql-server-job-schedule/#comments</comments>
		<pubDate>Thu, 01 Jan 2009 03:03:20 +0000</pubDate>
		<dc:creator>solihinho</dc:creator>
				<category><![CDATA[Script]]></category>
		<category><![CDATA[Job Schedule]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false">http://solihinho.wordpress.com/2008/12/25/sql-server-2000-query-job-scheduler/</guid>
		<description><![CDATA[BackgroundOn this occasion I want to share TSQL script to list Job Scheduler in SQL Server 2000 and next version. If using Enterprise Manager (EM), we can access under Management -&#62; SQL Server Agent -&#62; Jobs (see below picture). &#160; If using SQL Server Management Studio, it can be accessed under SQL Server Agent -&#62; [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=solihinho.wordpress.com&amp;blog=4251551&amp;post=685&amp;subd=solihinho&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div style="text-align:justify;font-family:lucida sans unicode;color:black;font-size:12px;"><img style="border-width:0;margin:10px 10px 10px 0;" border="0" alt="scheduler" align="left" src="http://solihinho.files.wordpress.com/2008/12/orgscheduler.jpg?w=163&#038;h=131" width="163" height="131"> <span style="font-family:lucida handwriting;color:#004466;font-size:16px;font-weight:bold;">Background</span><br />On this occasion I want to share TSQL script to list Job Scheduler in SQL Server 2000 and next version. If using Enterprise Manager (EM), we can access under <em>Management -&gt; SQL Server Agent -&gt; Jobs</em> (see below picture). <br />&nbsp;<img style="border-width:0;margin:10px 0 0;" border="0" alt="Job" src="http://solihinho.files.wordpress.com/2008/12/job2.jpg?w=561&#038;h=176" width="561" height="176"> </p>
<p>If using SQL Server Management Studio, it can be accessed under <em>SQL Server Agent -&gt; Jobs (see below picture)</em><br /><img style="border-width:0;margin:10px 0 5px;" border="0" alt="schedule 2005" src="http://solihinho.files.wordpress.com/2009/01/schedule-20051.jpg?w=614&#038;h=258" width="614" height="258"> </p>
<p>The issue is little difficult for me to review the schedule of the jobs. I have to click the each job and take note of each schedule. Since all these jobs keep on <em>MSDB</em> database, we are able to query the jobs within its schedule. When we have many jobs both maintenance or monitoring job etc, as a DBA we have to review the time when the jobs will be running. It is important to make our SQL Server load is balance which is mean not very heavy in one time but very light in another time. </p>
<p>The jobs general information keep in <em>msdb.dbo.sysjobs</em> table and the schedule keep in <em>msdb.dbo.sysjobschedules (SQL 2000) or msdb.dbo.sysschedules (SQL 2005 and next)</em>. Read SQL Server Book Online (BOL) for further information about the columns of these tables. </p>
<p><span style="font-family:lucida handwriting;color:#004466;font-size:16px;font-weight:bold;">Script </span></p>
<p>To make the main script simpler, I create 3 functions. Just run below script on <em>master </em>database. Actually you can it to another database and don&#8217;t forget to change the main query. These 3 functions valid for SQL Server 2000 and next. But there is little differences on the main query since there is a difference schema on table <em>msdb.dbo.sysjobschedules</em>.</p>
<table style="margin-top:10px;margin-bottom:10px;margin-left:50px;" width="70%">
<tbody>
<tr>
<td style="background-color:#ffffcc;font-family:courier new;color:black;font-size:12px;border-style:double;padding:5px 5px 5px 10px;">
<pre><font color="#0000ff">USE</font> master

GO

<font color="#0000ff">CREATE  FUNCTION</font> fn_freq_interval_desc(@freq_interval <font color="#0000ff">INT</font>)
<font color="#0000ff">RETURNS VARCHAR</font>(1000)
<font color="#0000ff">AS
BEGIN</font>
   <font color="#0000ff">DECLARE</font> @result <font color="#0000ff">VARCHAR</font>(1000)  

   <font color="#0000ff">SET</font> @result = <font color="#ff0000">''</font>

   <font color="#0000ff">IF</font> (@freq_interval &amp; 1 = 1)
      SET @result = <font color="#ff0000">'Sunday, '</font>
   <font color="#0000ff">IF</font> (@freq_interval &amp; 2 = 2)
      SET @result = @result + <font color="#ff0000">'Monday, '</font>
   <font color="#0000ff">IF</font> (@freq_interval &amp; 4 = 4)
      SET @result = @result + <font color="#ff0000">'Tuesday, '</font>
   <font color="#0000ff">IF</font> (@freq_interval &amp; 8 = 8)
      SET @result = @result + <font color="#ff0000">'Wednesday, '</font>
   <font color="#0000ff">IF</font> (@freq_interval &amp; 16 = 16)
      SET @result = @result + <font color="#ff0000">'Thursday, '</font>
   <font color="#0000ff">IF</font> (@freq_interval &amp; 32 = 32)
      SET @result = @result + <font color="#ff0000">'Friday, '</font>
   <font color="#0000ff">IF</font> (@freq_interval &amp; 64 = 64)
      SET @result = @result + <font color="#ff0000">'Saturday, '</font>  

   <font color="#0000ff">RETURN</font>(<font color="#ff00ff">LEFT</font>(@result,<font color="#ff00ff">LEN</font>(@result)-1))
<font color="#0000ff">END</font>   

GO

<font color="#0000ff">CREATE FUNCTION</font> fn_Time2Str(@time <font color="#0000ff">INT</font>)
<font color="#0000ff">RETURNS VARCHAR</font>(10)
<font color="#0000ff">AS</font>
<font color="#0000ff">BEGIN</font>
   <font color="#0000ff">DECLARE</font> @strtime <font color="#0000ff">CHAR</font>(6)
   <font color="#0000ff">SET</font> @strtime = <font color="#ff00ff">RIGHT</font>(<font color="#ff0000">'000000'</font> + <font color="#ff00ff">CONVERT</font>(<font color="#0000ff">VARCHAR</font>,@time),6)

   <font color="#0000ff">RETURN</font> <font color="#ff00ff">LEFT</font>(@strtime,2) + <font color="#ff0000">':' </font>+ <font color="#ff00ff">SUBSTRING</font>(@strtime,3,2) + <font color="#ff0000">':' </font>+ <font color="#ff00ff">RIGHT</font>(@strtime,2)
<font color="#0000ff">END</font>

GO	

<font color="#0000ff">CREATE FUNCTION</font> fn_Date2Str(@date <font color="#0000ff">INT</font>)
<font color="#0000ff">RETURNS VARCHAR</font>(10)
<font color="#0000ff">AS</font>
<font color="#0000ff">BEGIN</font>
   <font color="#0000ff">DECLARE</font> @strdate <font color="#0000ff">CHAR</font>(8)
   SET @strdate = LEFT(CONVERT(<font color="#0000ff">VARCHAR</font>,@date) + '00000000', 8)

   <font color="#0000ff">RETURN RIGHT</font>(@strdate,2) + <font color="#ff0000">'/'</font> + <font color="#ff00ff">SUBSTRING</font>(@strdate,5,2) + <font color="#ff0000">'/' </font>+ <font color="#ff00ff">LEFT</font>(@strdate,4)
<font color="#0000ff">END</font>
</pre>
</td>
</tr>
</tbody>
</table>
<p>Main query for <strong><font size="4">SQL Server 2000</font></strong><br /><em>revise: May 18, 2009 : fixed inaccurate next run date</em> </p>
<table style="margin-top:10px;margin-bottom:10px;margin-left:50px;" width="70%">
<tbody>
<tr>
<td style="background-color:#ffffcc;font-family:courier new;color:black;font-size:12px;border-style:double;padding:5px 5px 5px 10px;">
<pre><font color="#008000">/*
  Created by Solihin Ho - http://solihinho.wordpress.com

</a>  Usage : Change the value of variable @Filter
          'Y' --&gt; display only enabled job
          'N' --&gt; display only disabled job
          'A' --&gt; display all job
          'X' --&gt; display job which is duration already end
*/</font>

<font color="#0000ff">DECLARE</font> @Filter <font color="#0000ff">CHAR</font>(1)
<font color="#0000ff">SET</font> @Filter = <font color="#ff0000">'A'</font>

<font color="#0000ff">DECLARE</font> @sql <font color="#0000ff">VARCHAR</font>(8000)
<font color="#0000ff">DECLARE</font> @is_sysadmin <font color="#0000ff">INT</font>
<font color="#0000ff">DECLARE</font> @job_owner   <font color="#0000ff">sysname</font>

<font color="#0000ff">IF</font> <font color="#ff00ff">OBJECT_ID</font>(<font color="#ff0000">'tempdb..#xp_results'</font>) <font color="#808080">IS NOT NULL</font>
<font color="#0000ff">BEGIN</font>
    DROP TABLE #xp_results
END

<font color="#0000ff">CREATE TABLE</font> #xp_results (
     job_id                <font color="#0000ff">UNIQUEIDENTIFIER</font> NOT NULL,
     last_run_date         <font color="#0000ff">INT</font>              NOT NULL,
     last_run_time         <font color="#0000ff">INT</font>              NOT NULL,
     next_run_date         <font color="#0000ff">INT</font>              NOT NULL,
     next_run_time         <font color="#0000ff">INT</font>              NOT NULL,
     next_run_schedule_id  <font color="#0000ff">INT</font>              NOT NULL,
     requested_to_run      <font color="#0000ff">INT</font>              NOT NULL,
     request_source        <font color="#0000ff">INT</font>              NOT NULL,
     request_source_id     <font color="#0000ff">sysname </font>         COLLATE database_default NULL,
     running               <font color="#0000ff">INT</font>              NOT NULL,
     current_step          <font color="#0000ff">INT</font>              NOT NULL,
     current_retry_attempt <font color="#0000ff">INT</font>              NOT NULL,
     job_state             <font color="#0000ff">INT </font>             NOT NULL
)

<font color="#0000ff">SELECT</font> @is_sysadmin = <font color="#ff00ff">ISNULL</font>(<font color="#ff00ff">IS_SRVROLEMEMBER</font>(<font color="#ff0000">N'sysadmin'</font>), 0)
<font color="#0000ff">SELECT</font> @job_owner = <font color="#ff00ff">SUSER_SNAME</font>()

<font color="#0000ff">INSERT INTO</font> #xp_results
    <font color="#0000ff">EXECUTE</font> master.dbo.xp_sqlagent_enum_jobs @is_sysadmin, @job_owner	

<font color="#0000ff">SET</font> @sql = <font color="#ff0000">'
SELECT
  j.Name AS JobName
, c.Name AS Category
, CASE j.enabled WHEN 1 THEN ''Yes'' else ''No'' END as Enabled
, CASE s.enabled WHEN 1 THEN ''Yes'' else ''No'' END as Scheduled
, j.Description
, CASE s.freq_type
     WHEN  1 THEN ''Once''
     WHEN  4 THEN ''Daily''
     WHEN  8 THEN ''Weekly''
     WHEN 16 THEN ''Monthly''
     WHEN 32 THEN ''Monthly relative''
     WHEN 64 THEN ''When SQL Server Agent starts''
     WHEN 128 THEN ''Start whenever the CPU(s) become idle'' END as Occurs
, CASE s.freq_type
     WHEN  1 THEN ''O''
     WHEN  4 THEN ''Every ''
        + convert(varchar,s.freq_interval)
        + '' day(s)''
     WHEN  8 THEN ''Every ''
        + convert(varchar,s.freq_recurrence_factor)
        + '' weeks(s) on ''
        + master.dbo.fn_freq_interval_desc(s.freq_interval)
     WHEN 16 THEN ''Day '' + convert(varchar,s.freq_interval)
        + '' of every ''
        + convert(varchar,s.freq_recurrence_factor)
        + '' month(s)''
     WHEN 32 THEN ''The ''
        + CASE s.freq_relative_interval
            WHEN  1 THEN ''First''
            WHEN  2 THEN ''Second''
            WHEN  4 THEN ''Third''
            WHEN  8 THEN ''Fourth''
            WHEN 16 THEN ''Last'' END
        + CASE s.freq_interval
            WHEN  1 THEN '' Sunday''
            WHEN  2 THEN '' Monday''
            WHEN  3 THEN '' Tuesday''
            WHEN  4 THEN '' Wednesday''
            WHEN  5 THEN '' Thursday''
            WHEN  6 THEN '' Friday''
            WHEN  7 THEN '' Saturday''
            WHEN  8 THEN '' Day''
            WHEN  9 THEN '' Weekday''
            WHEN 10 THEN '' Weekend Day'' END
        + '' of every ''
        + convert(varchar,s.freq_recurrence_factor)
        + '' month(s)'' END AS Occurs_detail
, CASE s.freq_subday_type
     WHEN 1 THEN ''Occurs once at ''
        + master.dbo.fn_Time2Str(s.active_start_time)
     WHEN 2 THEN ''Occurs every ''
        + convert(varchar,s.freq_subday_interval)
        + '' Seconds(s) Starting at ''
        + master.dbo.fn_Time2Str(s.active_start_time)
        + '' ending at ''
        + master.dbo.fn_Time2Str(s.active_end_time)
     WHEN 4 THEN ''Occurs every ''
        + convert(varchar,s.freq_subday_interval)
        + '' Minute(s) Starting at ''
        + master.dbo.fn_Time2Str(s.active_start_time)
        + '' ending at ''
        + master.dbo.fn_Time2Str(s.active_end_time)
     WHEN 8 THEN ''Occurs every ''
        + convert(varchar,s.freq_subday_interval)
        + '' Hour(s) Starting at ''
        + master.dbo.fn_Time2Str(s.active_start_time)
        + '' ending at ''
        + master.dbo.fn_Time2Str(s.active_end_time) END AS Frequency
, CASE WHEN s.freq_type =  1 THEN ''On date: ''
          + master.dbo.fn_Date2Str(active_start_date)
          + '' At time: ''
          + master.dbo.fn_Time2Str(s.active_start_time)
       WHEN s.freq_type &lt; 64 THEN ''Start date: ''
          + master.dbo.fn_Date2Str(s.active_start_date)
          + '' end date: ''
          + master.dbo.fn_Date2Str(s.active_end_date) END as Duration
, master.dbo.fn_Date2Str(xp.next_run_date) + '' ''
    + master.dbo.fn_Time2Str(xp.next_run_time) AS Next_Run_Date
FROM  msdb.dbo.sysjobs j (NOLOCK)
INNER JOIN msdb.dbo.sysjobschedules s (nolock) ON j.job_id = s.job_id
INNER JOIN msdb.dbo.syscategories c (NOLOCK) ON j.category_id = c.category_id
INNER JOIN #xp_results xp (NOLOCK) ON j.job_id = xp.job_id
WHERE 1 = 1
@Filter
ORDER BY j.name'</font>

<font color="#0000ff">IF</font> @Filter = <font color="#ff0000">'Y'</font>
   <font color="#0000ff">SET</font> @sql = <font color="#ff00ff">REPLACE</font>(@sql,<font color="#ff0000">'@Filter'</font>,<font color="#ff0000">' AND j.enabled = 1 '</font>)
<font color="#0000ff">ELSE</font>
<font color="#0000ff">IF</font> @Filter = <font color="#ff0000">'N'</font>
   <font color="#0000ff">SET</font> @sql = <font color="#ff00ff">REPLACE</font>(@sql,<font color="#ff0000">'@Filter'</font>,<font color="#ff0000">' AND j.enabled = 0 '</font>)
<font color="#0000ff">ELSE</font>
<font color="#0000ff">IF</font> @Filter = <font color="#ff0000">'X'</font>
   <font color="#0000ff">SET</font> @sql = <font color="#ff00ff">REPLACE</font>(@sql,<font color="#ff0000">'@Filter'</font>,
                            <font color="#ff0000">'AND s.active_end_date &lt; convert(varchar(8),GetDate(),112) '</font>)
<font color="#0000ff">ELSE</font>
   <font color="#0000ff">SET</font> @sql = <font color="#ff00ff">REPLACE</font>(@sql,<font color="#ff0000">'@Filter'</font>,<font color="#ff0000">''</font>)

<font color="#0000ff">EXEC</font>(@sql)
</pre>
</td>
</tr>
</tbody>
</table>
<p>Main Query for <strong><font size="4">SQL Server 2005 and next version</font></strong> </p>
<table style="margin-top:10px;margin-bottom:10px;margin-left:50px;" width="70%">
<tbody>
<tr>
<td style="background-color:#ffffcc;font-family:courier new;color:black;font-size:12px;border-style:double;padding:5px 5px 5px 10px;">
<pre><font color="#008000">/*
  Created by Solihin Ho - http://solihinho.wordpress.com

</a>  Usage : Change the value of variable @Filter
          'Y' --&gt; display only enabled job
          'N' --&gt; display only disabled job
          'A' --&gt; display all job
          'X' --&gt; display job which is duration already end
*/</font>

<font color="#0000ff">DECLARE</font> @Filter <font color="#0000ff">CHAR</font>(1)
<font color="#0000ff">SET</font> @Filter = <font color="#ff0000">'A'</font>

<font color="#0000ff">DECLARE</font> @sql <font color="#0000ff">VARCHAR</font>(8000)
<font color="#0000ff">DECLARE</font> @is_sysadmin <font color="#0000ff">INT</font>
<font color="#0000ff">DECLARE</font> @job_owner   <font color="#0000ff">sysname</font>

<font color="#0000ff">IF</font> <font color="#ff00ff">OBJECT_ID</font>(<font color="#ff0000">'tempdb..#xp_results'</font>) IS NOT NULL
<font color="#0000ff">BEGIN</font>
    <font color="#0000ff">DROP TABLE</font> #xp_results
<font color="#0000ff">END</font>

<font color="#0000ff">CREATE TABLE</font> #xp_results (
     job_id                <font color="#0000ff">UNIQUEIDENTIFIER</font> NOT NULL,
     last_run_date         <font color="#0000ff">INT</font>              NOT NULL,
     last_run_time         <font color="#0000ff">INT</font>              NOT NULL,
     next_run_date         <font color="#0000ff">INT</font>              NOT NULL,
     next_run_time         <font color="#0000ff">INT</font>              NOT NULL,
     next_run_schedule_id  <font color="#0000ff">INT</font>              NOT NULL,
     requested_to_run      <font color="#0000ff">INT</font>              NOT NULL,
     request_source        <font color="#0000ff">INT</font>              NOT NULL,
     request_source_id     <font color="#0000ff">sysname</font>          COLLATE database_default NULL,
     running               <font color="#0000ff">INT</font>              NOT NULL,
     current_step          <font color="#0000ff">INT</font>              NOT NULL,
     current_retry_attempt <font color="#0000ff">INT</font>              NOT NULL,
     job_state             <font color="#0000ff">INT</font>              NOT NULL)

<font color="#0000ff">SELECT</font> @is_sysadmin = <font color="#ff00ff">ISNULL</font>(<font color="#ff00ff">IS_SRVROLEMEMBER</font>(N<font color="#ff0000">'sysadmin'</font>), 0)
<font color="#0000ff">SELECT</font> @job_owner = <font color="#ff00ff">SUSER_SNAME</font>()

<font color="#0000ff">INSERT INTO</font> #xp_results
    <font color="#0000ff">EXECUTE</font> master.dbo.xp_sqlagent_enum_jobs @is_sysadmin, @job_owner

<font color="#0000ff">SET</font> @sql = <font color="#ff0000">'
SELECT
  j.Name AS JobName
, c.Name AS Category
, CASE j.enabled WHEN 1 THEN ''Yes'' else ''No'' END as Enabled
, CASE s.enabled WHEN 1 THEN ''Yes'' else ''No'' END as Scheduled
, j.Description
, CASE s.freq_type
     WHEN  1 THEN ''Once''
     WHEN  4 THEN ''Daily''
     WHEN  8 THEN ''Weekly''
     WHEN 16 THEN ''Monthly''
     WHEN 32 THEN ''Monthly relative''
     WHEN 64 THEN ''When SQL Server Agent starts''
     WHEN 128 THEN ''Start whenever the CPU(s) become idle'' END as Occurs
, CASE s.freq_type
     WHEN  1 THEN ''O''
     WHEN  4 THEN ''Every ''
        + convert(varchar,s.freq_interval)
        + '' day(s)''
     WHEN  8 THEN ''Every ''
        + convert(varchar,s.freq_recurrence_factor)
        + '' weeks(s) on ''
        + master.dbo.fn_freq_interval_desc(s.freq_interval)
     WHEN 16 THEN ''Day '' + convert(varchar,s.freq_interval)
        + '' of every ''
        + convert(varchar,s.freq_recurrence_factor)
        + '' month(s)''
     WHEN 32 THEN ''The ''
        + CASE s.freq_relative_interval
            WHEN  1 THEN ''First''
            WHEN  2 THEN ''Second''
            WHEN  4 THEN ''Third''
            WHEN  8 THEN ''Fourth''
            WHEN 16 THEN ''Last'' END
        + CASE s.freq_interval
            WHEN  1 THEN '' Sunday''
            WHEN  2 THEN '' Monday''
            WHEN  3 THEN '' Tuesday''
            WHEN  4 THEN '' Wednesday''
            WHEN  5 THEN '' Thursday''
            WHEN  6 THEN '' Friday''
            WHEN  7 THEN '' Saturday''
            WHEN  8 THEN '' Day''
            WHEN  9 THEN '' Weekday''
            WHEN 10 THEN '' Weekend Day'' END
        + '' of every ''
        + convert(varchar,s.freq_recurrence_factor)
        + '' month(s)'' END AS Occurs_detail
, CASE s.freq_subday_type
     WHEN 1 THEN ''Occurs once at ''
        + master.dbo.fn_Time2Str(s.active_start_time)
     WHEN 2 THEN ''Occurs every ''
        + convert(varchar,s.freq_subday_interval)
        + '' Seconds(s) Starting at ''
        + master.dbo.fn_Time2Str(s.active_start_time)
        + '' ending at ''
        + master.dbo.fn_Time2Str(s.active_end_time)
     WHEN 4 THEN ''Occurs every ''
        + convert(varchar,s.freq_subday_interval)
        + '' Minute(s) Starting at ''
        + master.dbo.fn_Time2Str(s.active_start_time)
        + '' ending at ''
        + master.dbo.fn_Time2Str(s.active_end_time)
     WHEN 8 THEN ''Occurs every ''
        + convert(varchar,s.freq_subday_interval)
        + '' Hour(s) Starting at ''
        + master.dbo.fn_Time2Str(s.active_start_time)
        + '' ending at ''
        + master.dbo.fn_Time2Str(s.active_end_time) END AS Frequency
, CASE WHEN s.freq_type =  1 THEN ''On date: ''
          + master.dbo.fn_Date2Str(s.active_start_date)
          + '' At time: ''
          + master.dbo.fn_Time2Str(s.active_start_time)
       WHEN s.freq_type &lt; 64 THEN ''Start date: ''
          + master.dbo.fn_Date2Str(s.active_start_date)
          + '' end date: ''
          + master.dbo.fn_Date2Str(s.active_end_date) END as Duration
, master.dbo.fn_Date2Str(xp.next_run_date) + '' ''
    + master.dbo.fn_Time2Str(xp.next_run_time) AS Next_Run_Date
FROM  msdb.dbo.sysjobs j (NOLOCK)
INNER JOIN msdb.dbo.sysjobschedules js (NOLOCK) ON j.job_id = js.job_id
INNER JOIN msdb.dbo.sysschedules s (NOLOCK) ON js.schedule_id = s.schedule_id
INNER JOIN msdb.dbo.syscategories c (NOLOCK) ON j.category_id = c.category_id
INNER JOIN #xp_results xp (NOLOCK) ON j.job_id = xp.job_id
WHERE 1 = 1
@Filter
ORDER BY j.name'</font>

<font color="#0000ff">IF</font> @Filter = <font color="#ff0000">'Y'</font>
   <font color="#0000ff">SET</font> @sql = <font color="#ff00ff">REPLACE</font>(@sql,<font color="#ff0000">'@Filter'</font>,<font color="#ff0000">' AND j.enabled = 1 '</font>)
<font color="#0000ff">ELSE</font>
<font color="#0000ff">IF</font> @Filter = <font color="#ff0000">'N'</font>
   <font color="#0000ff">SET</font> @sql = <font color="#ff00ff">REPLACE</font>(@sql,<font color="#ff0000">'@Filter'</font>,<font color="#ff0000">' AND j.enabled = 0 '</font>)
<font color="#0000ff">ELSE</font>
<font color="#0000ff">IF</font> @Filter = <font color="#ff0000">'X'</font>
   <font color="#0000ff">SET</font> @sql = <font color="#ff00ff">REPLACE</font>(@sql,<font color="#ff0000">'@Filter'</font>,
                            <font color="#ff0000">'AND s.active_end_date &lt; convert(varchar(8),GetDate(),112) '</font>)
<font color="#0000ff">ELSE</font>
   <font color="#0000ff">SET</font> @sql = <font color="#ff00ff">REPLACE</font>(@sql,<font color="#ff0000">'@Filter'</font>,<font color="#ff0000">''</font>)

<font color="#0000ff">EXEC</font>(@sql)
</pre>
</td>
</tr>
</tbody>
</table>
<p>Below sample result of above script <br /><a href="http://solihinho.files.wordpress.com/2008/12/result1.jpg"><img style="border-width:0;margin:5px 0 5px 10px;" border="0" alt="result" src="http://solihinho.files.wordpress.com/2008/12/result-thumb1.jpg?w=740&#038;h=80" width="740" height="80"></a> </div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/solihinho.wordpress.com/685/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/solihinho.wordpress.com/685/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/solihinho.wordpress.com/685/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/solihinho.wordpress.com/685/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/solihinho.wordpress.com/685/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/solihinho.wordpress.com/685/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/solihinho.wordpress.com/685/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/solihinho.wordpress.com/685/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/solihinho.wordpress.com/685/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/solihinho.wordpress.com/685/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/solihinho.wordpress.com/685/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/solihinho.wordpress.com/685/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/solihinho.wordpress.com/685/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/solihinho.wordpress.com/685/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=solihinho.wordpress.com&amp;blog=4251551&amp;post=685&amp;subd=solihinho&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://solihinho.wordpress.com/2009/01/01/query-for-listing-sql-server-job-schedule/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
	
		<media:content url="" medium="image">
			<media:title type="html">solihinho</media:title>
		</media:content>

		<media:content url="http://solihinho.files.wordpress.com/2008/12/orgscheduler.jpg" medium="image">
			<media:title type="html">scheduler</media:title>
		</media:content>

		<media:content url="http://solihinho.files.wordpress.com/2008/12/job2.jpg" medium="image">
			<media:title type="html">Job</media:title>
		</media:content>

		<media:content url="http://solihinho.files.wordpress.com/2009/01/schedule-20051.jpg" medium="image">
			<media:title type="html">schedule 2005</media:title>
		</media:content>

		<media:content url="http://solihinho.files.wordpress.com/2008/12/result-thumb1.jpg" medium="image">
			<media:title type="html">result</media:title>
		</media:content>
	</item>
	</channel>
</rss>
