<?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>Ritesh's Blog for SQL-SERVER &#38; .NET</title>
	<atom:link href="http://riteshshah.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://riteshshah.wordpress.com</link>
	<description>tech articles for SQL-Server and Microsoft .NET technology...</description>
	<lastBuildDate>Fri, 27 Jan 2012 03:26:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='riteshshah.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://1.gravatar.com/blavatar/13ab75c51063885c4201cb78ad40358f?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>Ritesh's Blog for SQL-SERVER &#38; .NET</title>
		<link>http://riteshshah.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://riteshshah.wordpress.com/osd.xml" title="Ritesh&#039;s Blog for SQL-SERVER &#38; .NET" />
	<atom:link rel='hub' href='http://riteshshah.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Book Review – SQL Server Interview Questions and Answers</title>
		<link>http://riteshshah.wordpress.com/2012/01/02/book-review-sql-server-interview-questions-and-answers/</link>
		<comments>http://riteshshah.wordpress.com/2012/01/02/book-review-sql-server-interview-questions-and-answers/#comments</comments>
		<pubDate>Mon, 02 Jan 2012 07:46:00 +0000</pubDate>
		<dc:creator>riteshshah</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://riteshshah.wordpress.com/2012/01/02/book-review-sql-server-interview-questions-and-answers</guid>
		<description><![CDATA[Well, I am not as qualified in SQL Server as the authors ofthis book (Pinal Dave &#38; Vinod Kumar) though I dare to writemy review for his book as both of the authors are in my favourite list and I don’teven miss any article written by them then how could I miss a book written [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=riteshshah.wordpress.com&amp;blog=6754420&amp;post=836&amp;subd=riteshshah&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div dir="ltr" style="text-align:left;"><!--[if gte mso 9]&gt;   Normal  0          false  false  false    EN-IN  X-NONE  X-NONE                                       MicrosoftInternetExplorer4                                     &lt;![endif]--><!--[if gte mso 9]&gt;                                                                                                                                                                                                                                                                                    &lt;![endif]--><!--[if gte mso 10]&gt; /* Style Definitions */ table.MsoNormalTable {mso-style-name:"Table Normal"; mso-tstyle-rowband-size:0; mso-tstyle-colband-size:0; mso-style-noshow:yes; mso-style-priority:99; mso-style-qformat:yes; mso-style-parent:""; mso-padding-alt:0cm 5.4pt 0cm 5.4pt; mso-para-margin:0cm; mso-para-margin-bottom:.0001pt; mso-pagination:widow-orphan; font-size:10.0pt; font-family:"Calibri","sans-serif";}&lt;![endif]-->
<div class="MsoNormal">Well, I am not as qualified in SQL Server as the authors ofthis book (<a href="http://blog.sqlauthority.com/">Pinal Dave</a> &amp; <a href="http://blogs.extremeexperts.com/">Vinod Kumar</a>) though I dare to writemy review for his book as both of the authors are in my favourite list and I don’teven miss any article written by them then how could I miss a book written bythem? After reading the book, I really tempted to share my personal opinion with my blog reader.</div>
<div class="MsoNormal"></div>
<div class="MsoNormal">“SQL Server Interview Questions and Answer” makes firstimpression that this book is written to prepare you for the interview of DBA orSQL Developer or BI professional but NO, this book is not ONLY for those whoare preparing for an interview, even seasoned developer or DBA could refer thisbook to master the basics which we may avoid or forget over the time butknowing those may create a firm ground for the project we all are working on.</div>
<div class="MsoNormal"></div>
<div class="MsoNormal">Both of these authors are well known to present hard &amp;difficult concept in very simple yet powerful manner which directly executeINSERT command in your memory without any trigger or exception, just like straitthrown dart pinch in board, this really show very hard work of both authors.</div>
<div class="MsoNormal"></div>
<div class="MsoNormal">I really impressed with the book for following points.</div>
<div class="MsoNormal"></div>
<ul style="margin-top:0;" type="disc">
<li class="MsoNormal">“Points to Ponder” section     at the end of each chapter as a Quick references to Joes 2 Pros books (I     had privilege to read few of them)</li>
<li class="MsoNormal">Very inspiring quote at     the begging of chapters (I enjoy it in Vinod Kumar’s twits too, in twitter)     </li>
<li class="MsoNormal">Links to SQLAuthorty’s     articles</li>
</ul>
<div class="MsoNormal">When I first heard about this book, I though how much morecontent could be there in this book? As Pinal has already written the seriesarticles on this topic but with my surprise, there are LOT MORE to learn inbook so even if you have read all articles on this series in Pinal’s blog, don’thesitate to have this book, you will have so many (80%) new stuffs to look at.</div>
<div class="MsoNormal"></div>
<div class="MsoNormal">This book scores <b>10/10</b> and I personally highly recommend thisbook as a good &amp; quick reference to any professional who are dealing withSQL Server at any level.</div>
<div class="MsoNormal"></div>
<div class="MsoNormal">To order the copy of the book for your own, visit <b><a href="http://blog.sqlauthority.com/sql-server-books/sql-server-interview-questions-and-answers-for-all-database-developers-and-developers-administrators/">SQLAuthority.com</a></b></div>
<div class="MsoNormal"></div>
<div class="MsoNormal">Happy Reading!!!</div>
<div class="MsoNormal"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><b><span style="color:#333333;font-family:&quot;font-size:12pt;">Reference: Ritesh Shah</span></b><span style="font-family:&quot;font-size:12pt;"></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:12pt;"><a href="http://www.sqlhub.com/"><b><span style="color:blue;">http://www.sqlhub.com</span></b></a><b><span style="color:#333333;"><br />Note: Microsoft Books online is a default reference of all articles butexamples and explanations prepared by Ritesh Shah, founder of </span></b><a href="http://www.sqlhub.com/"><b><span style="color:blue;">http://www.SQLHub.com</span></b></a></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><b><span style="color:red;font-family:&quot;font-size:12pt;">Ask me any SQL Server related question at my “<a href="http://beyondrelational.com/ask/ritesh/default.aspx"><span style="color:blue;">ASK Profile</span></a>”</span></b><span style="font-family:&quot;font-size:12pt;"></span></div>
</div>
<div class="blogger-post-footer">Microsoft SQL Server Blog. Fight the fear of SQL with SQLHub.com. Founder is Ritesh Shah<img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/9075786158122089104-1852727815343800234?l=www.sqlhub.com' alt='' /></div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/riteshshah.wordpress.com/836/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/riteshshah.wordpress.com/836/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/riteshshah.wordpress.com/836/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/riteshshah.wordpress.com/836/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/riteshshah.wordpress.com/836/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/riteshshah.wordpress.com/836/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/riteshshah.wordpress.com/836/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/riteshshah.wordpress.com/836/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/riteshshah.wordpress.com/836/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/riteshshah.wordpress.com/836/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/riteshshah.wordpress.com/836/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/riteshshah.wordpress.com/836/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/riteshshah.wordpress.com/836/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/riteshshah.wordpress.com/836/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=riteshshah.wordpress.com&amp;blog=6754420&amp;post=836&amp;subd=riteshshah&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://riteshshah.wordpress.com/2012/01/02/book-review-sql-server-interview-questions-and-answers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/77d9fe086ee2f3980400752a46f3b66f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Ritesh Shah</media:title>
		</media:content>

		<media:content url="https://blogger.googleusercontent.com/tracker/9075786158122089104-1852727815343800234?l=www.sqlhub.com" medium="image" />
	</item>
		<item>
		<title>Error Fix: Msg 605 Attempt to fetch logical page (3:1307866) in database 6 failed. It belongs to allocation unit 72057594113359872 not to 72057594113490944</title>
		<link>http://riteshshah.wordpress.com/2011/11/03/error-fix-msg-605-attempt-to-fetch-logical-page-31307866-in-database-6-failed-it-belongs-to-allocation-unit-72057594113359872-not-to-72057594113490944/</link>
		<comments>http://riteshshah.wordpress.com/2011/11/03/error-fix-msg-605-attempt-to-fetch-logical-page-31307866-in-database-6-failed-it-belongs-to-allocation-unit-72057594113359872-not-to-72057594113490944/#comments</comments>
		<pubDate>Thu, 03 Nov 2011 03:20:00 +0000</pubDate>
		<dc:creator>riteshshah</dc:creator>
				<category><![CDATA[DBCC CheckDB]]></category>
		<category><![CDATA[DBCC Page]]></category>
		<category><![CDATA[MS SQL Server]]></category>
		<category><![CDATA[Ritesh Shah]]></category>
		<category><![CDATA[dbcc page]]></category>
		<category><![CDATA[dbcc traceon]]></category>

		<guid isPermaLink="false">http://riteshshah.wordpress.com/2011/11/03/error-fix-msg-605-attempt-to-fetch-logical-page-31307866-in-database-6-failed-it-belongs-to-allocation-unit-72057594113359872-not-to-72057594113490944</guid>
		<description><![CDATA[Msg 605, Level 21, State 3, Line 1 Attempt to fetch logical page (3:1307866) in database 6failed. It belongs to allocation unit 72057594113359872 not to72057594113490944. Or Msg 21, Level 21, State 1, Line 1 Warning: Fatal error 605 occurred at OCT  31 2011 6:08AM. Note the error and time, and contact your system administrator. This error [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=riteshshah.wordpress.com&amp;blog=6754420&amp;post=835&amp;subd=riteshshah&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div style="text-align:left;" dir="ltr">
<div class="MsoNormal"></div>
<div class="MsoNormal"><strong>Msg 605, Level 21, State 3, Line 1</strong></div>
<div class="MsoNormal"><strong>Attempt to fetch logical page (3:1307866) in database 6failed. It belongs to allocation unit 72057594113359872 not to72057594113490944.</strong></div>
<div class="MsoNormal"></div>
<div class="MsoNormal">Or</div>
<div class="MsoNormal"></div>
<div class="MsoNormal"><strong><span style="color:red;">Msg 21, Level 21, State 1, Line 1</span></strong></div>
<div class="MsoNormal"><strong><span style="color:red;">Warning: Fatal error 605 occurred at OCT  31 2011 6:08AM. Note the error and time, and contact your system administrator.</span></strong></div>
<div class="MsoNormal"></div>
<div class="MsoNormal">This error comes in red color is disguise, if you login withany user which is not sysadmin type user, you will see this kind of error incorruption of page(s), if you login with SA, Administrator or and otherSysAdmin login, you will not see error comes in red color but the descriptiveerror given in the title</div>
<div class="MsoNormal"></div>
<div class="MsoNormal">There was a festival holidays in our Indian office from 26<sup>th</sup>to 28<sup>th</sup> OCT 2011 (Wed/Friday) so obviously I was out of town andback after 5 days on 31<sup>st</sup> OCT 2011. As soon as I come to the officeand checked few of my email, I suddenly get complain that few pages in oursoftware are throwing an error so I picked up the query which was there inthose pages along with the same parameter they were providing and found theerror given above.</div>
<div class="MsoNormal"></div>
<div class="separator" style="clear:both;text-align:center;"><a style="margin-left:1em;margin-right:1em;" href="http://riteshshah.files.wordpress.com/2011/11/1.jpg"><img src="http://riteshshah.files.wordpress.com/2011/11/1.jpg?w=320&#038;h=164" alt="" width="320" height="164" border="0" /></a></div>
<div class="MsoNormal"></div>
<div class="MsoNormal">As soon as I read this error, I came to know that there is apage corruption in database.  It may bedue to heavy snow fall in NJ in October, they have got power failure there fromfew days and tried to keep the server up and running via power generator. Assoon as Power Generator’s fuel get finished, everything gets down and afterrefuel, they starts everything again. I came to know this as soon as I comeback from holiday. I think this is the Indian New Year gift to me.</div>
<div class="MsoNormal"></div>
<div class="MsoNormal">I tried to gather some more information about the data fileand page number given in error message with DBCC PAGE command.</div>
<div class="MsoNormal"></div>
<div class="MsoNormal"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">dbcc</span><span style="font-family:&quot;font-size:10pt;"> traceon<span style="color:grey;">(</span>3604<span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">dbcc</span><span style="font-family:&quot;font-size:10pt;"> page<span style="color:grey;">(</span><span style="color:red;">&#8216;MyDatabaseName&#8217;</span><span style="color:grey;">,</span>3<span style="color:grey;">,</span>1307866<span style="color:grey;">,</span></span><strong><span style="font-family:&quot;font-size:13pt;">3</span></strong><span style="color:grey;font-family:&quot;font-size:10pt;">)</span></div>
<p><span style="color:blue;font-family:&quot;font-size:10pt;line-height:115%;">dbcc</span><span style="font-family:&quot;font-size:10pt;line-height:115%;"> traceoff<span style="color:grey;">(</span>3604<span style="color:grey;">)</span></span></p>
<div class="MsoNormal"></div>
<div class="MsoNormal"></div>
<div class="MsoNormal"></div>
<div class="MsoNormal">You can use option 0,1,2,3 as the last parameter (bold 3) ofDBCC PAGE command.</div>
<div class="MsoNormal">So now I have two options.</div>
<div class="MsoNormal" style="margin-left:.5in;text-indent:-.25in;"></div>
<div class="MsoNormal" style="margin-left:.5in;text-indent:-.25in;">1.)<span style="font:7pt &quot;">    </span>Restorethat page from full Database backup</div>
<div class="MsoNormal" style="margin-left:.5in;text-indent:-.25in;">2.)<span style="font:7pt &quot;">    </span>TryDBCC commands and if needed, allow data loss</div>
<div class="MsoNormal"></div>
<div class="MsoNormal">I HIGHLY RECOMMEND to go for 1<sup>st</sup> option but ifyou are out of luck and don’t have last full backup, you must have to go forsecond way.</div>
<div class="MsoNormal"></div>
<div class="MsoNormal">I have restored page from my last full database backup withbelow given TSQL.</div>
<div class="MsoNormal"></div>
<div class="MsoNormal"><span style="color:blue;">RESTORE</span> <span style="color:blue;">DATABASE</span> MyDatabaseName<br />
PAGE <span style="color:grey;">=</span> <span style="color:red;">&#8217;3:1307866&#8242;</span><br />
<span style="color:blue;">FROM</span> <span style="color:blue;">DISK</span> <span style="color:grey;">=</span> <span style="color:red;">&#8216;D:\MyDatabaseName.bak&#8217;</span><br />
<span style="color:blue;">WITH</span> NORECOVERY</div>
<div class="MsoNormal"></div>
<div class="MsoNormal">Now, let us talk about second option if you don’t have fullbackup of your database. Use DBCC CHECKDB command.</div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:.0001pt;"><span style="color:green;font-family:&quot;font-size:10pt;">&#8211;checkingdatabase&#8217;s integrity and won&#8217;t show so many informational message,</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:green;font-family:&quot;font-size:10pt;">&#8211;it will onlyshows error messages and warnings.</span></div>
<div class="MsoNormal"><span style="color:blue;font-family:&quot;font-size:10pt;line-height:115%;">DBCC</span><span style="font-family:&quot;font-size:10pt;line-height:115%;"> CHECKDB<span style="color:grey;">(</span><span style="color:red;">&#8216;MyDatabaseName&#8217;</span><span style="color:grey;">)</span> <span style="color:blue;">WITH</span> <span style="color:blue;">NO_INFOMSGS</span></span></div>
<div class="MsoNormal"></div>
<div class="MsoNormal">Once you execute above command, it will recommend you repairlevel. it may recommend REPAIR_REBUILD if you really lucky but if you will seerepair level REPAIR_ALLOW_DATA_LOSS, you have to be ready to lose some of yourdata.</div>
<div class="MsoNormal"></div>
<div class="MsoNormal">You may use either of the below given command based on theRepair Level you have been suggested.</div>
<div class="MsoNormal"></div>
<div class="MsoNormal">1.)</div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">ALTER</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:blue;">DATABASE</span> MyDatabaseName <span style="color:blue;">SET</span><span style="color:blue;">SINGLE_USER</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">GO</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">DBCC</span><span style="font-family:&quot;font-size:10pt;"> CHECKDB<span style="color:grey;">(</span><span style="color:red;">&#8216;MyDatabaseName&#8217;</span><span style="color:grey;">,</span> REPAIR_REBUILD<span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">GO</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">ALTER</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:blue;">database</span> MyDatabaseName <span style="color:blue;">SET</span><span style="color:blue;">MULTI_USER</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">GO</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal"></div>
<div class="MsoNormal">2.)</div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">ALTER</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:blue;">DATABASE</span> MyDatabaseName <span style="color:blue;">SET</span><span style="color:blue;">SINGLE_USER</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">GO</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">DBCC</span><span style="font-family:&quot;font-size:10pt;"> CHECKDB<span style="color:grey;">(</span><span style="color:red;">&#8216;MyDatabaseName&#8217;</span><span style="color:grey;">,</span> REPAIR_ALLOW_DATA_LOSS<span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">GO</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">ALTER</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:blue;">database</span> MyDatabaseName <span style="color:blue;">SET</span><span style="color:blue;">MULTI_USER</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">GO</span></div>
<div class="MsoNormal"></div>
<div class="MsoNormal">Moral of the story is, always have FULL recovery model foryour database and schedule full/ transaction/ differential backup policy. Thisis a MUST DO for any DBAs. You never know, when will you need it!!!!</div>
<div class="MsoNormal"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><strong><span style="color:#333333;font-family:&quot;font-size:12pt;">Reference: Ritesh Shah</span></strong><strong><span style="color:#333333;font-family:&quot;font-size:12pt;"><br />
</span></strong><strong><span style="font-family:&quot;font-size:12pt;"> </span></strong></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:12pt;"><a href="http://www.sqlhub.com/"><strong><span style="color:blue;">http://www.sqlhub.com</span></strong></a><strong><span style="color:#333333;"><br />
Note: Microsoft Books online is a default reference of all articles but examplesand explanations prepared by Ritesh Shah, founder of </span></strong><a href="http://www.sqlhub.com/"><strong><span style="color:blue;">http://www.SQLHub.com</span></strong></a></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><strong><span style="color:red;font-family:&quot;font-size:12pt;">Ask me any SQL Serverrelated question at my “<a href="http://beyondrelational.com/ask/ritesh/default.aspx"><span style="color:blue;">ASK Profile</span></a>”</span></strong></div>
</div>
<div class="blogger-post-footer">Microsoft SQL Server Blog. Fight the fear of SQL with SQLHub.com. Founder is Ritesh Shah<img src="https://blogger.googleusercontent.com/tracker/9075786158122089104-3991458631965220345?l=www.sqlhub.com" alt="" width="1" height="1" /></div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/riteshshah.wordpress.com/835/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/riteshshah.wordpress.com/835/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/riteshshah.wordpress.com/835/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/riteshshah.wordpress.com/835/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/riteshshah.wordpress.com/835/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/riteshshah.wordpress.com/835/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/riteshshah.wordpress.com/835/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/riteshshah.wordpress.com/835/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/riteshshah.wordpress.com/835/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/riteshshah.wordpress.com/835/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/riteshshah.wordpress.com/835/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/riteshshah.wordpress.com/835/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/riteshshah.wordpress.com/835/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/riteshshah.wordpress.com/835/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=riteshshah.wordpress.com&amp;blog=6754420&amp;post=835&amp;subd=riteshshah&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://riteshshah.wordpress.com/2011/11/03/error-fix-msg-605-attempt-to-fetch-logical-page-31307866-in-database-6-failed-it-belongs-to-allocation-unit-72057594113359872-not-to-72057594113490944/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/77d9fe086ee2f3980400752a46f3b66f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Ritesh Shah</media:title>
		</media:content>

		<media:content url="http://riteshshah.files.wordpress.com/2011/11/1.jpg?w=300" medium="image" />

		<media:content url="https://blogger.googleusercontent.com/tracker/9075786158122089104-3991458631965220345?l=www.sqlhub.com" medium="image" />
	</item>
		<item>
		<title>Playing with OPTION KEEPFIXED PLAN hint in SQL Server</title>
		<link>http://riteshshah.wordpress.com/2011/11/02/playing-with-option-keepfixed-plan-hint-in-sql-server-2/</link>
		<comments>http://riteshshah.wordpress.com/2011/11/02/playing-with-option-keepfixed-plan-hint-in-sql-server-2/#comments</comments>
		<pubDate>Wed, 02 Nov 2011 07:26:00 +0000</pubDate>
		<dc:creator>riteshshah</dc:creator>
				<category><![CDATA[hint]]></category>
		<category><![CDATA[KEEPFIXED PLAN]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[MS SQL Server]]></category>
		<category><![CDATA[Ritesh Shah]]></category>

		<guid isPermaLink="false">http://riteshshah.wordpress.com/2011/11/02/playing-with-option-keepfixed-plan-hint-in-sql-server-2</guid>
		<description><![CDATA[My previous article was on Compilation&#38; Recompilation of Stored Procedure. After reading that article, two ofmy reader sent me an email about more clarification on some terminology and thatis the base of writing this article. Actually SQL Server Query Optimizer is really one of thesmart tools which used to find or generate best execution plan [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=riteshshah.wordpress.com&amp;blog=6754420&amp;post=834&amp;subd=riteshshah&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div dir="ltr" style="text-align:left;">
<div class="MsoNormal">My previous article was on <a href="http://www.sqlhub.com/2011/10/some-facts-about-stored-procedure.html">Compilation&amp; Recompilation of Stored Procedure</a>. After reading that article, two ofmy reader sent me an email about more clarification on some terminology and thatis the base of writing this article.</div>
<div class="MsoNormal"></div>
<div class="MsoNormal">Actually SQL Server Query Optimizer is really one of thesmart tools which used to find or generate best execution plan for query or storedprocedure and make the task easy for DBA. Basically it uses Cost BasedOptimizer (CBO) to do this task, even sometime feels to have specific ExecutionPlan for query so Microsoft have provided many different hints at differentlevel.</div>
<div class="MsoNormal"></div>
<div class="MsoNormal">Query Optimizer (QO) used to save execution plan when itcompiles Stored Procedure first time and use the same execution plan when thesame stored procedure is being used again so that you can get rid of overheadof generating execution plan again and again.&nbsp;</div>
<div class="MsoNormal"></div>
<div class="MsoNormal">When index statistics changes heavily, QO used to recompilethe statement to get best plan for your stored procedure but if you are surethat the existing Plan would works fine than one should go for OPTION KEEPFIXEDPLAN for that statement so that you can save your resources and boost upperformance.</div>
<div class="MsoNormal"></div>
<div class="MsoNormal">I will demonstrate this with few queries run in SQL ServerManagement Studio (SSMS) and capture the results in Profiler.&nbsp;</div>
<div class="MsoNormal"></div>
<div class="MsoNormal">I will select following Events in Profiler while creatingnew Trace for my server.</div>
<div class="MsoNormal"></div>
<div class="MsoNormal"><b>Stored Procedure</b></div>
<ul style="margin-top:0;" type="disc">
<li class="MsoNormal">SP:Completed</li>
<li class="MsoNormal">SP:Recompile</li>
<li class="MsoNormal">SP:smtpCompleted</li>
<li class="MsoNormal">SP:smtpStarting</li>
</ul>
<div class="separator" style="clear:both;text-align:center;"></div>
<div class="separator" style="clear:both;text-align:center;"><a href="http://riteshshah.files.wordpress.com/2011/11/13.jpg" style="margin-left:1em;margin-right:1em;"><img border="0" height="200" src="http://riteshshah.files.wordpress.com/2011/11/13.jpg?w=320&#038;h=200" width="320" /></a></div>
<p>&nbsp;
<div class="MsoNormal">Now execute following TSQL in your SSMS.</div>
<div class="MsoNormal"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:green;font-family:&quot;font-size:10pt;">&#8211;create onedatabase which you can delete after running this example</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">create</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:blue;">database</span> SQLHub</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">GO</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">USE</span><span style="font-family:&quot;font-size:10pt;"> SQLHub</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">GO</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:green;font-family:&quot;font-size:10pt;">&#8211;if orderstable is already there. you can delete it than create new one with name&#8221;Orders&#8221;</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">IF</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:magenta;">OBJECT_ID</span><span style="color:grey;">(</span><span style="color:red;">&#8216;orders&#8217;</span><span style="color:grey;">,</span> <span style="color:red;">&#8216;U&#8217;</span><span style="color:grey;">)</span> <span style="color:grey;">IS</span> <span style="color:grey;">NOT</span> <span style="color:grey;">NULL</span> <span style="color:blue;">BEGIN</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:blue;">DROP</span> <span style="color:blue;">TABLE</span> orders</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">END</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">GO</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:green;font-family:&quot;font-size:10pt;">&#8211;creating table</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">CREATE</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:blue;">TABLE</span> orders<span style="color:blue;"> </span><span style="color:grey;">(</span>OrderID <span style="color:blue;">INT</span> <span style="color:blue;">IDENTITY</span><span style="color:grey;">,</span> OrderDate <span style="color:blue;">DATETIME</span><span style="color:grey;">,</span> Amount <span style="color:blue;">MONEY</span><span style="color:grey;">,</span> Refno <span style="color:blue;">INT</span><span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">GO</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:green;font-family:&quot;font-size:10pt;">&#8211;creatingnonclustered index</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">CREATE</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:blue;">NONCLUSTERED</span> <span style="color:blue;">INDEX</span>IDX_ORD <span style="color:blue;">ON</span> Orders<span style="color:grey;">(</span>amount<span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">GO</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:green;font-family:&quot;font-size:10pt;">&#8211;inserting only1 record in our table</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">INSERT</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:blue;">INTO</span> orders <span style="color:blue;">VALUES</span><span style="color:grey;">(</span><span style="color:magenta;">GETDATE</span><span style="color:grey;">(),</span>1001<span style="color:grey;">,</span>111<span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">GO</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:green;font-family:&quot;font-size:10pt;">&#8211;creating SPwhich will SELECT all records from Orders table where Amount is 1001</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">CREATE</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:blue;">PROC</span> spGet_ord</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">AS</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">SELECT</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:grey;">*</span> <span style="color:blue;">FROM</span> orders <span style="color:blue;">WHERE</span> Amount<span style="color:grey;">=</span>1001</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">GO</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:green;font-family:&quot;font-size:10pt;">&#8211;execute SP</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">exec</span><span style="font-family:&quot;font-size:10pt;"> spGet_ord</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">GO</span></div>
<div class="MsoNormal"></div>
<div class="MsoNormal">If you will see in profiler than you would get followingevent captured</div>
<p>
<div class="separator" style="clear:both;text-align:center;"><a href="http://riteshshah.files.wordpress.com/2011/11/3.jpg" style="margin-left:1em;margin-right:1em;"><br /></a></div>
<div class="separator" style="clear:both;text-align:center;"><a href="http://riteshshah.files.wordpress.com/2011/11/2.jpg" style="margin-left:1em;margin-right:1em;"><img border="0" height="49" src="http://riteshshah.files.wordpress.com/2011/11/3.jpg?w=320&#038;h=49" width="320" /></a></div>
<p>
<div class="separator" style="clear:both;text-align:center;"><a href="http://riteshshah.files.wordpress.com/2011/11/2.jpg" style="margin-left:1em;margin-right:1em;"><br /></a></div>
<p>
<div class="MsoNormal"></div>
<div class="MsoNormal">Now, make change in Index statistics by inserting so manyrecords in table so that we can test the effect in profiler after executing SPagain.</div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:green;font-family:&quot;font-size:10pt;">&#8211;inserting50000 fack rows into table</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">INSERT</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:blue;">INTO</span> orders<span style="color:blue;"> </span><span style="color:grey;">(</span>OrderDate<span style="color:grey;">,</span> Amount<span style="color:grey;">,</span> Refno<span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">SELECT</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:blue;">TOP</span> 50000</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:magenta;">DATEADD</span><span style="color:grey;">(</span><span style="color:blue;">minute</span><span style="color:grey;">,</span> <span style="color:magenta;">ABS</span><span style="color:grey;">(</span>a<span style="color:grey;">.</span><span style="color:magenta;">object_id</span> <span style="color:grey;">%</span> 50000 <span style="color:grey;">),</span> <span style="color:magenta;">CAST</span><span style="color:grey;">(</span><span style="color:red;">&#8217;2010-02-01&#8242;</span> <span style="color:blue;">AS</span> <span style="color:blue;">DATETIME</span><span style="color:grey;">)),</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:magenta;">ABS</span><span style="color:grey;">(</span>a<span style="color:grey;">.</span><span style="color:magenta;">object_id</span> <span style="color:grey;">%</span> 10<span style="color:grey;">),</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:magenta;">CAST</span><span style="color:grey;">(</span><span style="color:magenta;">ABS</span><span style="color:grey;">(</span>a<span style="color:grey;">.</span><span style="color:magenta;">object_id</span><span style="color:grey;">)</span> <span style="color:blue;">AS</span> <span style="color:blue;">VARCHAR</span><span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">FROM</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:green;">sys</span><span style="color:grey;">.</span><span style="color:green;">all_objects</span> a</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:grey;font-family:&quot;font-size:10pt;">CROSS</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:grey;">JOIN</span> <span style="color:green;">sys</span><span style="color:grey;">.</span><span style="color:green;">all_objects</span> b</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">GO</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:green;font-family:&quot;font-size:10pt;">&#8211;execute SPagain</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">exec</span><span style="font-family:&quot;font-size:10pt;"> spGet_ord</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">GO</span></div>
<p>
<div class="separator" style="clear:both;text-align:center;"><a href="http://riteshshah.files.wordpress.com/2011/11/3.jpg" style="margin-left:1em;margin-right:1em;"><img border="0" height="63" src="http://riteshshah.files.wordpress.com/2011/11/2.jpg?w=320&#038;h=63" width="320" /></a></div>
<div class="MsoNormal">Now, if you see the profiler, you will see “SP:Recompile”event as your statistics are updated so it need recompilation of statement. If youagain few more thousands record in same table and execute the same SP again,you will see recompilation again.</div>
<div class="MsoNormal"></div>
<div class="MsoNormal">Now, after droping table and SP, we will make one smallchange in Stored Procedure and will use “KEEPFIXED PLAN” hint so that we canavoid recompilation.</div>
<div class="MsoNormal"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">drop</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:blue;">proc</span> spGet_ord</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">GO</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">drop</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:blue;">table</span> orders</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">go</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:green;font-family:&quot;font-size:10pt;">&#8211;creating table</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">CREATE</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:blue;">TABLE</span> orders<span style="color:blue;"> </span><span style="color:grey;">(</span>OrderID <span style="color:blue;">INT</span> <span style="color:blue;">IDENTITY</span><span style="color:grey;">,</span> OrderDate <span style="color:blue;">DATETIME</span><span style="color:grey;">,</span> Amount <span style="color:blue;">MONEY</span><span style="color:grey;">,</span> Refno <span style="color:blue;">INT</span><span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">GO</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">CREATE</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:blue;">NONCLUSTERED</span> <span style="color:blue;">INDEX</span>IDX_ORD <span style="color:blue;">ON</span> Orders<span style="color:grey;">(</span>amount<span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">GO</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">INSERT</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:blue;">INTO</span> orders <span style="color:blue;">VALUES</span><span style="color:grey;">(</span><span style="color:magenta;">GETDATE</span><span style="color:grey;">(),</span>1001<span style="color:grey;">,</span>111<span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">GO</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">CREATE</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:blue;">PROC</span> spGet_ord</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">AS</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">SELECT</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:grey;">*</span> <span style="color:blue;">FROM</span> orders <span style="color:blue;">WHERE</span> Amount<span style="color:grey;">=</span>1001 </span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:green;font-family:&quot;font-size:10pt;">&#8211;adding belowstatement other than that, same code as previous SP</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">OPTION </span><span style="color:grey;font-family:&quot;font-size:10pt;">(</span><span style="color:blue;font-family:&quot;font-size:10pt;">KEEPFIXED</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:blue;">PLAN</span><span style="color:grey;">);</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">GO</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">exec</span><span style="font-family:&quot;font-size:10pt;"> spGet_ord</span></div>
<div class="MsoNormal"><span style="color:blue;font-family:&quot;font-size:10pt;line-height:115%;">GO</span></div>
<div class="MsoNormal"></div>
<div class="MsoNormal">After recreating table along with fresh records, we aregoing to see what happened while executing SP in above statement.</div>
<p>
<div class="separator" style="clear:both;text-align:center;"><a href="http://riteshshah.files.wordpress.com/2011/11/4.jpg" style="margin-left:1em;margin-right:1em;"><img border="0" height="44" src="http://riteshshah.files.wordpress.com/2011/11/4.jpg?w=320&#038;h=44" width="320" /></a></div>
<div class="MsoNormal">Since our table now has only 1 record so it is time tochange statistics with bulk insert as follows again.</div>
<div class="MsoNormal"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:green;font-family:&quot;font-size:10pt;">&#8211;inserting100000 fack rows into table (BTW, thank to Jacob Sebastian, copy in INSERTscript from one of his demo to generate big table)</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">INSERT</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:blue;">INTO</span> orders<span style="color:blue;"> </span><span style="color:grey;">(</span>OrderDate<span style="color:grey;">,</span> Amount<span style="color:grey;">,</span> Refno<span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">SELECT</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:blue;">TOP</span> 50000</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:magenta;">DATEADD</span><span style="color:grey;">(</span><span style="color:blue;">minute</span><span style="color:grey;">,</span> <span style="color:magenta;">ABS</span><span style="color:grey;">(</span>a<span style="color:grey;">.</span><span style="color:magenta;">object_id</span> <span style="color:grey;">%</span> 50000 <span style="color:grey;">),</span> <span style="color:magenta;">CAST</span><span style="color:grey;">(</span><span style="color:red;">&#8217;2010-02-01&#8242;</span> <span style="color:blue;">AS</span> <span style="color:blue;">DATETIME</span><span style="color:grey;">)),</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:magenta;">ABS</span><span style="color:grey;">(</span>a<span style="color:grey;">.</span><span style="color:magenta;">object_id</span> <span style="color:grey;">%</span> 10<span style="color:grey;">),</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span style="color:magenta;">CAST</span><span style="color:grey;">(</span><span style="color:magenta;">ABS</span><span style="color:grey;">(</span>a<span style="color:grey;">.</span><span style="color:magenta;">object_id</span><span style="color:grey;">)</span> <span style="color:blue;">AS</span> <span style="color:blue;">VARCHAR</span><span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">FROM</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:green;">sys</span><span style="color:grey;">.</span><span style="color:green;">all_objects</span> a</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:grey;font-family:&quot;font-size:10pt;">CROSS</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:grey;">JOIN</span> <span style="color:green;">sys</span><span style="color:grey;">.</span><span style="color:green;">all_objects</span> b</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">GO</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">exec</span><span style="font-family:&quot;font-size:10pt;"> spGet_ord</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">GO</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:green;font-family:&quot;font-size:10pt;">&#8211;if you wish,you can uncomment below code and delete SQLHub database</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:green;font-family:&quot;font-size:10pt;">&#8211;use master</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:green;font-family:&quot;font-size:10pt;">&#8211;go</span></div>
<div class="MsoNormal"><span style="color:green;font-family:&quot;font-size:10pt;line-height:115%;">&#8211;drop database sqlhub</span></div>
<div class="MsoNormal"></div>
<div class="MsoNormal">Here is the screen capture of Profiler which doesn’t showRecompilation.</div>
<div class="MsoNormal"></div>
<div class="separator" style="clear:both;text-align:center;"><a href="http://riteshshah.files.wordpress.com/2011/11/51.jpg" style="margin-left:1em;margin-right:1em;"><img border="0" height="37" src="http://riteshshah.files.wordpress.com/2011/11/51.jpg?w=320&#038;h=37" width="320" /></a></div>
<div class="MsoNormal"></div>
<p>&nbsp;
<div class="MsoNormal">You can even add few more thousands of rows and execute SPagain, you won’t get recompilation.&nbsp;</div>
<div class="MsoNormal"></div>
<div class="MsoNormal"><b><span style="color:#333333;">Reference: Ritesh Shah</span></b><b><span style="color:#333333;"><br /></span></b><b>&nbsp;</b></div>
<div class="MsoNormal"><a href="http://www.sqlhub.com/"><b>http://www.sqlhub.com</b></a><b><span style="color:#333333;"><br />Note: Microsoft Books online is a default reference of all articles butexamples and explanations prepared by Ritesh Shah, founder of </span></b><a href="http://www.sqlhub.com/"><b>http://www.SQLHub.com</b></a></div>
<div class="MsoNormal"></div>
<div class="MsoNormal"><b><span style="color:red;">Ask me any SQL Server related question at my “<a href="http://beyondrelational.com/ask/ritesh/default.aspx">ASK Profile</a>”</span></b></div>
<p>
<div class="separator" style="clear:both;text-align:center;"><a href="http://riteshshah.files.wordpress.com/2011/11/4.jpg" style="margin-left:1em;margin-right:1em;"><br /></a></div>
<p>
<div class="separator" style="clear:both;text-align:center;"><a href="http://riteshshah.files.wordpress.com/2011/11/51.jpg" style="margin-left:1em;margin-right:1em;"><br /></a></div>
<p></div>
<div class="blogger-post-footer">Microsoft SQL Server Blog. Fight the fear of SQL with SQLHub.com. Founder is Ritesh Shah<img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/9075786158122089104-1079544875679609570?l=www.sqlhub.com' alt='' /></div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/riteshshah.wordpress.com/834/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/riteshshah.wordpress.com/834/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/riteshshah.wordpress.com/834/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/riteshshah.wordpress.com/834/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/riteshshah.wordpress.com/834/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/riteshshah.wordpress.com/834/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/riteshshah.wordpress.com/834/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/riteshshah.wordpress.com/834/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/riteshshah.wordpress.com/834/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/riteshshah.wordpress.com/834/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/riteshshah.wordpress.com/834/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/riteshshah.wordpress.com/834/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/riteshshah.wordpress.com/834/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/riteshshah.wordpress.com/834/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=riteshshah.wordpress.com&amp;blog=6754420&amp;post=834&amp;subd=riteshshah&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://riteshshah.wordpress.com/2011/11/02/playing-with-option-keepfixed-plan-hint-in-sql-server-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/77d9fe086ee2f3980400752a46f3b66f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Ritesh Shah</media:title>
		</media:content>

		<media:content url="http://riteshshah.files.wordpress.com/2011/11/13.jpg?w=300" medium="image" />

		<media:content url="http://riteshshah.files.wordpress.com/2011/11/3.jpg?w=300" medium="image" />

		<media:content url="http://riteshshah.files.wordpress.com/2011/11/2.jpg?w=300" medium="image" />

		<media:content url="http://riteshshah.files.wordpress.com/2011/11/4.jpg?w=300" medium="image" />

		<media:content url="http://riteshshah.files.wordpress.com/2011/11/51.jpg?w=300" medium="image" />

		<media:content url="https://blogger.googleusercontent.com/tracker/9075786158122089104-1079544875679609570?l=www.sqlhub.com" medium="image" />
	</item>
		<item>
		<title>Playing with OPTION KEEPFIXED PLAN hint in SQL Server</title>
		<link>http://riteshshah.wordpress.com/2011/11/02/playing-with-option-keepfixed-plan-hint-in-sql-server/</link>
		<comments>http://riteshshah.wordpress.com/2011/11/02/playing-with-option-keepfixed-plan-hint-in-sql-server/#comments</comments>
		<pubDate>Wed, 02 Nov 2011 07:26:00 +0000</pubDate>
		<dc:creator>riteshshah</dc:creator>
				<category><![CDATA[hint]]></category>
		<category><![CDATA[KEEPFIXED PLAN]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[MS SQL Server]]></category>
		<category><![CDATA[Ritesh Shah]]></category>
		<category><![CDATA[keepfixed plan]]></category>
		<category><![CDATA[non clustered index]]></category>
		<category><![CDATA[profiler]]></category>

		<guid isPermaLink="false">http://riteshshah.wordpress.com/2011/11/02/playing-with-option-keepfixed-plan-hint-in-sql-server</guid>
		<description><![CDATA[My previous article was on Compilation&#38; Recompilation of Stored Procedure. After reading that article, two ofmy reader sent me an email about more clarification on some terminology and thatis the base of writing this article. Actually SQL Server Query Optimizer is really one of thesmart tools which used to find or generate best execution plan [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=riteshshah.wordpress.com&amp;blog=6754420&amp;post=833&amp;subd=riteshshah&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div style="text-align:left;" dir="ltr">
<div class="MsoNormal">My previous article was on <a href="http://www.sqlhub.com/2011/10/some-facts-about-stored-procedure.html">Compilation&amp; Recompilation of Stored Procedure</a>. After reading that article, two ofmy reader sent me an email about more clarification on some terminology and thatis the base of writing this article.</div>
<div class="MsoNormal"></div>
<div class="MsoNormal">Actually SQL Server Query Optimizer is really one of thesmart tools which used to find or generate best execution plan for query or storedprocedure and make the task easy for DBA. Basically it uses Cost BasedOptimizer (CBO) to do this task, even sometime feels to have specific ExecutionPlan for query so Microsoft have provided many different hints at differentlevel.</div>
<div class="MsoNormal"></div>
<div class="MsoNormal">Query Optimizer (QO) used to save execution plan when itcompiles Stored Procedure first time and use the same execution plan when thesame stored procedure is being used again so that you can get rid of overheadof generating execution plan again and again.</div>
<div class="MsoNormal"></div>
<div class="MsoNormal">When index statistics changes heavily, QO used to recompilethe statement to get best plan for your stored procedure but if you are surethat the existing Plan would works fine than one should go for OPTION KEEPFIXEDPLAN for that statement so that you can save your resources and boost upperformance.</div>
<div class="MsoNormal"></div>
<div class="MsoNormal">I will demonstrate this with few queries run in SQL ServerManagement Studio (SSMS) and capture the results in Profiler.</div>
<div class="MsoNormal"></div>
<div class="MsoNormal">I will select following Events in Profiler while creatingnew Trace for my server.</div>
<div class="MsoNormal"></div>
<div class="MsoNormal"><strong>Stored Procedure</strong></div>
<ul style="margin-top:0;" type="disc">
<li class="MsoNormal">SP:Completed</li>
<li class="MsoNormal">SP:Recompile</li>
<li class="MsoNormal">SP:smtpCompleted</li>
<li class="MsoNormal">SP:smtpStarting</li>
</ul>
<div class="separator" style="clear:both;text-align:center;"></div>
<div class="separator" style="clear:both;text-align:center;"><a style="margin-left:1em;margin-right:1em;" href="http://2.bp.blogspot.com/-qEMK6CxhCLE/TrDoI5Nr6PI/AAAAAAAAAUs/_HgZldd9WiI/s1600/1.jpg"><img src="http://2.bp.blogspot.com/-qEMK6CxhCLE/TrDoI5Nr6PI/AAAAAAAAAUs/_HgZldd9WiI/s320/1.jpg" alt="" width="320" height="200" border="0" /></a></div>
<p>&nbsp;</p>
<div class="MsoNormal">Now execute following TSQL in your SSMS.</div>
<div class="MsoNormal"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:green;font-family:&quot;font-size:10pt;">&#8211;create onedatabase which you can delete after running this example</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">create</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:blue;">database</span> SQLHub</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">GO</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">USE</span><span style="font-family:&quot;font-size:10pt;"> SQLHub</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">GO</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:green;font-family:&quot;font-size:10pt;">&#8211;if orderstable is already there. you can delete it than create new one with name&#8221;Orders&#8221;</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">IF</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:magenta;">OBJECT_ID</span><span style="color:grey;">(</span><span style="color:red;">&#8216;orders&#8217;</span><span style="color:grey;">,</span> <span style="color:red;">&#8216;U&#8217;</span><span style="color:grey;">)</span> <span style="color:grey;">IS</span> <span style="color:grey;">NOT</span> <span style="color:grey;">NULL</span> <span style="color:blue;">BEGIN</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;">      <span style="color:blue;">DROP</span> <span style="color:blue;">TABLE</span> orders</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">END</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">GO</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:green;font-family:&quot;font-size:10pt;">&#8211;creating table</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">CREATE</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:blue;">TABLE</span> orders<span style="color:grey;">(</span>OrderID <span style="color:blue;">INT</span> <span style="color:blue;">IDENTITY</span><span style="color:grey;">,</span> OrderDate <span style="color:blue;">DATETIME</span><span style="color:grey;">,</span> Amount <span style="color:blue;">MONEY</span><span style="color:grey;">,</span> Refno <span style="color:blue;">INT</span><span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">GO</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:green;font-family:&quot;font-size:10pt;">&#8211;creatingnonclustered index</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">CREATE</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:blue;">NONCLUSTERED</span> <span style="color:blue;">INDEX</span>IDX_ORD <span style="color:blue;">ON</span> Orders<span style="color:grey;">(</span>amount<span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">GO</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:green;font-family:&quot;font-size:10pt;">&#8211;inserting only1 record in our table</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">INSERT</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:blue;">INTO</span> orders <span style="color:blue;">VALUES</span><span style="color:grey;">(</span><span style="color:magenta;">GETDATE</span><span style="color:grey;">(),</span>1001<span style="color:grey;">,</span>111<span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">GO</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:green;font-family:&quot;font-size:10pt;">&#8211;creating SPwhich will SELECT all records from Orders table where Amount is 1001</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">CREATE</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:blue;">PROC</span> spGet_ord</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">AS</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">SELECT</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:grey;">*</span> <span style="color:blue;">FROM</span> orders <span style="color:blue;">WHERE</span> Amount<span style="color:grey;">=</span>1001</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">GO</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:green;font-family:&quot;font-size:10pt;">&#8211;execute SP</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">exec</span><span style="font-family:&quot;font-size:10pt;"> spGet_ord</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">GO</span></div>
<div class="MsoNormal"></div>
<div class="MsoNormal">If you will see in profiler than you would get followingevent captured</div>
<div class="separator" style="clear:both;text-align:center;"><a style="margin-left:1em;margin-right:1em;" href="http://riteshshah.files.wordpress.com/2011/11/3.jpg"><br />
</a></div>
<div class="separator" style="clear:both;text-align:center;"><a style="margin-left:1em;margin-right:1em;" href="http://riteshshah.files.wordpress.com/2011/11/2.jpg"><img src="http://riteshshah.files.wordpress.com/2011/11/3.jpg?w=320&#038;h=49" alt="" width="320" height="49" border="0" /></a></div>
<div class="separator" style="clear:both;text-align:center;"><a style="margin-left:1em;margin-right:1em;" href="http://riteshshah.files.wordpress.com/2011/11/2.jpg"><br />
</a></div>
<div class="MsoNormal"></div>
<div class="MsoNormal">Now, make change in Index statistics by inserting so manyrecords in table so that we can test the effect in profiler after executing SPagain.</div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:green;font-family:&quot;font-size:10pt;">&#8211;inserting50000 fack rows into table</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">INSERT</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:blue;">INTO</span> orders<span style="color:grey;">(</span>OrderDate<span style="color:grey;">,</span> Amount<span style="color:grey;">,</span> Refno<span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">SELECT</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:blue;">TOP</span> 50000</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;">      <span style="color:magenta;">DATEADD</span><span style="color:grey;">(</span><span style="color:blue;">minute</span><span style="color:grey;">,</span> <span style="color:magenta;">ABS</span><span style="color:grey;">(</span>a<span style="color:grey;">.</span><span style="color:magenta;">object_id</span> <span style="color:grey;">%</span> 50000 <span style="color:grey;">),</span> <span style="color:magenta;">CAST</span><span style="color:grey;">(</span><span style="color:red;">&#8217;2010-02-01&#8242;</span> <span style="color:blue;">AS</span> <span style="color:blue;">DATETIME</span><span style="color:grey;">)),</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;">      <span style="color:magenta;">ABS</span><span style="color:grey;">(</span>a<span style="color:grey;">.</span><span style="color:magenta;">object_id</span> <span style="color:grey;">%</span> 10<span style="color:grey;">),</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;">      <span style="color:magenta;">CAST</span><span style="color:grey;">(</span><span style="color:magenta;">ABS</span><span style="color:grey;">(</span>a<span style="color:grey;">.</span><span style="color:magenta;">object_id</span><span style="color:grey;">)</span> <span style="color:blue;">AS</span> <span style="color:blue;">VARCHAR</span><span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">FROM</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:green;">sys</span><span style="color:grey;">.</span><span style="color:green;">all_objects</span> a</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:grey;font-family:&quot;font-size:10pt;">CROSS</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:grey;">JOIN</span> <span style="color:green;">sys</span><span style="color:grey;">.</span><span style="color:green;">all_objects</span> b</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">GO</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:green;font-family:&quot;font-size:10pt;">&#8211;execute SPagain</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">exec</span><span style="font-family:&quot;font-size:10pt;"> spGet_ord</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">GO</span></div>
<div class="separator" style="clear:both;text-align:center;"><a style="margin-left:1em;margin-right:1em;" href="http://riteshshah.files.wordpress.com/2011/11/3.jpg"><img src="http://riteshshah.files.wordpress.com/2011/11/2.jpg?w=320&#038;h=63" alt="" width="320" height="63" border="0" /></a></div>
<div class="MsoNormal">Now, if you see the profiler, you will see “SP:Recompile”event as your statistics are updated so it need recompilation of statement. If youagain few more thousands record in same table and execute the same SP again,you will see recompilation again.</div>
<div class="MsoNormal"></div>
<div class="MsoNormal">Now, after droping table and SP, we will make one smallchange in Stored Procedure and will use “KEEPFIXED PLAN” hint so that we canavoid recompilation.</div>
<div class="MsoNormal"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">drop</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:blue;">proc</span> spGet_ord</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">GO</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">drop</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:blue;">table</span> orders</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">go</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:green;font-family:&quot;font-size:10pt;">&#8211;creating table</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">CREATE</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:blue;">TABLE</span> orders<span style="color:grey;">(</span>OrderID <span style="color:blue;">INT</span> <span style="color:blue;">IDENTITY</span><span style="color:grey;">,</span> OrderDate <span style="color:blue;">DATETIME</span><span style="color:grey;">,</span> Amount <span style="color:blue;">MONEY</span><span style="color:grey;">,</span> Refno <span style="color:blue;">INT</span><span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">GO</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">CREATE</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:blue;">NONCLUSTERED</span> <span style="color:blue;">INDEX</span>IDX_ORD <span style="color:blue;">ON</span> Orders<span style="color:grey;">(</span>amount<span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">GO</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">INSERT</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:blue;">INTO</span> orders <span style="color:blue;">VALUES</span><span style="color:grey;">(</span><span style="color:magenta;">GETDATE</span><span style="color:grey;">(),</span>1001<span style="color:grey;">,</span>111<span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">GO</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">CREATE</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:blue;">PROC</span> spGet_ord</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">AS</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">SELECT</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:grey;">*</span> <span style="color:blue;">FROM</span> orders <span style="color:blue;">WHERE</span> Amount<span style="color:grey;">=</span>1001 </span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:green;font-family:&quot;font-size:10pt;">&#8211;adding belowstatement other than that, same code as previous SP</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">OPTION </span><span style="color:grey;font-family:&quot;font-size:10pt;">(</span><span style="color:blue;font-family:&quot;font-size:10pt;">KEEPFIXED</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:blue;">PLAN</span><span style="color:grey;">);</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">GO</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">exec</span><span style="font-family:&quot;font-size:10pt;"> spGet_ord</span></div>
<div class="MsoNormal"><span style="color:blue;font-family:&quot;font-size:10pt;line-height:115%;">GO</span></div>
<div class="MsoNormal"></div>
<div class="MsoNormal">After recreating table along with fresh records, we aregoing to see what happened while executing SP in above statement.</div>
<div class="separator" style="clear:both;text-align:center;"><a style="margin-left:1em;margin-right:1em;" href="http://riteshshah.files.wordpress.com/2011/11/4.jpg"><img src="http://riteshshah.files.wordpress.com/2011/11/4.jpg?w=320&#038;h=44" alt="" width="320" height="44" border="0" /></a></div>
<div class="MsoNormal">Since our table now has only 1 record so it is time tochange statistics with bulk insert as follows again.</div>
<div class="MsoNormal"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:green;font-family:&quot;font-size:10pt;">&#8211;inserting100000 fack rows into table (BTW, thank to Jacob Sebastian, copy in INSERTscript from one of his demo to generate big table)</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">INSERT</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:blue;">INTO</span> orders<span style="color:grey;">(</span>OrderDate<span style="color:grey;">,</span> Amount<span style="color:grey;">,</span> Refno<span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">SELECT</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:blue;">TOP</span> 50000</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;">      <span style="color:magenta;">DATEADD</span><span style="color:grey;">(</span><span style="color:blue;">minute</span><span style="color:grey;">,</span> <span style="color:magenta;">ABS</span><span style="color:grey;">(</span>a<span style="color:grey;">.</span><span style="color:magenta;">object_id</span> <span style="color:grey;">%</span> 50000 <span style="color:grey;">),</span> <span style="color:magenta;">CAST</span><span style="color:grey;">(</span><span style="color:red;">&#8217;2010-02-01&#8242;</span> <span style="color:blue;">AS</span> <span style="color:blue;">DATETIME</span><span style="color:grey;">)),</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;">      <span style="color:magenta;">ABS</span><span style="color:grey;">(</span>a<span style="color:grey;">.</span><span style="color:magenta;">object_id</span> <span style="color:grey;">%</span> 10<span style="color:grey;">),</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;">      <span style="color:magenta;">CAST</span><span style="color:grey;">(</span><span style="color:magenta;">ABS</span><span style="color:grey;">(</span>a<span style="color:grey;">.</span><span style="color:magenta;">object_id</span><span style="color:grey;">)</span> <span style="color:blue;">AS</span> <span style="color:blue;">VARCHAR</span><span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">FROM</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:green;">sys</span><span style="color:grey;">.</span><span style="color:green;">all_objects</span> a</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:grey;font-family:&quot;font-size:10pt;">CROSS</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:grey;">JOIN</span> <span style="color:green;">sys</span><span style="color:grey;">.</span><span style="color:green;">all_objects</span> b</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">GO</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">exec</span><span style="font-family:&quot;font-size:10pt;"> spGet_ord</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">GO</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:green;font-family:&quot;font-size:10pt;">&#8211;if you wish,you can uncomment below code and delete SQLHub database</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:green;font-family:&quot;font-size:10pt;">&#8211;use master</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:green;font-family:&quot;font-size:10pt;">&#8211;go</span></div>
<div class="MsoNormal"><span style="color:green;font-family:&quot;font-size:10pt;line-height:115%;">&#8211;drop database sqlhub</span></div>
<div class="MsoNormal"></div>
<div class="MsoNormal">Here is the screen capture of Profiler which doesn’t showRecompilation.</div>
<div class="MsoNormal"></div>
<div class="separator" style="clear:both;text-align:center;"><a style="margin-left:1em;margin-right:1em;" href="http://riteshshah.files.wordpress.com/2011/11/5.jpg"><img src="http://riteshshah.files.wordpress.com/2011/11/5.jpg?w=320&#038;h=37" alt="" width="320" height="37" border="0" /></a></div>
<div class="MsoNormal"></div>
<p>&nbsp;</p>
<div class="MsoNormal">You can even add few more thousands of rows and execute SPagain, you won’t get recompilation.</div>
<div class="MsoNormal"></div>
<div class="MsoNormal"><strong><span style="color:#333333;">Reference: Ritesh Shah</span></strong><strong><span style="color:#333333;"><br />
</span></strong><strong> </strong></div>
<div class="MsoNormal"><a href="http://www.sqlhub.com/"><strong>http://www.sqlhub.com</strong></a><strong><span style="color:#333333;"><br />
Note: Microsoft Books online is a default reference of all articles butexamples and explanations prepared by Ritesh Shah, founder of </span></strong><a href="http://www.sqlhub.com/"><strong>http://www.SQLHub.com</strong></a></div>
<div class="MsoNormal"></div>
<div class="MsoNormal"><strong><span style="color:red;">Ask me any SQL Server related question at my “<a href="http://beyondrelational.com/ask/ritesh/default.aspx">ASK Profile</a>”</span></strong></div>
<div class="separator" style="clear:both;text-align:center;"><a style="margin-left:1em;margin-right:1em;" href="http://riteshshah.files.wordpress.com/2011/11/4.jpg"><br />
</a></div>
<div class="separator" style="clear:both;text-align:center;"><a style="margin-left:1em;margin-right:1em;" href="http://riteshshah.files.wordpress.com/2011/11/5.jpg"><br />
</a></div>
</div>
<div class="blogger-post-footer">Microsoft SQL Server Blog. Fight the fear of SQL with SQLHub.com. Founder is Ritesh Shah<img src="https://blogger.googleusercontent.com/tracker/9075786158122089104-1079544875679609570?l=www.sqlhub.com" alt="" width="1" height="1" /></div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/riteshshah.wordpress.com/833/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/riteshshah.wordpress.com/833/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/riteshshah.wordpress.com/833/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/riteshshah.wordpress.com/833/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/riteshshah.wordpress.com/833/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/riteshshah.wordpress.com/833/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/riteshshah.wordpress.com/833/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/riteshshah.wordpress.com/833/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/riteshshah.wordpress.com/833/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/riteshshah.wordpress.com/833/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/riteshshah.wordpress.com/833/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/riteshshah.wordpress.com/833/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/riteshshah.wordpress.com/833/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/riteshshah.wordpress.com/833/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=riteshshah.wordpress.com&amp;blog=6754420&amp;post=833&amp;subd=riteshshah&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://riteshshah.wordpress.com/2011/11/02/playing-with-option-keepfixed-plan-hint-in-sql-server/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/77d9fe086ee2f3980400752a46f3b66f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Ritesh Shah</media:title>
		</media:content>

		<media:content url="http://2.bp.blogspot.com/-qEMK6CxhCLE/TrDoI5Nr6PI/AAAAAAAAAUs/_HgZldd9WiI/s320/1.jpg" medium="image" />

		<media:content url="http://riteshshah.files.wordpress.com/2011/11/3.jpg?w=300" medium="image" />

		<media:content url="http://riteshshah.files.wordpress.com/2011/11/2.jpg?w=300" medium="image" />

		<media:content url="http://riteshshah.files.wordpress.com/2011/11/4.jpg?w=300" medium="image" />

		<media:content url="http://riteshshah.files.wordpress.com/2011/11/5.jpg?w=300" medium="image" />

		<media:content url="https://blogger.googleusercontent.com/tracker/9075786158122089104-1079544875679609570?l=www.sqlhub.com" medium="image" />
	</item>
		<item>
		<title>Some facts about Stored Procedure Compilation &amp; Recompilation</title>
		<link>http://riteshshah.wordpress.com/2011/10/31/some-facts-about-stored-procedure-compilation-recompilation/</link>
		<comments>http://riteshshah.wordpress.com/2011/10/31/some-facts-about-stored-procedure-compilation-recompilation/#comments</comments>
		<pubDate>Mon, 31 Oct 2011 04:23:00 +0000</pubDate>
		<dc:creator>riteshshah</dc:creator>
				<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[MS SQL Server]]></category>
		<category><![CDATA[Ritesh Shah]]></category>
		<category><![CDATA[stored procedure]]></category>

		<guid isPermaLink="false">http://riteshshah.wordpress.com/2011/10/31/some-facts-about-stored-procedure-compilation-recompilation</guid>
		<description><![CDATA[I generally ask one question in interview “If we have onecomplex query which JOIN five table, what would work best from .NETapplication, Call of Query or Stored Procedure?” 80% of candidate used to say “SPworks fast”. My next question always would be “WHY?” than out of those 80%, 60%would say “Because SP is a compiled [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=riteshshah.wordpress.com&amp;blog=6754420&amp;post=832&amp;subd=riteshshah&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div dir="ltr" style="text-align:left;">
<div class="MsoNormal">I generally ask one question in interview “If we have onecomplex query which JOIN five table, what would work best from .NETapplication, Call of Query or Stored Procedure?” 80% of candidate used to say “SPworks fast”. My next question always would be “WHY?” than out of those 80%, 60%would say “Because SP is a compiled code”. After that I fire my follow upquestion, I can see option of “Compile page or project in .NET but I never seethat kind of option in SSMS, how do you compile your SP?”, once I EXECUTE thisquestion, 90% candidate of those 60% candidate would like to keep silence ordivert the talk.</div>
<div class="MsoNormal"></div>
<div class="MsoNormal">Anyway, intention of this article is to let you know somefacts about SP compilation &amp; Recompilation. Since it is big topic andwouldn’t cover under on article, I may come up with some more articles on thistopic but right now, let me explain some basic facts only.</div>
<div class="MsoNormal"></div>
<div class="MsoNormal">First of all, let me tell you that you don’t need to compileStored Procedure manually, when you execute it, SQL Server will compile yourStored Procedure for you and save the execution plan for future use so that itdoesn’t need to compile again and again, this is generic understanding, itdoesn’t work all time as few facts are there which cause recompilation manytime or every time. If you want to recompile your Stored Procedure manually,you should use “SP_Recompile” Stored Procedure given by SQL Server.</div>
<div class="MsoNormal"></div>
<div class="MsoNormal">Now, you think if recompilation is that bad than whyMicrosoft has given facility to recompile? Well, let me have an opportunity tobreak the ice, recompilation of stored procedure is not always bad. It may bebeneficial or may be harmful, it is totally depends on the situation.</div>
<div class="MsoNormal"></div>
<div class="MsoNormal">Actually compilation of Stored Procedure stores theexecution plan first time you execute your Stored Procedure and every follow upcall would use the same execution plan but recompilation of SP would be helpfulif you have new statistics or new index on the table. BTW, in SQL Server 2008+there is in-built functionality to recompile at statement level rather thanrecompiling whole stored procedure which is less resource centric.&nbsp;</div>
<div class="MsoNormal"></div>
<div class="MsoNormal">Following is the list of basic cause which forces StoredProcedure to recompile.</div>
<div class="MsoListParagraphCxSpFirst" style="text-indent:-.25in;"><span style="font-family:Symbol;">·<span style="font:7pt &quot;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></span>&nbsp;</div>
<ul style="text-align:left;">
<li>Change in SET option within Stored Procedure</li>
<li><span style="font-family:Symbol;"><span style="font:7pt &quot;"></span></span>Execution plan is very old</li>
<li><span style="font-family:Symbol;"><span style="font:7pt &quot;"></span></span>Schema change in table, index, view or temptables which are used in Stored Procedure</li>
<li><span style="font-family:Symbol;"><span style="font:7pt &quot;">&nbsp;</span></span>“Deferred object resolution”, means object wasnot available while compiling Stored Procedure&nbsp; but you have created later on,may be some temp table you have created in Stored Procedure.</li>
<li><span style="font-family:Symbol;"><span style="font:7pt &quot;">&nbsp;</span></span>Call of “SP_Recompile” Stored Procedure.</li>
<li><span style="font-family:Symbol;"><span style="font:7pt &quot;"></span></span>Call of RECOMPILE clause in Stored Procedure.</li>
<li><span style="font-family:Symbol;"><span style="font:7pt &quot;"></span></span>Statistics are old</li>
</ul>
<div class="MsoNormal"></div>
<div class="MsoNormal">How to avoid Stored Procedure recompilations?</div>
<ul style="text-align:left;">
<li>Avoid using temp table or other DDL statements as long aspossible.</li>
<li>Use table variable in Stored Procedure if needed</li>
<li>Avoid changing SET option in Stored Procedure likeARITHABORT, Quoted_Identifier, ANSI_NULLS, ANSI_WARNINGS etc.</li>
<li>Avoiding recompilation by statistics change by using “<a href="http://www.sqlhub.com/2011/11/playing-with-option-keepfixed-plan-hint.html" target="_blank">KEEPFIXEDPLAN</a>” hint.</li>
<li>Disable Auto Update statistics for your database.</li>
</ul>
<div class="MsoNormal"></div>
<div class="MsoNormal">Well, these are very basic understanding and each point ofthis article may consume separate dedicated article and I may come up withseries on this very soon.</div>
<div class="MsoNormal"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><b><span style="color:#333333;font-family:&quot;font-size:12pt;">Reference: Ritesh Shah</span></b><span style="font-family:&quot;font-size:12pt;"></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:12pt;"><a href="http://www.sqlhub.com/"><b><span style="color:blue;">http://www.sqlhub.com</span></b></a><b><span style="color:#333333;"><br />Note: Microsoft Books online is a default reference of all articles butexamples and explanations prepared by Ritesh Shah, founder of </span></b><a href="http://www.sqlhub.com/"><b><span style="color:blue;">http://www.SQLHub.com</span></b></a></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><b><span style="color:red;font-family:&quot;font-size:12pt;">Ask me any SQL Server related question at my “<a href="http://beyondrelational.com/ask/ritesh/default.aspx"><span style="color:blue;">ASK Profile</span></a>”</span></b><span style="font-family:&quot;font-size:12pt;"></span></div>
<div class="MsoNormal"></div>
</div>
<div class="blogger-post-footer">Microsoft SQL Server Blog. Fight the fear of SQL with SQLHub.com. Founder is Ritesh Shah<img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/9075786158122089104-5388993642219666899?l=www.sqlhub.com' alt='' /></div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/riteshshah.wordpress.com/832/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/riteshshah.wordpress.com/832/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/riteshshah.wordpress.com/832/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/riteshshah.wordpress.com/832/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/riteshshah.wordpress.com/832/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/riteshshah.wordpress.com/832/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/riteshshah.wordpress.com/832/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/riteshshah.wordpress.com/832/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/riteshshah.wordpress.com/832/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/riteshshah.wordpress.com/832/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/riteshshah.wordpress.com/832/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/riteshshah.wordpress.com/832/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/riteshshah.wordpress.com/832/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/riteshshah.wordpress.com/832/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=riteshshah.wordpress.com&amp;blog=6754420&amp;post=832&amp;subd=riteshshah&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://riteshshah.wordpress.com/2011/10/31/some-facts-about-stored-procedure-compilation-recompilation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/77d9fe086ee2f3980400752a46f3b66f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Ritesh Shah</media:title>
		</media:content>

		<media:content url="https://blogger.googleusercontent.com/tracker/9075786158122089104-5388993642219666899?l=www.sqlhub.com" medium="image" />
	</item>
		<item>
		<title>Last chance to win free Apple iPad this year</title>
		<link>http://riteshshah.wordpress.com/2011/09/12/last-chance-to-win-free-apple-ipad-this-year/</link>
		<comments>http://riteshshah.wordpress.com/2011/09/12/last-chance-to-win-free-apple-ipad-this-year/#comments</comments>
		<pubDate>Mon, 12 Sep 2011 07:36:00 +0000</pubDate>
		<dc:creator>riteshshah</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://riteshshah.wordpress.com/2011/09/12/last-chance-to-win-free-apple-ipad-this-year</guid>
		<description><![CDATA[SQL Server MVP/Auther/Trainer Jacob Sebastian haslaunched another quiz “SQLServer DBA Quiz 2011” which is the last quiz of this year. So don’t missthis chance and answer the quiz question asked by Quiz master. My question is already published regarding DeadLock, Live Lock and Blocking. If you have not participated yet, still, youhave chance to win [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=riteshshah.wordpress.com&amp;blog=6754420&amp;post=830&amp;subd=riteshshah&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div dir="ltr" style="text-align:left;"><!--[if gte mso 9]&gt;   Normal  0          false  false  false    EN-US  X-NONE  X-NONE                                       MicrosoftInternetExplorer4                                     &lt;![endif]--><!--[if gte mso 9]&gt;                                                                                                                                                                                                                                                                                    &lt;![endif]--><!--[if gte mso 10]&gt; /* Style Definitions */ table.MsoNormalTable	{mso-style-name:"Table Normal";	mso-tstyle-rowband-size:0;	mso-tstyle-colband-size:0;	mso-style-noshow:yes;	mso-style-priority:99;	mso-style-qformat:yes;	mso-style-parent:"";	mso-padding-alt:0in 5.4pt 0in 5.4pt;	mso-para-margin:0in;	mso-para-margin-bottom:.0001pt;	mso-pagination:widow-orphan;	font-size:10.0pt;	font-family:"Calibri","sans-serif";}&lt;![endif]-->
<div class="MsoNormal">SQL Server MVP/Auther/Trainer <a href="http://beyondrelational.com/blogs/jacob/">Jacob Sebastian</a> haslaunched another quiz “<a href="http://beyondrelational.com/quiz/sqlserver/dba/2011/default.aspx">SQLServer DBA Quiz 2011</a>” which is the last quiz of this year. So don’t missthis chance and answer the quiz question asked by Quiz master.</div>
<div class="MsoNormal"></div>
<div class="MsoNormal">My question is already published regarding <a href="http://beyondrelational.com/quiz/sqlserver/dba/2011/questions/167/dead-lock-live-lock-blocking.aspx">DeadLock, Live Lock and Blocking</a>. If you have not participated yet, still, youhave chance to win as all questions are still open.&nbsp;</div>
<div class="MsoNormal"></div>
<div class="MsoNormal">If you are not seasoned DBA, still you can refer allquestions and answer given by expert to enhance your knowledge.</div>
<div class="MsoNormal"></div>
<div class="MsoNormal">If you are experienced DBA and you know the answer, you cananswer the question, share you knowledge with community and have chance to winiPad. So what are you waiting for? Try to answer as more question as possible.</div>
<div class="MsoNormal"></div>
<div class="MsoNormal"><b><span style="color:#333333;">Reference: Ritesh Shah</span></b></div>
<div class="MsoNormal"></div>
<div class="MsoNormal"><a href="http://www.sqlhub.com/"><b>http://www.sqlhub.com</b></a><b><span style="color:#333333;"><br />Note: Microsoft Books online is a default reference of all articles butexamples and explanations prepared by Ritesh Shah, founder of </span></b><a href="http://www.sqlhub.com/"><b>http://www.SQLHub.com</b></a></div>
<div class="MsoNormal"><b><span style="color:red;">Ask me any SQL Server related question at my “<a href="http://beyondrelational.com/ask/ritesh/default.aspx">ASK Profile</a>”</span></b></div>
</div>
<div class="blogger-post-footer">Microsoft SQL Server Blog. Fight the fear of SQL with SQLHub.com. Founder is Ritesh Shah<img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/9075786158122089104-7717429444877149763?l=www.sqlhub.com' alt='' /></div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/riteshshah.wordpress.com/830/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/riteshshah.wordpress.com/830/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/riteshshah.wordpress.com/830/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/riteshshah.wordpress.com/830/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/riteshshah.wordpress.com/830/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/riteshshah.wordpress.com/830/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/riteshshah.wordpress.com/830/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/riteshshah.wordpress.com/830/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/riteshshah.wordpress.com/830/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/riteshshah.wordpress.com/830/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/riteshshah.wordpress.com/830/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/riteshshah.wordpress.com/830/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/riteshshah.wordpress.com/830/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/riteshshah.wordpress.com/830/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=riteshshah.wordpress.com&amp;blog=6754420&amp;post=830&amp;subd=riteshshah&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://riteshshah.wordpress.com/2011/09/12/last-chance-to-win-free-apple-ipad-this-year/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/77d9fe086ee2f3980400752a46f3b66f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Ritesh Shah</media:title>
		</media:content>

		<media:content url="https://blogger.googleusercontent.com/tracker/9075786158122089104-7717429444877149763?l=www.sqlhub.com" medium="image" />
	</item>
		<item>
		<title>Move login with password from one SQL Server instance to another</title>
		<link>http://riteshshah.wordpress.com/2011/08/05/move-login-with-password-from-one-sql-server-instance-to-another-2/</link>
		<comments>http://riteshshah.wordpress.com/2011/08/05/move-login-with-password-from-one-sql-server-instance-to-another-2/#comments</comments>
		<pubDate>Fri, 05 Aug 2011 08:03:00 +0000</pubDate>
		<dc:creator>riteshshah</dc:creator>
				<category><![CDATA[login]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[MS SQL Server]]></category>
		<category><![CDATA[Ritesh Shah]]></category>
		<category><![CDATA[user]]></category>

		<guid isPermaLink="false">http://riteshshah.wordpress.com/2011/08/05/move-login-with-password-from-one-sql-server-instance-to-another-2</guid>
		<description><![CDATA[I have already written a small script to fix orphanusers while moving database from one server to anotherserver but the major problem is, that script just auto fix the login but can’thave login with the same password which old instance/server was having. Microsoft has provided one small script to help you outin this situation. Look [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=riteshshah.wordpress.com&amp;blog=6754420&amp;post=831&amp;subd=riteshshah&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div dir="ltr" style="text-align:left;"><!--[if gte mso 9]&gt;   Normal  0          false  false  false    EN-US  X-NONE  X-NONE                                       MicrosoftInternetExplorer4                                     &lt;![endif]--><!--[if gte mso 9]&gt;                                                                                                                                                                                                                                                                                    &lt;![endif]--><!--[if gte mso 10]&gt; /* Style Definitions */ table.MsoNormalTable	{mso-style-name:"Table Normal";	mso-tstyle-rowband-size:0;	mso-tstyle-colband-size:0;	mso-style-noshow:yes;	mso-style-priority:99;	mso-style-qformat:yes;	mso-style-parent:"";	mso-padding-alt:0in 5.4pt 0in 5.4pt;	mso-para-margin:0in;	mso-para-margin-bottom:.0001pt;	mso-pagination:widow-orphan;	font-size:10.0pt;	font-family:"Calibri","sans-serif";}&lt;![endif]-->
<div class="MsoNormal">I have already written a small script to fix <b><span style="font-size:12pt;line-height:115%;"><a href="http://www.sqlhub.com/2009/04/fix-user-and-login-synchronization.html">orphanusers</a></span></b><span style="font-size:12pt;line-height:115%;"> </span>while moving database from one server to anotherserver but the major problem is, that script just auto fix the login but can’thave login with the same password which old instance/server was having.</div>
<div class="MsoNormal"></div>
<div class="MsoNormal">Microsoft has provided one <a href="http://support.microsoft.com/kb/918992">small script</a> to help you outin this situation. Look at the below script which will generate T-SQL for allusers available in current instance with password so that you can run that sameLogin script in another server/instance of SQL Server and have samelogin/password.</div>
<div class="MsoNormal"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">USE</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:blue;">master</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">GO</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">IF</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:magenta;">OBJECT_ID</span><span style="color:blue;"> </span><span style="color:grey;">(</span><span style="color:red;">&#8216;sp_hexadecimal&#8217;</span><span style="color:grey;">)</span> <span style="color:grey;">IS</span> <span style="color:grey;">NOT</span> <span style="color:grey;">NULL</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp; </span><span style="color:blue;">DROP</span> <span style="color:blue;">PROCEDURE</span> sp_hexadecimal</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">GO</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">CREATE</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:blue;">PROCEDURE</span> sp_hexadecimal</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp; </span>@binvalue <span style="color:blue;">varbinary</span><span style="color:grey;">(</span>256<span style="color:grey;">),</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp; </span>@hexvalue <span style="color:blue;">varchar </span><span style="color:grey;">(</span>514<span style="color:grey;">)</span> <span style="color:blue;">OUTPUT</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">AS</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">DECLARE</span><span style="font-family:&quot;font-size:10pt;"> @charvalue<span style="color:blue;">varchar </span><span style="color:grey;">(</span>514<span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">DECLARE</span><span style="font-family:&quot;font-size:10pt;"> @i <span style="color:blue;">int</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">DECLARE</span><span style="font-family:&quot;font-size:10pt;"> @length <span style="color:blue;">int</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">DECLARE</span><span style="font-family:&quot;font-size:10pt;"> @hexstring<span style="color:blue;">char</span><span style="color:grey;">(</span>16<span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">SELECT</span><span style="font-family:&quot;font-size:10pt;"> @charvalue<span style="color:grey;">=</span> <span style="color:red;">&#8217;0x&#8217;</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">SELECT</span><span style="font-family:&quot;font-size:10pt;"> @i <span style="color:grey;">=</span> 1</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">SELECT</span><span style="font-family:&quot;font-size:10pt;"> @length <span style="color:grey;">=</span> <span style="color:magenta;">DATALENGTH</span><span style="color:blue;"> </span><span style="color:grey;">(</span>@binvalue<span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">SELECT</span><span style="font-family:&quot;font-size:10pt;"> @hexstring<span style="color:grey;">=</span> <span style="color:red;">&#8217;0123456789ABCDEF&#8217;</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">WHILE </span><span style="color:grey;font-family:&quot;font-size:10pt;">(</span><span style="font-family:&quot;font-size:10pt;">@i <span style="color:grey;">&lt;=</span> @length<span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">BEGIN</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp; </span><span style="color:blue;">DECLARE</span>@tempint <span style="color:blue;">int</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp; </span><span style="color:blue;">DECLARE</span>@firstint <span style="color:blue;">int</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp; </span><span style="color:blue;">DECLARE</span>@secondint <span style="color:blue;">int</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp; </span><span style="color:blue;">SELECT</span> @tempint<span style="color:grey;">=</span> <span style="color:magenta;">CONVERT</span><span style="color:grey;">(</span><span style="color:blue;">int</span><span style="color:grey;">,</span> <span style="color:magenta;">SUBSTRING</span><span style="color:grey;">(</span>@binvalue<span style="color:grey;">,</span>@i<span style="color:grey;">,</span>1<span style="color:grey;">))</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp; </span><span style="color:blue;">SELECT</span>@firstint <span style="color:grey;">=</span> <span style="color:magenta;">FLOOR</span><span style="color:grey;">(</span>@tempint<span style="color:grey;">/</span>16<span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp; </span><span style="color:blue;">SELECT</span>@secondint <span style="color:grey;">=</span> @tempint <span style="color:grey;">-</span><span style="color:blue;"> </span><span style="color:grey;">(</span>@firstint<span style="color:grey;">*</span>16<span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp; </span><span style="color:blue;">SELECT</span>@charvalue <span style="color:grey;">=</span> @charvalue <span style="color:grey;">+</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp; </span><span style="color:magenta;">SUBSTRING</span><span style="color:grey;">(</span>@hexstring<span style="color:grey;">,</span>@firstint<span style="color:grey;">+</span>1<span style="color:grey;">,</span> 1<span style="color:grey;">)</span> <span style="color:grey;">+</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp; </span><span style="color:magenta;">SUBSTRING</span><span style="color:grey;">(</span>@hexstring<span style="color:grey;">,</span>@secondint<span style="color:grey;">+</span>1<span style="color:grey;">,</span> 1<span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp; </span><span style="color:blue;">SELECT</span> @i <span style="color:grey;">=</span> @i <span style="color:grey;">+</span> 1</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">END</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">SELECT</span><span style="font-family:&quot;font-size:10pt;"> @hexvalue <span style="color:grey;">=</span> @charvalue</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">GO</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">IF</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:magenta;">OBJECT_ID</span><span style="color:blue;"> </span><span style="color:grey;">(</span><span style="color:red;">&#8216;sp_help_revlogin&#8217;</span><span style="color:grey;">)</span> <span style="color:grey;">IS</span> <span style="color:grey;">NOT</span> <span style="color:grey;">NULL</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp; </span><span style="color:blue;">DROP</span> <span style="color:blue;">PROCEDURE</span> sp_help_revlogin</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">GO</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">CREATE</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:blue;">PROCEDURE</span> sp_help_revlogin @login_name <span style="color:blue;">sysname</span> <span style="color:grey;">=</span> <span style="color:grey;">NULL</span> <span style="color:blue;">AS</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">DECLARE</span><span style="font-family:&quot;font-size:10pt;"> @name <span style="color:blue;">sysname</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">DECLARE</span><span style="font-family:&quot;font-size:10pt;"> @type <span style="color:blue;">varchar </span><span style="color:grey;">(</span>1<span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">DECLARE</span><span style="font-family:&quot;font-size:10pt;"> @hasaccess<span style="color:blue;">int</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">DECLARE</span><span style="font-family:&quot;font-size:10pt;"> @denylogin<span style="color:blue;">int</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">DECLARE</span><span style="font-family:&quot;font-size:10pt;">@is_disabled <span style="color:blue;">int</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">DECLARE</span><span style="font-family:&quot;font-size:10pt;">@PWD_varbinary<span>&nbsp; </span><span style="color:blue;">varbinary</span><span style="color:grey;">(</span>256<span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">DECLARE</span><span style="font-family:&quot;font-size:10pt;">@PWD_string<span>&nbsp; </span><span style="color:blue;">varchar</span><span style="color:grey;">(</span>514<span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">DECLARE</span><span style="font-family:&quot;font-size:10pt;">@SID_varbinary <span style="color:blue;">varbinary </span><span style="color:grey;">(</span>85<span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">DECLARE</span><span style="font-family:&quot;font-size:10pt;">@SID_string <span style="color:blue;">varchar </span><span style="color:grey;">(</span>514<span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">DECLARE</span><span style="font-family:&quot;font-size:10pt;">@tmpstr<span>&nbsp; </span><span style="color:blue;">varchar</span><span style="color:grey;">(</span>1024<span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">DECLARE</span><span style="font-family:&quot;font-size:10pt;">@is_policy_checked <span style="color:blue;">varchar </span><span style="color:grey;">(</span>3<span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">DECLARE</span><span style="font-family:&quot;font-size:10pt;">@is_expiration_checked <span style="color:blue;">varchar </span><span style="color:grey;">(</span>3<span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">DECLARE</span><span style="font-family:&quot;font-size:10pt;"> @defaultdb<span style="color:blue;">sysname</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">IF </span><span style="color:grey;font-family:&quot;font-size:10pt;">(</span><span style="font-family:&quot;font-size:10pt;">@login_name<span style="color:grey;">IS</span> <span style="color:grey;">NULL)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp; </span><span style="color:blue;">DECLARE</span>login_curs <span style="color:blue;">CURSOR</span> <span style="color:blue;">FOR</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">SELECT</span> p<span style="color:grey;">.</span><span style="color:blue;">sid</span><span style="color:grey;">,</span> p<span style="color:grey;">.</span>name<span style="color:grey;">,</span> p<span style="color:grey;">.</span><span style="color:blue;">type</span><span style="color:grey;">,</span> p<span style="color:grey;">.</span>is_disabled<span style="color:grey;">,</span> p<span style="color:grey;">.</span>default_database_name<span style="color:grey;">,</span>l<span style="color:grey;">.</span>hasaccess<span style="color:grey;">,</span> l<span style="color:grey;">.</span>denylogin <span style="color:blue;">FROM</span> </span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:green;font-family:&quot;font-size:10pt;">sys</span><span style="color:grey;font-family:&quot;font-size:10pt;">.</span><span style="color:green;font-family:&quot;font-size:10pt;">server_principals</span><span style="font-family:&quot;font-size:10pt;"> p <span style="color:grey;">LEFT</span> <span style="color:grey;">JOIN</span> <span style="color:green;">sys</span><span style="color:grey;">.</span><span style="color:green;">syslogins</span> l</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">ON </span><span style="color:grey;">(</span> l<span style="color:grey;">.</span>name <span style="color:grey;">=</span> p<span style="color:grey;">.</span>name <span style="color:grey;">)</span> <span style="color:blue;">WHERE</span> p<span style="color:grey;">.</span><span style="color:blue;">type</span> <span style="color:grey;">IN</span><span style="color:blue;"> </span><span style="color:grey;">(</span> <span style="color:red;">&#8216;S&#8217;</span><span style="color:grey;">,</span> <span style="color:red;">&#8216;G&#8217;</span><span style="color:grey;">,</span> <span style="color:red;">&#8216;U&#8217;</span> <span style="color:grey;">)</span> <span style="color:grey;">AND</span> p<span style="color:grey;">.</span>name <span style="color:grey;">&lt;&gt;</span> <span style="color:red;">&#8216;sa&#8217;</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">ELSE</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp; </span><span style="color:blue;">DECLARE</span>login_curs <span style="color:blue;">CURSOR</span> <span style="color:blue;">FOR</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">SELECT</span> p<span style="color:grey;">.</span><span style="color:blue;">sid</span><span style="color:grey;">,</span> p<span style="color:grey;">.</span>name<span style="color:grey;">,</span> p<span style="color:grey;">.</span><span style="color:blue;">type</span><span style="color:grey;">,</span> p<span style="color:grey;">.</span>is_disabled<span style="color:grey;">,</span> p<span style="color:grey;">.</span>default_database_name<span style="color:grey;">,</span>l<span style="color:grey;">.</span>hasaccess<span style="color:grey;">,</span> l<span style="color:grey;">.</span>denylogin <span style="color:blue;">FROM</span> </span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:green;font-family:&quot;font-size:10pt;">sys</span><span style="color:grey;font-family:&quot;font-size:10pt;">.</span><span style="color:green;font-family:&quot;font-size:10pt;">server_principals</span><span style="font-family:&quot;font-size:10pt;"> p <span style="color:grey;">LEFT</span> <span style="color:grey;">JOIN</span> <span style="color:green;">sys</span><span style="color:grey;">.</span><span style="color:green;">syslogins</span> l</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">ON </span><span style="color:grey;">(</span> l<span style="color:grey;">.</span>name <span style="color:grey;">=</span> p<span style="color:grey;">.</span>name <span style="color:grey;">)</span> <span style="color:blue;">WHERE</span> p<span style="color:grey;">.</span><span style="color:blue;">type</span> <span style="color:grey;">IN</span><span style="color:blue;"> </span><span style="color:grey;">(</span> <span style="color:red;">&#8216;S&#8217;</span><span style="color:grey;">,</span> <span style="color:red;">&#8216;G&#8217;</span><span style="color:grey;">,</span> <span style="color:red;">&#8216;U&#8217;</span> <span style="color:grey;">)</span> <span style="color:grey;">AND</span> p<span style="color:grey;">.</span>name <span style="color:grey;">=</span> @login_name</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">OPEN</span><span style="font-family:&quot;font-size:10pt;"> login_curs</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">FETCH</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:blue;">NEXT</span> <span style="color:blue;">FROM</span> login_curs <span style="color:blue;">INTO</span> @SID_varbinary<span style="color:grey;">,</span>@name<span style="color:grey;">,</span> @type<span style="color:grey;">,</span>@is_disabled<span style="color:grey;">,</span> @defaultdb<span style="color:grey;">,</span> @hasaccess<span style="color:grey;">,</span> @denylogin</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">IF </span><span style="color:grey;font-family:&quot;font-size:10pt;">(</span><span style="color:magenta;font-family:&quot;font-size:10pt;">@@fetch_status</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:grey;">=</span> <span style="color:grey;">-</span>1<span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">BEGIN</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp; </span><span style="color:blue;">PRINT</span> <span style="color:red;">&#8216;No login(s) found.&#8217;</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp; </span><span style="color:blue;">CLOSE</span>login_curs</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp; </span><span style="color:blue;">DEALLOCATE</span>login_curs</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp; </span><span style="color:blue;">RETURN</span> <span style="color:grey;">-</span>1</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">END</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">SET</span><span style="font-family:&quot;font-size:10pt;"> @tmpstr <span style="color:grey;">=</span> <span style="color:red;">&#8216;/* sp_help_revlogin script&#8217;</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">PRINT</span><span style="font-family:&quot;font-size:10pt;"> @tmpstr</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">SET</span><span style="font-family:&quot;font-size:10pt;"> @tmpstr <span style="color:grey;">=</span> <span style="color:red;">&#8216;** Generated &#8216;</span> <span style="color:grey;">+</span> <span style="color:magenta;">CONVERT</span><span style="color:blue;"> </span><span style="color:grey;">(</span><span style="color:blue;">varchar</span><span style="color:grey;">,</span> <span style="color:magenta;">GETDATE</span><span style="color:grey;">())</span> <span style="color:grey;">+</span> <span style="color:red;">&#8216; on &#8216;</span> <span style="color:grey;">+</span> <span style="color:magenta;">@@SERVERNAME</span> <span style="color:grey;">+</span> <span style="color:red;">&#8216; */&#8217;</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">PRINT</span><span style="font-family:&quot;font-size:10pt;"> @tmpstr</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">PRINT</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:red;">&#8221;</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">WHILE </span><span style="color:grey;font-family:&quot;font-size:10pt;">(</span><span style="color:magenta;font-family:&quot;font-size:10pt;">@@fetch_status</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:grey;">&lt;&gt;</span> <span style="color:grey;">-</span>1<span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">BEGIN</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp; </span><span style="color:blue;">IF </span><span style="color:grey;">(</span><span style="color:magenta;">@@fetch_status</span> <span style="color:grey;">&lt;&gt;</span> <span style="color:grey;">-</span>2<span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp; </span><span style="color:blue;">BEGIN</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">PRINT</span> <span style="color:red;">&#8221;</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">SET</span> @tmpstr<span style="color:grey;">=</span> <span style="color:red;">&#8216;&#8211; Login: &#8216;</span> <span style="color:grey;">+</span> @name</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">PRINT</span>@tmpstr</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">IF </span><span style="color:grey;">(</span>@type <span style="color:grey;">IN</span><span style="color:blue;"> </span><span style="color:grey;">(</span> <span style="color:red;">&#8216;G&#8217;</span><span style="color:grey;">,</span> <span style="color:red;">&#8216;U&#8217;</span><span style="color:grey;">))</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">BEGIN</span> <span style="color:green;">&#8211; NT authenticated account/group</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">SET</span>@tmpstr <span style="color:grey;">=</span> <span style="color:red;">&#8216;CREATE LOGIN&#8217;</span> <span style="color:grey;">+</span> <span style="color:magenta;">QUOTENAME</span><span style="color:grey;">(</span> @name <span style="color:grey;">)</span> <span style="color:grey;">+</span> <span style="color:red;">&#8216; FROM WINDOWS WITHDEFAULT_DATABASE = ['</span> <span style="color:grey;">+</span> @defaultdb <span style="color:grey;">+</span> <span style="color:red;">']&#8216;</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">END</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">ELSE</span> <span style="color:blue;">BEGIN</span> <span style="color:green;">&#8211; SQL Serverauthentication</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:green;">&#8211; obtainpassword and sid</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">SET</span>@PWD_varbinary <span style="color:grey;">=</span> <span style="color:magenta;">CAST</span><span style="color:grey;">(</span> <span style="color:magenta;">LOGINPROPERTY</span><span style="color:grey;">(</span> @name<span style="color:grey;">,</span> <span style="color:red;">&#8216;PasswordHash&#8217;</span> <span style="color:grey;">)</span> <span style="color:blue;">AS</span> <span style="color:blue;">varbinary </span><span style="color:grey;">(</span>256<span style="color:grey;">)</span> <span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">EXEC</span>sp_hexadecimal<span style="color:blue;"> </span>@PWD_varbinary<span style="color:grey;">,</span> @PWD_string <span style="color:blue;">OUT</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">EXEC</span>sp_hexadecimal<span style="color:blue;"> </span>@SID_varbinary<span style="color:grey;">,</span>@SID_string <span style="color:blue;">OUT</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:green;">&#8211; obtainpassword policy state</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">SELECT</span>@is_policy_checked <span style="color:grey;">=</span> <span style="color:blue;">CASE</span>is_policy_checked <span style="color:blue;">WHEN</span> 1 <span style="color:blue;">THEN</span> <span style="color:red;">&#8216;ON&#8217;</span> <span style="color:blue;">WHEN</span> 0 <span style="color:blue;">THEN</span> <span style="color:red;">&#8216;OFF&#8217;</span> <span style="color:blue;">ELSE</span> <span style="color:grey;">NULL</span> <span style="color:blue;">END</span> <span style="color:blue;">FROM</span> <span style="color:green;">sys</span><span style="color:grey;">.</span><span style="color:green;">sql_logins</span> <span style="color:blue;">WHERE</span> name <span style="color:grey;">=</span> @name</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">SELECT</span>@is_expiration_checked <span style="color:grey;">=</span> <span style="color:blue;">CASE</span> is_expiration_checked <span style="color:blue;">WHEN</span>1 <span style="color:blue;">THEN</span> <span style="color:red;">&#8216;ON&#8217;</span> <span style="color:blue;">WHEN</span> 0 <span style="color:blue;">THEN</span> <span style="color:red;">&#8216;OFF&#8217;</span> <span style="color:blue;">ELSE</span> <span style="color:grey;">NULL</span> <span style="color:blue;">END</span> <span style="color:blue;">FROM</span> <span style="color:green;">sys</span><span style="color:grey;">.</span><span style="color:green;">sql_logins</span> <span style="color:blue;">WHERE</span> name <span style="color:grey;">=</span> @name</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">SET</span>@tmpstr <span style="color:grey;">=</span> <span style="color:red;">&#8216;CREATE LOGIN&#8217;</span> <span style="color:grey;">+</span> <span style="color:magenta;">QUOTENAME</span><span style="color:grey;">(</span> @name <span style="color:grey;">)</span> <span style="color:grey;">+</span> <span style="color:red;">&#8216; WITH PASSWORD = &#8216;</span> <span style="color:grey;">+</span> @PWD_string <span style="color:grey;">+</span> <span style="color:red;">&#8216; HASHED, SID = &#8216;</span> <span style="color:grey;">+</span>@SID_string <span style="color:grey;">+</span> <span style="color:red;">&#8216;,DEFAULT_DATABASE = ['</span> <span style="color:grey;">+</span> @defaultdb <span style="color:grey;">+</span> <span style="color:red;">']&#8216;</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">IF </span><span style="color:grey;">(</span> @is_policy_checked <span style="color:grey;">IS</span><span style="color:grey;">NOT</span> <span style="color:grey;">NULL</span> <span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">BEGIN</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">SET</span>@tmpstr <span style="color:grey;">=</span> @tmpstr <span style="color:grey;">+</span><span style="color:red;">&#8216;, CHECK_POLICY = &#8216;</span> <span style="color:grey;">+</span>@is_policy_checked</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">END</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">IF </span><span style="color:grey;">(</span> @is_expiration_checked <span style="color:grey;">IS</span><span style="color:grey;">NOT</span> <span style="color:grey;">NULL</span> <span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">BEGIN</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">SET</span>@tmpstr <span style="color:grey;">=</span> @tmpstr <span style="color:grey;">+</span><span style="color:red;">&#8216;, CHECK_EXPIRATION = &#8216;</span> <span style="color:grey;">+</span>@is_expiration_checked</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">END</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">END</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">IF </span><span style="color:grey;">(</span>@denylogin <span style="color:grey;">=</span> 1<span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">BEGIN</span> <span style="color:green;">&#8211; login is denied access</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">SET</span>@tmpstr <span style="color:grey;">=</span> @tmpstr <span style="color:grey;">+</span><span style="color:red;">&#8216;; DENY CONNECT SQL TO &#8216;</span> <span style="color:grey;">+</span> <span style="color:magenta;">QUOTENAME</span><span style="color:grey;">(</span> @name <span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">END</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">ELSE</span> <span style="color:blue;">IF </span><span style="color:grey;">(</span>@hasaccess <span style="color:grey;">=</span> 0<span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">BEGIN</span> <span style="color:green;">&#8211; login exists but does not have access</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">SET</span>@tmpstr <span style="color:grey;">=</span> @tmpstr <span style="color:grey;">+</span><span style="color:red;">&#8216;; REVOKE CONNECT SQL TO &#8216;</span> <span style="color:grey;">+</span> <span style="color:magenta;">QUOTENAME</span><span style="color:grey;">(</span> @name <span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">END</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">IF </span><span style="color:grey;">(</span>@is_disabled <span style="color:grey;">=</span> 1<span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">BEGIN</span> <span style="color:green;">&#8211; login is disabled</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">SET</span>@tmpstr <span style="color:grey;">=</span> @tmpstr <span style="color:grey;">+</span><span style="color:red;">&#8216;; ALTER LOGIN &#8216;</span> <span style="color:grey;">+</span><span style="color:magenta;">QUOTENAME</span><span style="color:grey;">(</span>@name <span style="color:grey;">)</span> <span style="color:grey;">+</span> <span style="color:red;">&#8216; DISABLE&#8217;</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">END</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">PRINT</span>@tmpstr</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp; </span><span style="color:blue;">END</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp; </span><span style="color:blue;">FETCH</span> <span style="color:blue;">NEXT</span> <span style="color:blue;">FROM</span> login_curs <span style="color:blue;">INTO</span> @SID_varbinary<span style="color:grey;">,</span>@name<span style="color:grey;">,</span> @type<span style="color:grey;">,</span>@is_disabled<span style="color:grey;">,</span> @defaultdb<span style="color:grey;">,</span> @hasaccess<span style="color:grey;">,</span> @denylogin</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp; </span><span style="color:blue;">END</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">CLOSE</span><span style="font-family:&quot;font-size:10pt;"> login_curs</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">DEALLOCATE</span><span style="font-family:&quot;font-size:10pt;"> login_curs</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">RETURN</span><span style="font-family:&quot;font-size:10pt;"> 0</span></div>
<div class="MsoNormal"><span style="color:blue;font-family:&quot;font-size:10pt;line-height:115%;">GO</span></div>
<div class="MsoNormal"></div>
<div class="MsoNormal">This script is copied from <a href="http://support.microsoft.com/kb/918992">here</a>.</div>
<div class="MsoNormal"></div>
<div class="MsoNormal">Now all you need to do is, run stored procedure “<b><span style="font-size:12pt;line-height:115%;">sp_help_revlogin</span></b>” with following command.</div>
<div class="MsoNormal"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">USE</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:blue;">master</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">GO</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">EXEC</span><span style="font-family:&quot;font-size:10pt;">sp_help_revlogin</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">GO</span></div>
<div class="MsoNormal"></div>
<div class="MsoNormal">This is really very helpful script which I have even used somany times. Keep it handy, you never know when you will need it.</div>
<div class="MsoNormal"></div>
<div class="MsoNormal"><b><span style="color:#333333;">Reference: Ritesh Shah</span></b></div>
<div class="MsoNormal"></div>
<div class="MsoNormal"><a href="http://www.sqlhub.com/"><b>http://www.sqlhub.com</b></a><b><span style="color:#333333;"><br />Note: Microsoft Books online is a default reference of all articles butexamples and explanations prepared by Ritesh Shah, founder of </span></b><a href="http://www.sqlhub.com/"><b>http://www.SQLHub.com</b></a></div>
<div class="MsoNormal"></div>
<div class="MsoNormal"><b><span style="color:red;">Ask me any SQL Server related question at my “<a href="http://beyondrelational.com/ask/ritesh/default.aspx">ASK Profile</a>”</span></b></div>
<div class="MsoNormal"></div>
</div>
<div class="blogger-post-footer">Microsoft SQL Server Blog. Fight the fear of SQL with SQLHub.com. Founder is Ritesh Shah<img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/9075786158122089104-1872425695856629560?l=www.sqlhub.com' alt='' /></div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/riteshshah.wordpress.com/831/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/riteshshah.wordpress.com/831/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/riteshshah.wordpress.com/831/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/riteshshah.wordpress.com/831/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/riteshshah.wordpress.com/831/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/riteshshah.wordpress.com/831/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/riteshshah.wordpress.com/831/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/riteshshah.wordpress.com/831/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/riteshshah.wordpress.com/831/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/riteshshah.wordpress.com/831/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/riteshshah.wordpress.com/831/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/riteshshah.wordpress.com/831/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/riteshshah.wordpress.com/831/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/riteshshah.wordpress.com/831/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=riteshshah.wordpress.com&amp;blog=6754420&amp;post=831&amp;subd=riteshshah&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://riteshshah.wordpress.com/2011/08/05/move-login-with-password-from-one-sql-server-instance-to-another-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/77d9fe086ee2f3980400752a46f3b66f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Ritesh Shah</media:title>
		</media:content>

		<media:content url="https://blogger.googleusercontent.com/tracker/9075786158122089104-1872425695856629560?l=www.sqlhub.com" medium="image" />
	</item>
		<item>
		<title>Move login with password from one SQL Server instance to another</title>
		<link>http://riteshshah.wordpress.com/2011/08/05/move-login-with-password-from-one-sql-server-instance-to-another/</link>
		<comments>http://riteshshah.wordpress.com/2011/08/05/move-login-with-password-from-one-sql-server-instance-to-another/#comments</comments>
		<pubDate>Fri, 05 Aug 2011 08:03:00 +0000</pubDate>
		<dc:creator>riteshshah</dc:creator>
				<category><![CDATA[login]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[MS SQL Server]]></category>
		<category><![CDATA[Ritesh Shah]]></category>
		<category><![CDATA[user]]></category>

		<guid isPermaLink="false">http://riteshshah.wordpress.com/2011/08/05/move-login-with-password-from-one-sql-server-instance-to-another</guid>
		<description><![CDATA[I have already written a small script to fix orphanusers while moving database from one server to anotherserver but the major problem is, that script just auto fix the login but can’thave login with the same password which old instance/server was having. Microsoft has provided one small script to help you outin this situation. Look [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=riteshshah.wordpress.com&amp;blog=6754420&amp;post=829&amp;subd=riteshshah&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div dir="ltr" style="text-align:left;"><!--[if gte mso 9]&gt;   Normal  0          false  false  false    EN-US  X-NONE  X-NONE                                       MicrosoftInternetExplorer4                                     &lt;![endif]--><!--[if gte mso 9]&gt;                                                                                                                                                                                                                                                                                    &lt;![endif]--><!--[if gte mso 10]&gt; /* Style Definitions */ table.MsoNormalTable	{mso-style-name:"Table Normal";	mso-tstyle-rowband-size:0;	mso-tstyle-colband-size:0;	mso-style-noshow:yes;	mso-style-priority:99;	mso-style-qformat:yes;	mso-style-parent:"";	mso-padding-alt:0in 5.4pt 0in 5.4pt;	mso-para-margin:0in;	mso-para-margin-bottom:.0001pt;	mso-pagination:widow-orphan;	font-size:10.0pt;	font-family:"Calibri","sans-serif";}&lt;![endif]-->
<div class="MsoNormal">I have already written a small script to fix <b><span style="font-size:12pt;line-height:115%;"><a href="http://www.sqlhub.com/2009/04/fix-user-and-login-synchronization.html">orphanusers</a></span></b><span style="font-size:12pt;line-height:115%;"> </span>while moving database from one server to anotherserver but the major problem is, that script just auto fix the login but can’thave login with the same password which old instance/server was having.</div>
<div class="MsoNormal"></div>
<div class="MsoNormal">Microsoft has provided one <a href="http://support.microsoft.com/kb/918992">small script</a> to help you outin this situation. Look at the below script which will generate T-SQL for allusers available in current instance with password so that you can run that sameLogin script in another server/instance of SQL Server and have samelogin/password.</div>
<div class="MsoNormal"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">USE</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:blue;">master</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">GO</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">IF</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:magenta;">OBJECT_ID</span><span style="color:blue;"> </span><span style="color:grey;">(</span><span style="color:red;">&#8216;sp_hexadecimal&#8217;</span><span style="color:grey;">)</span> <span style="color:grey;">IS</span> <span style="color:grey;">NOT</span> <span style="color:grey;">NULL</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp; </span><span style="color:blue;">DROP</span> <span style="color:blue;">PROCEDURE</span> sp_hexadecimal</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">GO</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">CREATE</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:blue;">PROCEDURE</span> sp_hexadecimal</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp; </span>@binvalue <span style="color:blue;">varbinary</span><span style="color:grey;">(</span>256<span style="color:grey;">),</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp; </span>@hexvalue <span style="color:blue;">varchar </span><span style="color:grey;">(</span>514<span style="color:grey;">)</span> <span style="color:blue;">OUTPUT</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">AS</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">DECLARE</span><span style="font-family:&quot;font-size:10pt;"> @charvalue<span style="color:blue;">varchar </span><span style="color:grey;">(</span>514<span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">DECLARE</span><span style="font-family:&quot;font-size:10pt;"> @i <span style="color:blue;">int</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">DECLARE</span><span style="font-family:&quot;font-size:10pt;"> @length <span style="color:blue;">int</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">DECLARE</span><span style="font-family:&quot;font-size:10pt;"> @hexstring<span style="color:blue;">char</span><span style="color:grey;">(</span>16<span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">SELECT</span><span style="font-family:&quot;font-size:10pt;"> @charvalue<span style="color:grey;">=</span> <span style="color:red;">&#8217;0x&#8217;</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">SELECT</span><span style="font-family:&quot;font-size:10pt;"> @i <span style="color:grey;">=</span> 1</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">SELECT</span><span style="font-family:&quot;font-size:10pt;"> @length <span style="color:grey;">=</span> <span style="color:magenta;">DATALENGTH</span><span style="color:blue;"> </span><span style="color:grey;">(</span>@binvalue<span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">SELECT</span><span style="font-family:&quot;font-size:10pt;"> @hexstring<span style="color:grey;">=</span> <span style="color:red;">&#8217;0123456789ABCDEF&#8217;</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">WHILE </span><span style="color:grey;font-family:&quot;font-size:10pt;">(</span><span style="font-family:&quot;font-size:10pt;">@i <span style="color:grey;">&lt;=</span> @length<span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">BEGIN</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp; </span><span style="color:blue;">DECLARE</span>@tempint <span style="color:blue;">int</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp; </span><span style="color:blue;">DECLARE</span>@firstint <span style="color:blue;">int</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp; </span><span style="color:blue;">DECLARE</span>@secondint <span style="color:blue;">int</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp; </span><span style="color:blue;">SELECT</span> @tempint<span style="color:grey;">=</span> <span style="color:magenta;">CONVERT</span><span style="color:grey;">(</span><span style="color:blue;">int</span><span style="color:grey;">,</span> <span style="color:magenta;">SUBSTRING</span><span style="color:grey;">(</span>@binvalue<span style="color:grey;">,</span>@i<span style="color:grey;">,</span>1<span style="color:grey;">))</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp; </span><span style="color:blue;">SELECT</span>@firstint <span style="color:grey;">=</span> <span style="color:magenta;">FLOOR</span><span style="color:grey;">(</span>@tempint<span style="color:grey;">/</span>16<span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp; </span><span style="color:blue;">SELECT</span>@secondint <span style="color:grey;">=</span> @tempint <span style="color:grey;">-</span><span style="color:blue;"> </span><span style="color:grey;">(</span>@firstint<span style="color:grey;">*</span>16<span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp; </span><span style="color:blue;">SELECT</span>@charvalue <span style="color:grey;">=</span> @charvalue <span style="color:grey;">+</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp; </span><span style="color:magenta;">SUBSTRING</span><span style="color:grey;">(</span>@hexstring<span style="color:grey;">,</span>@firstint<span style="color:grey;">+</span>1<span style="color:grey;">,</span> 1<span style="color:grey;">)</span> <span style="color:grey;">+</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp; </span><span style="color:magenta;">SUBSTRING</span><span style="color:grey;">(</span>@hexstring<span style="color:grey;">,</span>@secondint<span style="color:grey;">+</span>1<span style="color:grey;">,</span> 1<span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp; </span><span style="color:blue;">SELECT</span> @i <span style="color:grey;">=</span> @i <span style="color:grey;">+</span> 1</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">END</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">SELECT</span><span style="font-family:&quot;font-size:10pt;"> @hexvalue <span style="color:grey;">=</span> @charvalue</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">GO</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">IF</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:magenta;">OBJECT_ID</span><span style="color:blue;"> </span><span style="color:grey;">(</span><span style="color:red;">&#8216;sp_help_revlogin&#8217;</span><span style="color:grey;">)</span> <span style="color:grey;">IS</span> <span style="color:grey;">NOT</span> <span style="color:grey;">NULL</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp; </span><span style="color:blue;">DROP</span> <span style="color:blue;">PROCEDURE</span> sp_help_revlogin</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">GO</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">CREATE</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:blue;">PROCEDURE</span> sp_help_revlogin @login_name <span style="color:blue;">sysname</span> <span style="color:grey;">=</span> <span style="color:grey;">NULL</span> <span style="color:blue;">AS</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">DECLARE</span><span style="font-family:&quot;font-size:10pt;"> @name <span style="color:blue;">sysname</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">DECLARE</span><span style="font-family:&quot;font-size:10pt;"> @type <span style="color:blue;">varchar </span><span style="color:grey;">(</span>1<span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">DECLARE</span><span style="font-family:&quot;font-size:10pt;"> @hasaccess<span style="color:blue;">int</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">DECLARE</span><span style="font-family:&quot;font-size:10pt;"> @denylogin<span style="color:blue;">int</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">DECLARE</span><span style="font-family:&quot;font-size:10pt;">@is_disabled <span style="color:blue;">int</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">DECLARE</span><span style="font-family:&quot;font-size:10pt;">@PWD_varbinary<span>&nbsp; </span><span style="color:blue;">varbinary</span><span style="color:grey;">(</span>256<span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">DECLARE</span><span style="font-family:&quot;font-size:10pt;">@PWD_string<span>&nbsp; </span><span style="color:blue;">varchar</span><span style="color:grey;">(</span>514<span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">DECLARE</span><span style="font-family:&quot;font-size:10pt;">@SID_varbinary <span style="color:blue;">varbinary </span><span style="color:grey;">(</span>85<span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">DECLARE</span><span style="font-family:&quot;font-size:10pt;">@SID_string <span style="color:blue;">varchar </span><span style="color:grey;">(</span>514<span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">DECLARE</span><span style="font-family:&quot;font-size:10pt;">@tmpstr<span>&nbsp; </span><span style="color:blue;">varchar</span><span style="color:grey;">(</span>1024<span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">DECLARE</span><span style="font-family:&quot;font-size:10pt;">@is_policy_checked <span style="color:blue;">varchar </span><span style="color:grey;">(</span>3<span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">DECLARE</span><span style="font-family:&quot;font-size:10pt;">@is_expiration_checked <span style="color:blue;">varchar </span><span style="color:grey;">(</span>3<span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">DECLARE</span><span style="font-family:&quot;font-size:10pt;"> @defaultdb<span style="color:blue;">sysname</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">IF </span><span style="color:grey;font-family:&quot;font-size:10pt;">(</span><span style="font-family:&quot;font-size:10pt;">@login_name<span style="color:grey;">IS</span> <span style="color:grey;">NULL)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp; </span><span style="color:blue;">DECLARE</span>login_curs <span style="color:blue;">CURSOR</span> <span style="color:blue;">FOR</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">SELECT</span> p<span style="color:grey;">.</span><span style="color:blue;">sid</span><span style="color:grey;">,</span> p<span style="color:grey;">.</span>name<span style="color:grey;">,</span> p<span style="color:grey;">.</span><span style="color:blue;">type</span><span style="color:grey;">,</span> p<span style="color:grey;">.</span>is_disabled<span style="color:grey;">,</span> p<span style="color:grey;">.</span>default_database_name<span style="color:grey;">,</span>l<span style="color:grey;">.</span>hasaccess<span style="color:grey;">,</span> l<span style="color:grey;">.</span>denylogin <span style="color:blue;">FROM</span> </span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:green;font-family:&quot;font-size:10pt;">sys</span><span style="color:grey;font-family:&quot;font-size:10pt;">.</span><span style="color:green;font-family:&quot;font-size:10pt;">server_principals</span><span style="font-family:&quot;font-size:10pt;"> p <span style="color:grey;">LEFT</span> <span style="color:grey;">JOIN</span> <span style="color:green;">sys</span><span style="color:grey;">.</span><span style="color:green;">syslogins</span> l</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">ON </span><span style="color:grey;">(</span> l<span style="color:grey;">.</span>name <span style="color:grey;">=</span> p<span style="color:grey;">.</span>name <span style="color:grey;">)</span> <span style="color:blue;">WHERE</span> p<span style="color:grey;">.</span><span style="color:blue;">type</span> <span style="color:grey;">IN</span><span style="color:blue;"> </span><span style="color:grey;">(</span> <span style="color:red;">&#8216;S&#8217;</span><span style="color:grey;">,</span> <span style="color:red;">&#8216;G&#8217;</span><span style="color:grey;">,</span> <span style="color:red;">&#8216;U&#8217;</span> <span style="color:grey;">)</span> <span style="color:grey;">AND</span> p<span style="color:grey;">.</span>name <span style="color:grey;">&lt;&gt;</span> <span style="color:red;">&#8216;sa&#8217;</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">ELSE</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp; </span><span style="color:blue;">DECLARE</span>login_curs <span style="color:blue;">CURSOR</span> <span style="color:blue;">FOR</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">SELECT</span> p<span style="color:grey;">.</span><span style="color:blue;">sid</span><span style="color:grey;">,</span> p<span style="color:grey;">.</span>name<span style="color:grey;">,</span> p<span style="color:grey;">.</span><span style="color:blue;">type</span><span style="color:grey;">,</span> p<span style="color:grey;">.</span>is_disabled<span style="color:grey;">,</span> p<span style="color:grey;">.</span>default_database_name<span style="color:grey;">,</span>l<span style="color:grey;">.</span>hasaccess<span style="color:grey;">,</span> l<span style="color:grey;">.</span>denylogin <span style="color:blue;">FROM</span> </span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:green;font-family:&quot;font-size:10pt;">sys</span><span style="color:grey;font-family:&quot;font-size:10pt;">.</span><span style="color:green;font-family:&quot;font-size:10pt;">server_principals</span><span style="font-family:&quot;font-size:10pt;"> p <span style="color:grey;">LEFT</span> <span style="color:grey;">JOIN</span> <span style="color:green;">sys</span><span style="color:grey;">.</span><span style="color:green;">syslogins</span> l</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">ON </span><span style="color:grey;">(</span> l<span style="color:grey;">.</span>name <span style="color:grey;">=</span> p<span style="color:grey;">.</span>name <span style="color:grey;">)</span> <span style="color:blue;">WHERE</span> p<span style="color:grey;">.</span><span style="color:blue;">type</span> <span style="color:grey;">IN</span><span style="color:blue;"> </span><span style="color:grey;">(</span> <span style="color:red;">&#8216;S&#8217;</span><span style="color:grey;">,</span> <span style="color:red;">&#8216;G&#8217;</span><span style="color:grey;">,</span> <span style="color:red;">&#8216;U&#8217;</span> <span style="color:grey;">)</span> <span style="color:grey;">AND</span> p<span style="color:grey;">.</span>name <span style="color:grey;">=</span> @login_name</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">OPEN</span><span style="font-family:&quot;font-size:10pt;"> login_curs</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">FETCH</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:blue;">NEXT</span> <span style="color:blue;">FROM</span> login_curs <span style="color:blue;">INTO</span> @SID_varbinary<span style="color:grey;">,</span>@name<span style="color:grey;">,</span> @type<span style="color:grey;">,</span>@is_disabled<span style="color:grey;">,</span> @defaultdb<span style="color:grey;">,</span> @hasaccess<span style="color:grey;">,</span> @denylogin</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">IF </span><span style="color:grey;font-family:&quot;font-size:10pt;">(</span><span style="color:magenta;font-family:&quot;font-size:10pt;">@@fetch_status</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:grey;">=</span> <span style="color:grey;">-</span>1<span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">BEGIN</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp; </span><span style="color:blue;">PRINT</span> <span style="color:red;">&#8216;No login(s) found.&#8217;</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp; </span><span style="color:blue;">CLOSE</span>login_curs</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp; </span><span style="color:blue;">DEALLOCATE</span>login_curs</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp; </span><span style="color:blue;">RETURN</span> <span style="color:grey;">-</span>1</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">END</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">SET</span><span style="font-family:&quot;font-size:10pt;"> @tmpstr <span style="color:grey;">=</span> <span style="color:red;">&#8216;/* sp_help_revlogin script&#8217;</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">PRINT</span><span style="font-family:&quot;font-size:10pt;"> @tmpstr</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">SET</span><span style="font-family:&quot;font-size:10pt;"> @tmpstr <span style="color:grey;">=</span> <span style="color:red;">&#8216;** Generated &#8216;</span> <span style="color:grey;">+</span> <span style="color:magenta;">CONVERT</span><span style="color:blue;"> </span><span style="color:grey;">(</span><span style="color:blue;">varchar</span><span style="color:grey;">,</span> <span style="color:magenta;">GETDATE</span><span style="color:grey;">())</span> <span style="color:grey;">+</span> <span style="color:red;">&#8216; on &#8216;</span> <span style="color:grey;">+</span> <span style="color:magenta;">@@SERVERNAME</span> <span style="color:grey;">+</span> <span style="color:red;">&#8216; */&#8217;</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">PRINT</span><span style="font-family:&quot;font-size:10pt;"> @tmpstr</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">PRINT</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:red;">&#8221;</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">WHILE </span><span style="color:grey;font-family:&quot;font-size:10pt;">(</span><span style="color:magenta;font-family:&quot;font-size:10pt;">@@fetch_status</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:grey;">&lt;&gt;</span> <span style="color:grey;">-</span>1<span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">BEGIN</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp; </span><span style="color:blue;">IF </span><span style="color:grey;">(</span><span style="color:magenta;">@@fetch_status</span> <span style="color:grey;">&lt;&gt;</span> <span style="color:grey;">-</span>2<span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp; </span><span style="color:blue;">BEGIN</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">PRINT</span> <span style="color:red;">&#8221;</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">SET</span> @tmpstr<span style="color:grey;">=</span> <span style="color:red;">&#8216;&#8211; Login: &#8216;</span> <span style="color:grey;">+</span> @name</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">PRINT</span>@tmpstr</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">IF </span><span style="color:grey;">(</span>@type <span style="color:grey;">IN</span><span style="color:blue;"> </span><span style="color:grey;">(</span> <span style="color:red;">&#8216;G&#8217;</span><span style="color:grey;">,</span> <span style="color:red;">&#8216;U&#8217;</span><span style="color:grey;">))</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">BEGIN</span> <span style="color:green;">&#8211; NT authenticated account/group</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">SET</span>@tmpstr <span style="color:grey;">=</span> <span style="color:red;">&#8216;CREATE LOGIN&#8217;</span> <span style="color:grey;">+</span> <span style="color:magenta;">QUOTENAME</span><span style="color:grey;">(</span> @name <span style="color:grey;">)</span> <span style="color:grey;">+</span> <span style="color:red;">&#8216; FROM WINDOWS WITHDEFAULT_DATABASE = ['</span> <span style="color:grey;">+</span> @defaultdb <span style="color:grey;">+</span> <span style="color:red;">']&#8216;</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">END</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">ELSE</span> <span style="color:blue;">BEGIN</span> <span style="color:green;">&#8211; SQL Serverauthentication</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:green;">&#8211; obtainpassword and sid</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">SET</span>@PWD_varbinary <span style="color:grey;">=</span> <span style="color:magenta;">CAST</span><span style="color:grey;">(</span> <span style="color:magenta;">LOGINPROPERTY</span><span style="color:grey;">(</span> @name<span style="color:grey;">,</span> <span style="color:red;">&#8216;PasswordHash&#8217;</span> <span style="color:grey;">)</span> <span style="color:blue;">AS</span> <span style="color:blue;">varbinary </span><span style="color:grey;">(</span>256<span style="color:grey;">)</span> <span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">EXEC</span>sp_hexadecimal<span style="color:blue;"> </span>@PWD_varbinary<span style="color:grey;">,</span> @PWD_string <span style="color:blue;">OUT</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">EXEC</span>sp_hexadecimal<span style="color:blue;"> </span>@SID_varbinary<span style="color:grey;">,</span>@SID_string <span style="color:blue;">OUT</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:green;">&#8211; obtainpassword policy state</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">SELECT</span>@is_policy_checked <span style="color:grey;">=</span> <span style="color:blue;">CASE</span>is_policy_checked <span style="color:blue;">WHEN</span> 1 <span style="color:blue;">THEN</span> <span style="color:red;">&#8216;ON&#8217;</span> <span style="color:blue;">WHEN</span> 0 <span style="color:blue;">THEN</span> <span style="color:red;">&#8216;OFF&#8217;</span> <span style="color:blue;">ELSE</span> <span style="color:grey;">NULL</span> <span style="color:blue;">END</span> <span style="color:blue;">FROM</span> <span style="color:green;">sys</span><span style="color:grey;">.</span><span style="color:green;">sql_logins</span> <span style="color:blue;">WHERE</span> name <span style="color:grey;">=</span> @name</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">SELECT</span>@is_expiration_checked <span style="color:grey;">=</span> <span style="color:blue;">CASE</span> is_expiration_checked <span style="color:blue;">WHEN</span>1 <span style="color:blue;">THEN</span> <span style="color:red;">&#8216;ON&#8217;</span> <span style="color:blue;">WHEN</span> 0 <span style="color:blue;">THEN</span> <span style="color:red;">&#8216;OFF&#8217;</span> <span style="color:blue;">ELSE</span> <span style="color:grey;">NULL</span> <span style="color:blue;">END</span> <span style="color:blue;">FROM</span> <span style="color:green;">sys</span><span style="color:grey;">.</span><span style="color:green;">sql_logins</span> <span style="color:blue;">WHERE</span> name <span style="color:grey;">=</span> @name</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">SET</span>@tmpstr <span style="color:grey;">=</span> <span style="color:red;">&#8216;CREATE LOGIN&#8217;</span> <span style="color:grey;">+</span> <span style="color:magenta;">QUOTENAME</span><span style="color:grey;">(</span> @name <span style="color:grey;">)</span> <span style="color:grey;">+</span> <span style="color:red;">&#8216; WITH PASSWORD = &#8216;</span> <span style="color:grey;">+</span> @PWD_string <span style="color:grey;">+</span> <span style="color:red;">&#8216; HASHED, SID = &#8216;</span> <span style="color:grey;">+</span>@SID_string <span style="color:grey;">+</span> <span style="color:red;">&#8216;,DEFAULT_DATABASE = ['</span> <span style="color:grey;">+</span> @defaultdb <span style="color:grey;">+</span> <span style="color:red;">']&#8216;</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">IF </span><span style="color:grey;">(</span> @is_policy_checked <span style="color:grey;">IS</span><span style="color:grey;">NOT</span> <span style="color:grey;">NULL</span> <span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">BEGIN</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">SET</span>@tmpstr <span style="color:grey;">=</span> @tmpstr <span style="color:grey;">+</span><span style="color:red;">&#8216;, CHECK_POLICY = &#8216;</span> <span style="color:grey;">+</span>@is_policy_checked</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">END</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">IF </span><span style="color:grey;">(</span> @is_expiration_checked <span style="color:grey;">IS</span><span style="color:grey;">NOT</span> <span style="color:grey;">NULL</span> <span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">BEGIN</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">SET</span>@tmpstr <span style="color:grey;">=</span> @tmpstr <span style="color:grey;">+</span><span style="color:red;">&#8216;, CHECK_EXPIRATION = &#8216;</span> <span style="color:grey;">+</span>@is_expiration_checked</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">END</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">END</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">IF </span><span style="color:grey;">(</span>@denylogin <span style="color:grey;">=</span> 1<span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">BEGIN</span> <span style="color:green;">&#8211; login is denied access</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">SET</span>@tmpstr <span style="color:grey;">=</span> @tmpstr <span style="color:grey;">+</span><span style="color:red;">&#8216;; DENY CONNECT SQL TO &#8216;</span> <span style="color:grey;">+</span> <span style="color:magenta;">QUOTENAME</span><span style="color:grey;">(</span> @name <span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">END</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">ELSE</span> <span style="color:blue;">IF </span><span style="color:grey;">(</span>@hasaccess <span style="color:grey;">=</span> 0<span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">BEGIN</span> <span style="color:green;">&#8211; login exists but does not have access</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">SET</span>@tmpstr <span style="color:grey;">=</span> @tmpstr <span style="color:grey;">+</span><span style="color:red;">&#8216;; REVOKE CONNECT SQL TO &#8216;</span> <span style="color:grey;">+</span> <span style="color:magenta;">QUOTENAME</span><span style="color:grey;">(</span> @name <span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">END</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">IF </span><span style="color:grey;">(</span>@is_disabled <span style="color:grey;">=</span> 1<span style="color:grey;">)</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">BEGIN</span> <span style="color:green;">&#8211; login is disabled</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">SET</span>@tmpstr <span style="color:grey;">=</span> @tmpstr <span style="color:grey;">+</span><span style="color:red;">&#8216;; ALTER LOGIN &#8216;</span> <span style="color:grey;">+</span><span style="color:magenta;">QUOTENAME</span><span style="color:grey;">(</span>@name <span style="color:grey;">)</span> <span style="color:grey;">+</span> <span style="color:red;">&#8216; DISABLE&#8217;</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">END</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp;&nbsp; </span><span style="color:blue;">PRINT</span>@tmpstr</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp; </span><span style="color:blue;">END</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp; </span><span style="color:blue;">FETCH</span> <span style="color:blue;">NEXT</span> <span style="color:blue;">FROM</span> login_curs <span style="color:blue;">INTO</span> @SID_varbinary<span style="color:grey;">,</span>@name<span style="color:grey;">,</span> @type<span style="color:grey;">,</span>@is_disabled<span style="color:grey;">,</span> @defaultdb<span style="color:grey;">,</span> @hasaccess<span style="color:grey;">,</span> @denylogin</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="font-family:&quot;font-size:10pt;"><span>&nbsp;&nbsp; </span><span style="color:blue;">END</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">CLOSE</span><span style="font-family:&quot;font-size:10pt;"> login_curs</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">DEALLOCATE</span><span style="font-family:&quot;font-size:10pt;"> login_curs</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">RETURN</span><span style="font-family:&quot;font-size:10pt;"> 0</span></div>
<div class="MsoNormal"><span style="color:blue;font-family:&quot;font-size:10pt;line-height:115%;">GO</span></div>
<div class="MsoNormal"></div>
<div class="MsoNormal">This script is copied from <a href="http://support.microsoft.com/kb/918992">here</a>.</div>
<div class="MsoNormal"></div>
<div class="MsoNormal">Now all you need to do is, run stored procedure “<b><span style="font-size:12pt;line-height:115%;">sp_help_revlogin</span></b>” with following command.</div>
<div class="MsoNormal"></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">USE</span><span style="font-family:&quot;font-size:10pt;"> <span style="color:blue;">master</span></span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">GO</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">EXEC</span><span style="font-family:&quot;font-size:10pt;">sp_help_revlogin</span></div>
<div class="MsoNormal" style="line-height:normal;margin-bottom:0;"><span style="color:blue;font-family:&quot;font-size:10pt;">GO</span></div>
<div class="MsoNormal"></div>
<div class="MsoNormal">This is really very helpful script which I have even used somany times. Keep it handy, you never know when you will need it.</div>
<div class="MsoNormal"></div>
<div class="MsoNormal"><b><span style="color:#333333;">Reference: Ritesh Shah</span></b></div>
<div class="MsoNormal"></div>
<div class="MsoNormal"><a href="http://www.sqlhub.com/"><b>http://www.sqlhub.com</b></a><b><span style="color:#333333;"><br />Note: Microsoft Books online is a default reference of all articles butexamples and explanations prepared by Ritesh Shah, founder of </span></b><a href="http://www.sqlhub.com/"><b>http://www.SQLHub.com</b></a></div>
<div class="MsoNormal"></div>
<div class="MsoNormal"><b><span style="color:red;">Ask me any SQL Server related question at my “<a href="http://beyondrelational.com/ask/ritesh/default.aspx">ASK Profile</a>”</span></b></div>
<div class="MsoNormal"></div>
</div>
<div class="blogger-post-footer">Microsoft SQL Server Blog. Fight the fear of SQL with SQLHub.com. Founder is Ritesh Shah<img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/9075786158122089104-1872425695856629560?l=www.sqlhub.com' alt='' /></div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/riteshshah.wordpress.com/829/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/riteshshah.wordpress.com/829/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/riteshshah.wordpress.com/829/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/riteshshah.wordpress.com/829/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/riteshshah.wordpress.com/829/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/riteshshah.wordpress.com/829/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/riteshshah.wordpress.com/829/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/riteshshah.wordpress.com/829/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/riteshshah.wordpress.com/829/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/riteshshah.wordpress.com/829/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/riteshshah.wordpress.com/829/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/riteshshah.wordpress.com/829/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/riteshshah.wordpress.com/829/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/riteshshah.wordpress.com/829/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=riteshshah.wordpress.com&amp;blog=6754420&amp;post=829&amp;subd=riteshshah&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://riteshshah.wordpress.com/2011/08/05/move-login-with-password-from-one-sql-server-instance-to-another/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/77d9fe086ee2f3980400752a46f3b66f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Ritesh Shah</media:title>
		</media:content>

		<media:content url="https://blogger.googleusercontent.com/tracker/9075786158122089104-1872425695856629560?l=www.sqlhub.com" medium="image" />
	</item>
		<item>
		<title>Do I need backup of my database?</title>
		<link>http://riteshshah.wordpress.com/2011/08/01/do-i-need-backup-of-my-database-2/</link>
		<comments>http://riteshshah.wordpress.com/2011/08/01/do-i-need-backup-of-my-database-2/#comments</comments>
		<pubDate>Mon, 01 Aug 2011 13:35:00 +0000</pubDate>
		<dc:creator>riteshshah</dc:creator>
				<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[MS SQL Server]]></category>
		<category><![CDATA[Ritesh Shah]]></category>

		<guid isPermaLink="false">http://riteshshah.wordpress.com/2011/08/01/do-i-need-backup-of-my-database-2</guid>
		<description><![CDATA[If you are working as a DBA or you have responsibility tomaintain your SQL Server database up &#38; running, take my words, you have NOOPTION of full database backup. Recently I came to know that one person is not taking backupjust because he has RAID and Mirroring setup so he is least worried aboutbackup. Believe [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=riteshshah.wordpress.com&amp;blog=6754420&amp;post=828&amp;subd=riteshshah&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div dir="ltr" style="text-align:left;"><!--[if gte mso 9]&gt;   Normal  0          false  false  false    EN-US  X-NONE  X-NONE                                       MicrosoftInternetExplorer4                                     &lt;![endif]--><!--[if gte mso 9]&gt;                                                                                                                                                                                                                                                                                    &lt;![endif]--><!--[if gte mso 10]&gt; /* Style Definitions */ table.MsoNormalTable	{mso-style-name:"Table Normal";	mso-tstyle-rowband-size:0;	mso-tstyle-colband-size:0;	mso-style-noshow:yes;	mso-style-priority:99;	mso-style-qformat:yes;	mso-style-parent:"";	mso-padding-alt:0in 5.4pt 0in 5.4pt;	mso-para-margin:0in;	mso-para-margin-bottom:.0001pt;	mso-pagination:widow-orphan;	font-size:10.0pt;	font-family:"Calibri","sans-serif";}&lt;![endif]-->
<div class="MsoNormal">If you are working as a DBA or you have responsibility tomaintain your SQL Server database up &amp; running, take my words, you have NOOPTION of full database backup.</div>
<div class="MsoNormal"></div>
<div class="MsoNormal">Recently I came to know that one person is not taking backupjust because he has RAID and Mirroring setup so he is least worried aboutbackup. Believe me, no matter what redundant hardware / software and highavailability setup you have, you MUST take backup regularly because even goodand up-to-date disaster recovery system could get failed. So, you never know,when and how do you need your latest backup file.</div>
<div class="MsoNormal"></div>
<div class="MsoNormal">Let us discuss why database backup is really that muchimportant.</div>
<div class="MsoNormal"></div>
<div class="MsoNormal">Suppose you have high availability solutions like RAID orserver clustering but what if you hardware gets corrupt and may be completedisk array get corrupt? You have to build up your database from backup only.</div>
<div class="MsoNormal"></div>
<div class="MsoNormal">Sometime, catastrophic events or natural disasters likeflooding, earth quake etc. could damage your hardware or may be complete datacenter and if you have no backup at other location, you have no place to gofor.</div>
<div class="MsoNormal"></div>
<div class="MsoNormal">May be sometime Security issues comes to the picture andsomebody intentionally or unintentionally damage you data which could beaffected adversely and in that situation you might look for the latest databasebackup to restore that data.</div>
<div class="MsoNormal"></div>
<div class="MsoNormal">Sometime your OS or even your SQL Server gets corrupted andyou might need to restore data from backup itself.</div>
<div class="MsoNormal"></div>
<div class="MsoNormal">In short, I would HIGHLY recommend database backup as “Preventionis always better than cure”. So, keep strong backup policies with full databasebackup, differential database backup and transaction log backup, depends onyour need and business policies.</div>
<div class="MsoNormal"></div>
<div class="MsoNormal">BTW, I am not against the high availability concepts likemirroring, clustering, replication or log shipping. You should implement thosetoo as per your business needs but also MUST keep strong backup policies.</div>
<div class="MsoNormal"></div>
<div class="MsoNormal"><b><span style="color:#333333;">Reference: Ritesh Shah</span></b></div>
<div class="MsoNormal"><a href="http://www.sqlhub.com/"><b>http://www.sqlhub.com</b></a><b><span style="color:#333333;"><br />Note: Microsoft Books online is a default reference of all articles butexamples and explanations prepared by Ritesh Shah, founder of </span></b><a href="http://www.sqlhub.com/"><b>http://www.SQLHub.com</b></a></div>
<div class="MsoNormal"><b><span style="color:red;">Ask me any SQL Server related question at my “<a href="http://beyondrelational.com/ask/ritesh/default.aspx">ASK Profile</a>”</span></b></div>
</div>
<div class="blogger-post-footer">Microsoft SQL Server Blog. Fight the fear of SQL with SQLHub.com. Founder is Ritesh Shah<img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/9075786158122089104-6825113043532347293?l=www.sqlhub.com' alt='' /></div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/riteshshah.wordpress.com/828/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/riteshshah.wordpress.com/828/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/riteshshah.wordpress.com/828/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/riteshshah.wordpress.com/828/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/riteshshah.wordpress.com/828/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/riteshshah.wordpress.com/828/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/riteshshah.wordpress.com/828/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/riteshshah.wordpress.com/828/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/riteshshah.wordpress.com/828/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/riteshshah.wordpress.com/828/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/riteshshah.wordpress.com/828/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/riteshshah.wordpress.com/828/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/riteshshah.wordpress.com/828/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/riteshshah.wordpress.com/828/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=riteshshah.wordpress.com&amp;blog=6754420&amp;post=828&amp;subd=riteshshah&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://riteshshah.wordpress.com/2011/08/01/do-i-need-backup-of-my-database-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/77d9fe086ee2f3980400752a46f3b66f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Ritesh Shah</media:title>
		</media:content>

		<media:content url="https://blogger.googleusercontent.com/tracker/9075786158122089104-6825113043532347293?l=www.sqlhub.com" medium="image" />
	</item>
		<item>
		<title>Do I need backup of my database?</title>
		<link>http://riteshshah.wordpress.com/2011/08/01/do-i-need-backup-of-my-database/</link>
		<comments>http://riteshshah.wordpress.com/2011/08/01/do-i-need-backup-of-my-database/#comments</comments>
		<pubDate>Mon, 01 Aug 2011 13:35:00 +0000</pubDate>
		<dc:creator>riteshshah</dc:creator>
				<category><![CDATA[database backup]]></category>
		<category><![CDATA[Microsoft]]></category>
		<category><![CDATA[MS SQL Server]]></category>
		<category><![CDATA[Ritesh Shah]]></category>

		<guid isPermaLink="false">http://riteshshah.wordpress.com/2011/08/01/do-i-need-backup-of-my-database</guid>
		<description><![CDATA[If you are working as a DBA or you have responsibility tomaintain your SQL Server database up &#38; running, take my words, you have NOOPTION of full database backup. Recently I came to know that one person is not taking backupjust because he has RAID and Mirroring setup so he is least worried aboutbackup. Believe [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=riteshshah.wordpress.com&amp;blog=6754420&amp;post=827&amp;subd=riteshshah&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<div dir="ltr" style="text-align:left;"><!--[if gte mso 9]&gt;   Normal  0          false  false  false    EN-US  X-NONE  X-NONE                                       MicrosoftInternetExplorer4                                     &lt;![endif]--><!--[if gte mso 9]&gt;                                                                                                                                                                                                                                                                                    &lt;![endif]--><!--[if gte mso 10]&gt; /* Style Definitions */ table.MsoNormalTable	{mso-style-name:"Table Normal";	mso-tstyle-rowband-size:0;	mso-tstyle-colband-size:0;	mso-style-noshow:yes;	mso-style-priority:99;	mso-style-qformat:yes;	mso-style-parent:"";	mso-padding-alt:0in 5.4pt 0in 5.4pt;	mso-para-margin:0in;	mso-para-margin-bottom:.0001pt;	mso-pagination:widow-orphan;	font-size:10.0pt;	font-family:"Calibri","sans-serif";}&lt;![endif]-->
<div class="MsoNormal">If you are working as a DBA or you have responsibility tomaintain your SQL Server database up &amp; running, take my words, you have NOOPTION of full database backup.</div>
<div class="MsoNormal"></div>
<div class="MsoNormal">Recently I came to know that one person is not taking backupjust because he has RAID and Mirroring setup so he is least worried aboutbackup. Believe me, no matter what redundant hardware / software and highavailability setup you have, you MUST take backup regularly because even goodand up-to-date disaster recovery system could get failed. So, you never know,when and how do you need your latest backup file.</div>
<div class="MsoNormal"></div>
<div class="MsoNormal">Let us discuss why database backup is really that muchimportant.</div>
<div class="MsoNormal"></div>
<div class="MsoNormal">Suppose you have high availability solutions like RAID orserver clustering but what if you hardware gets corrupt and may be completedisk array get corrupt? You have to build up your database from backup only.</div>
<div class="MsoNormal"></div>
<div class="MsoNormal">Sometime, catastrophic events or natural disasters likeflooding, earth quake etc. could damage your hardware or may be complete datacenter and if you have no backup at other location, you have no place to gofor.</div>
<div class="MsoNormal"></div>
<div class="MsoNormal">May be sometime Security issues comes to the picture andsomebody intentionally or unintentionally damage you data which could beaffected adversely and in that situation you might look for the latest databasebackup to restore that data.</div>
<div class="MsoNormal"></div>
<div class="MsoNormal">Sometime your OS or even your SQL Server gets corrupted andyou might need to restore data from backup itself.</div>
<div class="MsoNormal"></div>
<div class="MsoNormal">In short, I would HIGHLY recommend database backup as “Preventionis always better than cure”. So, keep strong backup policies with full databasebackup, differential database backup and transaction log backup, depends onyour need and business policies.</div>
<div class="MsoNormal"></div>
<div class="MsoNormal">BTW, I am not against the high availability concepts likemirroring, clustering, replication or log shipping. You should implement thosetoo as per your business needs but also MUST keep strong backup policies.</div>
<div class="MsoNormal"></div>
<div class="MsoNormal"><b><span style="color:#333333;">Reference: Ritesh Shah</span></b></div>
<div class="MsoNormal"><a href="http://www.sqlhub.com/"><b>http://www.sqlhub.com</b></a><b><span style="color:#333333;"><br />Note: Microsoft Books online is a default reference of all articles butexamples and explanations prepared by Ritesh Shah, founder of </span></b><a href="http://www.sqlhub.com/"><b>http://www.SQLHub.com</b></a></div>
<div class="MsoNormal"><b><span style="color:red;">Ask me any SQL Server related question at my “<a href="http://beyondrelational.com/ask/ritesh/default.aspx">ASK Profile</a>”</span></b></div>
</div>
<div class="blogger-post-footer">Microsoft SQL Server Blog. Fight the fear of SQL with SQLHub.com. Founder is Ritesh Shah<img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/9075786158122089104-6825113043532347293?l=www.sqlhub.com' alt='' /></div>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/riteshshah.wordpress.com/827/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/riteshshah.wordpress.com/827/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/riteshshah.wordpress.com/827/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/riteshshah.wordpress.com/827/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/riteshshah.wordpress.com/827/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/riteshshah.wordpress.com/827/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/riteshshah.wordpress.com/827/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/riteshshah.wordpress.com/827/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/riteshshah.wordpress.com/827/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/riteshshah.wordpress.com/827/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/riteshshah.wordpress.com/827/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/riteshshah.wordpress.com/827/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/riteshshah.wordpress.com/827/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/riteshshah.wordpress.com/827/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=riteshshah.wordpress.com&amp;blog=6754420&amp;post=827&amp;subd=riteshshah&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://riteshshah.wordpress.com/2011/08/01/do-i-need-backup-of-my-database/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/77d9fe086ee2f3980400752a46f3b66f?s=96&#38;d=identicon&#38;r=G" medium="image">
			<media:title type="html">Ritesh Shah</media:title>
		</media:content>

		<media:content url="https://blogger.googleusercontent.com/tracker/9075786158122089104-6825113043532347293?l=www.sqlhub.com" medium="image" />
	</item>
	</channel>
</rss>
