<?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/"
	>

<channel>
	<title>DBTechie</title>
	<atom:link href="http://www.dbtechie.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dbtechie.com</link>
	<description>Database Tips and Tricks</description>
	<pubDate>Thu, 06 Aug 2009 23:41:11 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How to return an empty set with named columns</title>
		<link>http://www.dbtechie.com/sql-server/how-to-return-an-empty-set-with-named-columns/</link>
		<comments>http://www.dbtechie.com/sql-server/how-to-return-an-empty-set-with-named-columns/#comments</comments>
		<pubDate>Wed, 15 Jul 2009 22:30:47 +0000</pubDate>
		<dc:creator>Eric North</dc:creator>
		
		<category><![CDATA[SQL Server]]></category>

		<category><![CDATA[UDF]]></category>

		<guid isPermaLink="false">http://www.dbtechie.com/?p=31</guid>
		<description><![CDATA[I was writing a Sql Server stored procedure and wanted it to return an empty record set if an error occurred and I wanted the column headings to be the same as for the result set when there is no error.  For example, when there is no error the stored procedure would output:




UserName


UserID


UserSex




Joe


1234


Male




Jane


5678


Female




 
When an error [...]]]></description>
			<content:encoded><![CDATA[<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Calibri;">I was writing a Sql Server stored procedure and wanted it to return an empty record set if an error occurred and I wanted the column headings to be the same as for the result set when there is no error.<span style="mso-spacerun: yes;">  </span>For example, when there is no error the stored procedure would output:</span></p>
<table class="MsoNormalTable" style="border-collapse: collapse; mso-border-alt: solid #9BBB59 1.0pt; mso-yfti-tbllook: 1184; mso-padding-alt: 0in 5.4pt 0in 5.4pt;" border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr style="mso-yfti-irow: 0; mso-yfti-firstrow: yes;">
<td style="border-right: #ece9d8; padding-right: 5.4pt; border-top: #9bbb59 1pt solid; padding-left: 5.4pt; background: #9bbb59; padding-bottom: 0in; border-left: #9bbb59 1pt solid; width: 0.95in; padding-top: 0in; border-bottom: #ece9d8; mso-background-themecolor: accent3;" width="91" valign="top">
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><strong><span style="color: white; mso-bidi-font-family: 'Times New Roman'; mso-fareast-font-family: Calibri; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-theme-font: minor-latin; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-bidi-theme-font: minor-bidi; mso-themecolor: background1;"><span style="font-size: small;"><span style="font-family: Calibri;">UserName</span></span></span></strong></p>
</td>
<td style="border-right: #ece9d8; padding-right: 5.4pt; border-top: #9bbb59 1pt solid; padding-left: 5.4pt; background: #9bbb59; padding-bottom: 0in; border-left: #ece9d8; width: 49.5pt; padding-top: 0in; border-bottom: #ece9d8; mso-background-themecolor: accent3;" width="66" valign="top">
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><strong><span style="color: white; mso-bidi-font-family: 'Times New Roman'; mso-fareast-font-family: Calibri; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-theme-font: minor-latin; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-bidi-theme-font: minor-bidi; mso-themecolor: background1;"><span style="font-size: small;"><span style="font-family: Calibri;">UserID</span></span></span></strong></p>
</td>
<td style="border-right: #9bbb59 1pt solid; padding-right: 5.4pt; border-top: #9bbb59 1pt solid; padding-left: 5.4pt; background: #9bbb59; padding-bottom: 0in; border-left: #ece9d8; width: 58.5pt; padding-top: 0in; border-bottom: #ece9d8; mso-background-themecolor: accent3;" width="78" valign="top">
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><strong><span style="color: white; mso-bidi-font-family: 'Times New Roman'; mso-fareast-font-family: Calibri; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-theme-font: minor-latin; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-bidi-theme-font: minor-bidi; mso-themecolor: background1;"><span style="font-size: small;"><span style="font-family: Calibri;">UserSex</span></span></span></strong></p>
</td>
</tr>
<tr style="mso-yfti-irow: 1;">
<td style="border-right: #ece9d8; padding-right: 5.4pt; border-top: #9bbb59 1pt solid; padding-left: 5.4pt; padding-bottom: 0in; border-left: #9bbb59 1pt solid; width: 0.95in; padding-top: 0in; border-bottom: #9bbb59 1pt solid; background-color: transparent; mso-border-themecolor: accent3;" width="91" valign="top">
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><strong><span style="mso-bidi-font-family: 'Times New Roman'; mso-fareast-font-family: Calibri; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-theme-font: minor-latin; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-bidi-theme-font: minor-bidi;"><span style="font-size: small;"><span style="font-family: Calibri;">Joe</span></span></span></strong></p>
</td>
<td style="border-right: #ece9d8; padding-right: 5.4pt; border-top: #9bbb59 1pt solid; padding-left: 5.4pt; padding-bottom: 0in; border-left: #ece9d8; width: 49.5pt; padding-top: 0in; border-bottom: #9bbb59 1pt solid; background-color: transparent; mso-border-top-themecolor: accent3; mso-border-bottom-themecolor: accent3;" width="66" valign="top">
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="mso-bidi-font-family: 'Times New Roman'; mso-fareast-font-family: Calibri; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-theme-font: minor-latin; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-bidi-theme-font: minor-bidi;"><span style="font-size: small;"><span style="font-family: Calibri;">1234</span></span></span></p>
</td>
<td style="border-right: #9bbb59 1pt solid; padding-right: 5.4pt; border-top: #9bbb59 1pt solid; padding-left: 5.4pt; padding-bottom: 0in; border-left: #ece9d8; width: 58.5pt; padding-top: 0in; border-bottom: #9bbb59 1pt solid; background-color: transparent; mso-border-themecolor: accent3;" width="78" valign="top">
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="mso-bidi-font-family: 'Times New Roman'; mso-fareast-font-family: Calibri; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-theme-font: minor-latin; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-bidi-theme-font: minor-bidi;"><span style="font-size: small;"><span style="font-family: Calibri;">Male</span></span></span></p>
</td>
</tr>
<tr style="mso-yfti-irow: 2; mso-yfti-lastrow: yes;">
<td style="border-right: #ece9d8; padding-right: 5.4pt; border-top: #ece9d8; padding-left: 5.4pt; padding-bottom: 0in; border-left: #9bbb59 1pt solid; width: 0.95in; padding-top: 0in; border-bottom: #9bbb59 1pt solid; background-color: transparent;" width="91" valign="top">
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><strong><span style="mso-bidi-font-family: 'Times New Roman'; mso-fareast-font-family: Calibri; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-theme-font: minor-latin; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-bidi-theme-font: minor-bidi;"><span style="font-size: small;"><span style="font-family: Calibri;">Jane</span></span></span></strong></p>
</td>
<td style="border-right: #ece9d8; padding-right: 5.4pt; border-top: #ece9d8; padding-left: 5.4pt; padding-bottom: 0in; border-left: #ece9d8; width: 49.5pt; padding-top: 0in; border-bottom: #9bbb59 1pt solid; background-color: transparent;" width="66" valign="top">
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="mso-bidi-font-family: 'Times New Roman'; mso-fareast-font-family: Calibri; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-theme-font: minor-latin; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-bidi-theme-font: minor-bidi;"><span style="font-size: small;"><span style="font-family: Calibri;">5678</span></span></span></p>
</td>
<td style="border-right: #9bbb59 1pt solid; padding-right: 5.4pt; border-top: #ece9d8; padding-left: 5.4pt; padding-bottom: 0in; border-left: #ece9d8; width: 58.5pt; padding-top: 0in; border-bottom: #9bbb59 1pt solid; background-color: transparent;" width="78" valign="top">
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><span style="mso-bidi-font-family: 'Times New Roman'; mso-fareast-font-family: Calibri; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-theme-font: minor-latin; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-bidi-theme-font: minor-bidi;"><span style="font-size: small;"><span style="font-family: Calibri;">Female</span></span></span></p>
</td>
</tr>
</tbody>
</table>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Calibri;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Calibri;">When an error occurred, the stored procedure would output a table with no records:</span></p>
<table class="MsoNormalTable" style="border-collapse: collapse; mso-border-alt: solid #9BBB59 1.0pt; mso-yfti-tbllook: 1184; mso-padding-alt: 0in 5.4pt 0in 5.4pt;" border="1" cellspacing="0" cellpadding="0">
<tbody>
<tr style="mso-yfti-irow: 0; mso-yfti-firstrow: yes; mso-yfti-lastrow: yes;">
<td style="border-right: #ece9d8; padding-right: 5.4pt; border-top: #9bbb59 1pt solid; padding-left: 5.4pt; background: #9bbb59; padding-bottom: 0in; border-left: #9bbb59 1pt solid; width: 0.95in; padding-top: 0in; border-bottom: #9bbb59 1pt solid; mso-background-themecolor: accent3;" width="91" valign="top">
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><strong><span style="color: white; mso-bidi-font-family: 'Times New Roman'; mso-fareast-font-family: Calibri; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-theme-font: minor-latin; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-bidi-theme-font: minor-bidi; mso-themecolor: background1;"><span style="font-size: small;"><span style="font-family: Calibri;">UserName</span></span></span></strong></p>
</td>
<td style="border-right: #ece9d8; padding-right: 5.4pt; border-top: #9bbb59 1pt solid; padding-left: 5.4pt; background: #9bbb59; padding-bottom: 0in; border-left: #ece9d8; width: 49.5pt; padding-top: 0in; border-bottom: #9bbb59 1pt solid; mso-background-themecolor: accent3;" width="66" valign="top">
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><strong><span style="color: white; mso-bidi-font-family: 'Times New Roman'; mso-fareast-font-family: Calibri; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-theme-font: minor-latin; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-bidi-theme-font: minor-bidi; mso-themecolor: background1;"><span style="font-size: small;"><span style="font-family: Calibri;">UserID</span></span></span></strong></p>
</td>
<td style="border-right: #9bbb59 1pt solid; padding-right: 5.4pt; border-top: #9bbb59 1pt solid; padding-left: 5.4pt; background: #9bbb59; padding-bottom: 0in; border-left: #ece9d8; width: 58.5pt; padding-top: 0in; border-bottom: #9bbb59 1pt solid; mso-background-themecolor: accent3;" width="78" valign="top">
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal;"><strong><span style="color: white; mso-bidi-font-family: 'Times New Roman'; mso-fareast-font-family: Calibri; mso-ascii-font-family: Calibri; mso-ascii-theme-font: minor-latin; mso-fareast-theme-font: minor-latin; mso-hansi-font-family: Calibri; mso-hansi-theme-font: minor-latin; mso-bidi-theme-font: minor-bidi; mso-themecolor: background1;"><span style="font-size: small;"><span style="font-family: Calibri;">UserSex</span></span></span></strong></p>
</td>
</tr>
</tbody>
</table>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Calibri;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Calibri;">So, what is an easy way to get this empty set?<span style="mso-spacerun: yes;">  </span>The answer is a simple Where clause that will never be true (click here to see a case when 1=2 <a href="http://www.techiebreak.com/uncategorized/algebraic-proof-that-12/">http://www.techiebreak.com/uncategorized/algebraic-proof-that-12/</a></span><span style="font-size: small; font-family: Calibri;">) :</span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"> </p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"> </p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: 10pt; color: blue; line-height: 115%; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">SELECT</span><span style="font-size: 10pt; line-height: 115%; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> <span style="color: gray;">NULL</span> <span style="color: blue;">AS</span> UserName<span style="color: gray;">,</span> <span style="color: gray;">NULL</span> <span style="color: blue;">AS</span> UserID<span style="color: gray;">,</span> <span style="color: gray;">NULL</span> <span style="color: blue;">AS</span> UserSex <span style="color: blue;">WHERE</span> 1<span style="color: gray;">=</span>2</span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: 10pt; line-height: 115%; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: 10pt; line-height: 115%; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">And that’s it!<span style="mso-spacerun: yes;">  </span>Note that the fact that I used “NULL” for a value is not important because the value will never acutally show up.</span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.dbtechie.com/sql-server/how-to-return-an-empty-set-with-named-columns/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Easy Random Text Generator Using Selective Characters</title>
		<link>http://www.dbtechie.com/sql-server/user-defined-functions-udf/easy-random-text-generator-using-selective-characters/</link>
		<comments>http://www.dbtechie.com/sql-server/user-defined-functions-udf/easy-random-text-generator-using-selective-characters/#comments</comments>
		<pubDate>Thu, 02 Jul 2009 00:14:01 +0000</pubDate>
		<dc:creator>Eric North</dc:creator>
		
		<category><![CDATA[User-Defined Functions (UDF)]]></category>

		<category><![CDATA[Rand]]></category>

		<category><![CDATA[SQL Server]]></category>

		<category><![CDATA[substring]]></category>

		<category><![CDATA[UDF]]></category>

		<category><![CDATA[User-defined function]]></category>

		<guid isPermaLink="false">http://www.dbtechie.com/?p=19</guid>
		<description><![CDATA[



I needed to create a user-defined function (UDF) in SQL Server that returned a random password.  My initial idea was to generate a random integer between some ASCII value ‘x’ and ASCII value ‘y,’ then concatenate several of these together and voila.  Upon looking at the ASCII chart, I realized that there was no acceptable [...]]]></description>
			<content:encoded><![CDATA[<p><div style="float: left;margin-right:5px;"><script type="text/javascript"><!--
google_ad_client = "pub-4622190097186006";
/* 250x250, created 7/6/09 */
google_ad_slot = "0934256002";
google_ad_width = 250;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small;"><span style="font-family: Calibri;">I needed to create a user-defined function (UDF) in SQL Server that returned a random password.<span style="mso-spacerun: yes;">  </span>My initial idea was to generate a random integer between some ASCII value ‘x’ and ASCII value ‘y,’ then concatenate several of these together and voila.<span style="mso-spacerun: yes;">  </span>Upon looking at the ASCII chart, I realized that there was no acceptable “block” of values that I wanted to use.<span style="mso-spacerun: yes;">  </span>I did not want to use punctuation marks or certain characters that are easily confused, such as: 0,o,O,l,1,i,I, etc.<span style="mso-spacerun: yes;">   </span>Additionally, to prevent an obscene word from being randomly created, I wanted to eliminate all vowels.<span style="mso-spacerun: yes;">  </span>So, my next thought was to create a look-up, cross reference table where a random ID value would correspond to a character in the table; however, this seemed like quite a bit of overhead for a simple user-defined function.<span style="mso-spacerun: yes;">  </span>Finally, I decided to use a single text variable to hold all of the desired characters.<span style="mso-spacerun: yes;">  </span>Then, I pulled random characters out of the variable base on the characters’ relative position inside the string of text.<span style="mso-spacerun: yes;">  </span></span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Calibri;">The T-SQL is as follows:</span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Calibri;">Note that the following UDF (fn_GetRandomPassword) uses the function MyRand() previously discussed here: </span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><a href="http://www.dbtechie.com/sql-server/user-defined-functions-udf/random-thoughts-on-sql-server/"><span style="font-size: small; color: #800080; font-family: Calibri;">http://www.dbtechie.com/sql-server/user-defined-functions-udf/random-thoughts-on-sql-server/</span></a></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Calibri;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; color: green; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">&#8211; =============================================</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; color: green; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">&#8211; Description:<span style="mso-tab-count: 1;">      </span>Create a random password of PasswordLength</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; color: green; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">&#8211;<span style="mso-spacerun: yes;">    </span>comprised of lowercase letters, not including [a,e,i,j,l,o,u] and</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; color: green; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">&#8211;<span style="mso-spacerun: yes;">    </span>including the numbers 2 through 8.<span style="mso-spacerun: yes;">  </span>Maximum length = 100 chars.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; color: green; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">&#8211; =============================================</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">CREATE</span><span style="font-size: 8pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> <span style="color: blue;">FUNCTION</span> [dbo]<span style="color: gray;">.</span>[fn_GetRandomPassword]</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; color: gray; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">(</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-tab-count: 1;">       </span>@PasswordLength <span style="color: blue;">INT</span> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; color: gray; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">)</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">RETURNS</span><span style="font-size: 8pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> <span style="color: blue;">varchar</span><span style="color: gray;">(</span>100<span style="color: gray;">)</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">AS</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">BEGIN</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-tab-count: 1;">       </span><span style="color: blue;">DECLARE</span> @retVal <span style="color: blue;">VARCHAR</span><span style="color: gray;">(</span>100<span style="color: gray;">)</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-tab-count: 1;">       </span><span style="color: blue;">DECLARE</span> @charList <span style="color: blue;">VARCHAR</span><span style="color: gray;">(</span>26<span style="color: gray;">)</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-tab-count: 1;">       </span><span style="color: blue;">DECLARE</span> @rnd <span style="color: blue;">FLOAT</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-tab-count: 1;">       </span><span style="color: blue;">DECLARE</span> @loopCounter <span style="color: blue;">INT</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-tab-count: 1;">       </span><span style="color: blue;">DECLARE</span> @randID <span style="color: blue;">INT</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-tab-count: 1;">       </span><span style="color: blue;">DECLARE</span> @oneChar <span style="color: blue;">CHAR</span><span style="color: gray;">(</span>1<span style="color: gray;">)</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; color: gray; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; color: gray; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-tab-count: 1;">       </span><span style="color: green;">&#8211; build the charList with chars a-z</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-tab-count: 1;">       </span><span style="color: blue;">SET</span> @charList <span style="color: gray;">=</span> <span style="color: red;">&#8221;</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-tab-count: 1;">       </span><span style="color: blue;">SET</span> @loopCounter<span style="color: gray;">=</span>97</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-tab-count: 1;">       </span><span style="color: blue;">WHILE</span> @loopCounter<span style="color: gray;">&lt;</span>123</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-tab-count: 1;">       </span><span style="color: blue;">BEGIN</span> <span style="color: green;">&#8211; while </span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-tab-count: 2;">              </span><span style="color: blue;">SET</span> @charList <span style="color: gray;">=</span> @charList <span style="color: gray;">+</span> <span style="color: blue;">CHAR</span><span style="color: gray;">(</span>@loopCounter<span style="color: gray;">)</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-tab-count: 2;">              </span><span style="color: blue;">SET</span> @loopCounter <span style="color: gray;">=</span> @loopCounter <span style="color: gray;">+</span> 1</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-tab-count: 1;">       </span><span style="color: blue;">END</span> <span style="color: green;">&#8211;while </span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; color: green; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; color: green; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-tab-count: 1;">       </span><span style="color: green;">&#8211;replace unwanted characters with numbers 2-8</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; color: green; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-tab-count: 1;">       </span><span style="color: green;">&#8211;replace a</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-tab-count: 1;">       </span><span style="color: blue;">SET</span> @charList <span style="color: gray;">=</span> <span style="color: fuchsia;">REPLACE</span><span style="color: gray;">(</span>@charList<span style="color: gray;">,</span><span style="color: red;">&#8216;a&#8217;</span><span style="color: gray;">,</span><span style="color: red;">&#8216;2&#8242;</span><span style="color: gray;">)</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; color: gray; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-tab-count: 1;">       </span><span style="color: green;">&#8211;replace e</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-tab-count: 1;">       </span><span style="color: blue;">SET</span> @charList <span style="color: gray;">=</span> <span style="color: fuchsia;">REPLACE</span><span style="color: gray;">(</span>@charList<span style="color: gray;">,</span><span style="color: red;">&#8216;e&#8217;</span><span style="color: gray;">,</span><span style="color: red;">&#8216;3&#8242;</span><span style="color: gray;">)</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; color: gray; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-tab-count: 1;">       </span><span style="color: green;">&#8211;replace i</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-tab-count: 1;">       </span><span style="color: blue;">SET</span> @charList <span style="color: gray;">=</span> <span style="color: fuchsia;">REPLACE</span><span style="color: gray;">(</span>@charList<span style="color: gray;">,</span><span style="color: red;">&#8216;i&#8217;</span><span style="color: gray;">,</span><span style="color: red;">&#8216;4&#8242;</span><span style="color: gray;">)</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; color: gray; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-tab-count: 1;">       </span><span style="color: green;">&#8211;replace j</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-tab-count: 1;">       </span><span style="color: blue;">SET</span> @charList <span style="color: gray;">=</span> <span style="color: fuchsia;">REPLACE</span><span style="color: gray;">(</span>@charList<span style="color: gray;">,</span><span style="color: red;">&#8216;j&#8217;</span><span style="color: gray;">,</span><span style="color: red;">&#8216;5&#8242;</span><span style="color: gray;">)</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; color: gray; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-tab-count: 1;">       </span><span style="color: green;">&#8211;replace l</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-tab-count: 1;">       </span><span style="color: blue;">SET</span> @charList <span style="color: gray;">=</span> <span style="color: fuchsia;">REPLACE</span><span style="color: gray;">(</span>@charList<span style="color: gray;">,</span><span style="color: red;">&#8216;l&#8217;</span><span style="color: gray;">,</span><span style="color: red;">&#8216;6&#8242;</span><span style="color: gray;">)</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; color: gray; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-tab-count: 1;">       </span><span style="color: green;">&#8211;replace o</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-tab-count: 1;">       </span><span style="color: blue;">SET</span> @charList <span style="color: gray;">=</span> <span style="color: fuchsia;">REPLACE</span><span style="color: gray;">(</span>@charList<span style="color: gray;">,</span><span style="color: red;">&#8216;o&#8217;</span><span style="color: gray;">,</span><span style="color: red;">&#8216;7&#8242;</span><span style="color: gray;">)</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; color: gray; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-tab-count: 1;">       </span><span style="color: green;">&#8211;replace u</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-tab-count: 1;">       </span><span style="color: blue;">SET</span> @charList <span style="color: gray;">=</span> <span style="color: fuchsia;">REPLACE</span><span style="color: gray;">(</span>@charList<span style="color: gray;">,</span><span style="color: red;">&#8216;u&#8217;</span><span style="color: gray;">,</span><span style="color: red;">&#8216;8&#8242;</span><span style="color: gray;">)</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-tab-count: 1;">       </span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-tab-count: 1;">       </span><span style="color: green;">&#8211; force passwordLength to be a legitimate value </span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-tab-count: 1;">       </span><span style="color: blue;">IF</span> @PasswordLength <span style="color: gray;">&gt;</span>100</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-tab-count: 2;">              </span><span style="color: blue;">SET</span> @PasswordLength<span style="color: gray;">=</span>100</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-tab-count: 1;">       </span><span style="color: blue;">IF</span> @PasswordLength <span style="color: gray;">&lt;</span>1 </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-tab-count: 2;">              </span><span style="color: blue;">SET</span> @PasswordLength <span style="color: gray;">=</span>1</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-tab-count: 1;">       </span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-tab-count: 1;">       </span><span style="color: green;">&#8211; build random password</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-tab-count: 1;">       </span><span style="color: blue;">SET</span> @loopCounter<span style="color: gray;">=</span>0</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-tab-count: 1;">       </span><span style="color: blue;">SET</span> @retVal <span style="color: gray;">=</span> <span style="color: red;">&#8221;</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; color: red; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-tab-count: 1;">       </span><span style="color: blue;">WHILE</span> @loopCounter <span style="color: gray;">&lt;</span> @PasswordLength</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-tab-count: 1;">       </span><span style="color: blue;">BEGIN</span> <span style="color: green;">&#8211; while</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; color: green; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-tab-count: 2;">              </span><span style="color: blue;">SET</span> @rnd <span style="color: gray;">=</span> <span style="color: gray;">(</span><span style="color: blue;">Select</span> dbo<span style="color: gray;">.</span>MyRand<span style="color: gray;">())</span><span style="mso-spacerun: yes;">  </span><span style="color: green;">&#8211;Get random float from 0-1</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; color: green; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-tab-count: 2;">              </span><span style="color: blue;">SET</span> @randID <span style="color: gray;">=</span> <span style="color: fuchsia;">ROUND</span><span style="color: gray;">(</span>25 <span style="color: gray;">*</span> @rnd<span style="color: gray;">,</span>0<span style="color: gray;">)</span> <span style="color: gray;">+</span> 1<span style="mso-spacerun: yes;">   </span><span style="color: green;">&#8211;Get random int from 1 to 26</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-tab-count: 2;">              </span><span style="color: blue;">SET</span> @oneChar <span style="color: gray;">=</span> <span style="color: fuchsia;">SUBSTRING</span><span style="color: gray;">(</span>@charList<span style="color: gray;">,</span>@randID<span style="color: gray;">,</span>1<span style="color: gray;">)</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-tab-count: 2;">              </span><span style="color: blue;">SET</span> @retVal <span style="color: gray;">=</span> @retVal <span style="color: gray;">+</span> @oneChar</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-tab-count: 2;">              </span><span style="color: blue;">SET</span> @loopCounter <span style="color: gray;">=</span> @loopCounter <span style="color: gray;">+</span> 1</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-tab-count: 1;">       </span><span style="color: blue;">END</span> <span style="color: green;">&#8211; while</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; color: green; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-tab-count: 1;">       </span><span style="color: blue;">RETURN</span> @retVal</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 8pt; color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">END</span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.dbtechie.com/sql-server/user-defined-functions-udf/easy-random-text-generator-using-selective-characters/feed/</wfw:commentRss>
		</item>
		<item>
		<title>How to Reference Multiple Databases on the Same Server</title>
		<link>http://www.dbtechie.com/sql-server/how-to-reference-multiple-databases-on-the-same-server/</link>
		<comments>http://www.dbtechie.com/sql-server/how-to-reference-multiple-databases-on-the-same-server/#comments</comments>
		<pubDate>Thu, 18 Jun 2009 22:50:08 +0000</pubDate>
		<dc:creator>Eric North</dc:creator>
		
		<category><![CDATA[SQL Server]]></category>

		<guid isPermaLink="false">http://www.dbtechie.com/?p=15</guid>
		<description><![CDATA[



Creating a SELECT statement that references multiple databases on the same server in SQL Server is simple, but the syntax is not obvious and not well documented.  Without the proper syntax, you will get the SQL message:  “Invalid object name.”  The correct syntax is as follows:
USE Database1
GO
 
SELECT     * 
FROM       Database2.dbo.TableName
The only real trick is remembering [...]]]></description>
			<content:encoded><![CDATA[<p><div style="float: left;margin-right:5px;"><script type="text/javascript"><!--
google_ad_client = "pub-4622190097186006";
/* 250x250, created 7/6/09 */
google_ad_slot = "0934256002";
google_ad_width = 250;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Calibri;">Creating a SELECT statement that references multiple databases on the same server in SQL Server is simple, but the syntax is not obvious and not well documented.<span style="mso-spacerun: yes;">  </span>Without the proper syntax, you will get the SQL message: <span style="mso-spacerun: yes;"> </span>“Invalid object name.”<span style="mso-spacerun: yes;">  </span>The correct syntax is as follows:</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">USE</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> Database1</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">GO</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">SELECT</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">     </span><span style="color: gray;">*</span> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: 10pt; color: blue; line-height: 115%; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">FROM</span><span style="font-size: 10pt; line-height: 115%; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">       </span>Database2<span style="color: gray;">.</span>dbo<span style="color: gray;">.</span>TableName</span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="mso-no-proof: yes; mso-bidi-font-family: 'Courier New';"><span style="font-size: small;"><span style="font-family: Calibri;">The only real trick is remembering to include the “dbo” in the correct spot.<span style="mso-spacerun: yes;">  </span>You will, of course, have to have permission to both databases.</span></span></span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.dbtechie.com/sql-server/how-to-reference-multiple-databases-on-the-same-server/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Random Thoughts on SQL Server</title>
		<link>http://www.dbtechie.com/sql-server/user-defined-functions-udf/random-thoughts-on-sql-server/</link>
		<comments>http://www.dbtechie.com/sql-server/user-defined-functions-udf/random-thoughts-on-sql-server/#comments</comments>
		<pubDate>Thu, 18 Jun 2009 01:12:52 +0000</pubDate>
		<dc:creator>Eric North</dc:creator>
		
		<category><![CDATA[User-Defined Functions (UDF)]]></category>

		<category><![CDATA[Rand]]></category>

		<category><![CDATA[random]]></category>

		<category><![CDATA[SQL Server]]></category>

		<category><![CDATA[UDF]]></category>

		<category><![CDATA[User-defined function]]></category>

		<guid isPermaLink="false">http://www.dbtechie.com/?p=4</guid>
		<description><![CDATA[



Recently, I created a user-defined SQL Server function to return random characters to be used as an initial password when signing up for an account.   Without knowing a few things about the RAND() function in advance, this fifteen-minute project could take you several hours.   The actual code for creating the random password I will discuss [...]]]></description>
			<content:encoded><![CDATA[<p><div style="float: left;margin-right:5px;"><script type="text/javascript"><!--
google_ad_client = "pub-4622190097186006";
/* 250x250, created 7/6/09 */
google_ad_slot = "0934256002";
google_ad_width = 250;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></div>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Calibri;">Recently, I created a user-defined SQL Server function to return random characters to be used as an initial password when signing up for an account.<span style="mso-spacerun: yes;">   </span>Without knowing a few things about the RAND() function in advance, this fifteen-minute project could take you several hours. <span style="mso-spacerun: yes;">  </span>The actual code for creating the random password I will discuss in a future article (<a href="http://www.dbtechie.com/sql-server/user-defined-functions-udf/easy-random-text-generator-using-selective-characters/"><span style="color: #800080;">http://www.dbtechie.com/sql-server/user-defined-functions-udf/easy-random-text-generator-using-selective-characters/</span></a>)  <span style="mso-spacerun: yes;">  </span>In this article, I will discuss a few of the pitfalls of the RAND() function.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt; tab-stops: 91.5pt;"><span style="mso-tab-count: 1;"><span style="font-size: small; font-family: Calibri;">                                         </span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><strong style="mso-bidi-font-weight: normal;"><span style="font-size: 12pt; line-height: 115%;"><span style="font-family: Calibri;">Why can’t I use RAND() in a user-defined function and how do I get around it? </span></span></strong></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Calibri;">The first thing to know is that the RAND() function (which returns a float between 0 and 1) is one of the nondeterministic built-in functions that cannot be called from a user-defined function (UDF).<span style="mso-spacerun: yes;">  </span>Per msdn (</span><a href="http://msdn.microsoft.com/en-us/library/ms178091(SQL.90).aspx"><span style="font-size: small; color: #800080; font-family: Calibri;">http://msdn.microsoft.com/en-us/library/ms178091(SQL.90).aspx</span></a><span style="font-size: small;"><span style="font-family: Calibri;">), “Deterministic functions always return the same result any time they are called with a specific set of input values and given the same state of the database. Nondeterministic functions may return different results each time they are called with a specific set of input values even if the database state that they access remains the same.”<span style="mso-spacerun: yes;">  </span>For a list of nondeterministic functions that can and cannot be used in UDF, see this msdn link:<span style="mso-spacerun: yes;">  </span></span></span><a href="http://msdn.microsoft.com/en-us/library/ms191320(SQL.90).aspx"><span style="font-size: small; color: #800080; font-family: Calibri;">http://msdn.microsoft.com/en-us/library/ms191320(SQL.90).aspx</span></a><span style="font-size: small; font-family: Calibri;">.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Calibri;">If you tried using RAND() in your user-defined function, you probably saw the message, “Invalid use of side-effecting or time-dependent operator in &#8216;rand&#8217; within a function.”<span style="mso-spacerun: yes;">   </span>In other words, RAND() is one of the nondeterministic functions that SQL won’t let you use in a UDF.<span style="mso-spacerun: yes;">  </span>The solution is simple.<span style="mso-spacerun: yes;">  </span>Put the RAND() function in a view and refer to the view in the UDF.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">CREATE</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> <span style="color: blue;">VIEW</span> [dbo]<span style="color: gray;">.</span>[vw_RandomFloat]</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">AS</span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: 10pt; color: blue; line-height: 115%; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">SELECT</span><span style="font-size: 10pt; line-height: 115%; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-spacerun: yes;">     </span><span style="color: fuchsia;">RAND</span><span style="color: gray;">()</span> <span style="color: blue;">AS</span> RandValue</span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: 10pt; line-height: 115%; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">&#8212;&#8211;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">CREATE</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> <span style="color: blue;">FUNCTION</span> MyRand<span style="color: gray;">()</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">RETURNS</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> <span style="color: blue;">FLOAT</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">AS</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">BEGIN</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"><span style="mso-tab-count: 1;">      </span><span style="color: blue;">RETURN</span> <span style="color: gray;">(</span><span style="color: blue;">select</span> RandValue <span style="color: blue;">from</span> dbo<span style="color: gray;">.</span>vw_RandomFloat<span style="color: gray;">)</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: 10pt; color: blue; line-height: 115%; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">END</span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: 10pt; color: blue; line-height: 115%; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">&#8212;&#8211;</span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: 10pt; color: blue; line-height: 115%; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">SELECT</span><span style="font-size: 10pt; line-height: 115%; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> dbo<span style="color: gray;">.</span>MyRand<span style="color: gray;">()</span></span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Calibri;"> </span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><strong style="mso-bidi-font-weight: normal;"><span style="font-size: 12pt; line-height: 115%;"><span style="font-family: Calibri;">Why aren’t my values random? </span></span></strong></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Calibri;">Using RAND() without parameters<span style="mso-spacerun: yes;">  </span>works just fine if you only plan on getting one random value per transaction.<span style="mso-spacerun: yes;">  </span>RAND() is considered a pseudo-random function, because it depends on a seed value to determine where to start returning a predetermined sequence of values.<span style="mso-spacerun: yes;">   </span>The same seed value will always return the same sequence of values.<span style="mso-spacerun: yes;">  </span>RAND() is “seeded” with an integer to randomize the values returned.<span style="mso-spacerun: yes;">  </span>The classic way to randomize is by seeding using a function from the internal clock of the computer, which is how the RAND() function will seed if you do not specify a seed value.<span style="mso-spacerun: yes;">   </span>The problem is that if you include the RAND() function in a SELECT statement that returns multiple rows, the same seed value is used for every row in the result set of the SELECT statement.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Calibri;">We now find ourselves with the paradoxical problem of needing to find a way to create a random integer to seed the random function.<span style="mso-spacerun: yes;">  </span>At first, you may think to use an identity column as a seed, but you will quickly be discouraged with this method because seed values that are close in value also return values that are close in value.<span style="mso-spacerun: yes;">  </span>For example, RAND(1) returns 0.713591993212924 and RAND(2) returns 0.713610626184182.<span style="mso-spacerun: yes;">  </span>These values are close enough that, for all intents and purposes , they might as well be the same number.</span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Calibri;">The best solution I have seen is to seed using the NEWID() function.<span style="mso-spacerun: yes;">  </span>NEWID() returns a uniqueidentifier, so there is still a tiny bit of work left to convert it to an integer to seed the RAND() function.<span style="mso-spacerun: yes;">   </span>The easiest is by using the CHECKSUM() function: Checksum(NEWID()).<span style="mso-spacerun: yes;">  </span>You could also use the CAST() function: CAST(CAST(NEWID() AS VARBINARY) AS INT).</span></p>
<p class="MsoNormal" style="margin: 0in 0in 10pt;"><span style="font-size: small; font-family: Calibri;">So, the new and improved random view would be:</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">CREATE</span><span style="font-size: 10pt; font-family: &quot;Courier New&quot;; mso-no-proof: yes;"> <span style="color: blue;">VIEW</span> [dbo]<span style="color: gray;">.</span>[vw_RandomFloat]</span></p>
<p class="MsoNormal" style="margin: 0in 0in 0pt; line-height: normal; mso-layout-grid-align: none;"><span style="font-size: 10pt; color: blue; font-family: &quot;Courier New&quot;; mso-no-proof: yes;">AS</span></p>
<p><span style="font-size: 10pt; color: blue; line-height: 115%; font-family: &quot;Courier New&quot;; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA;">SELECT</span><span style="font-size: 10pt; line-height: 115%; font-family: &quot;Courier New&quot;; mso-fareast-font-family: Calibri; mso-no-proof: yes; mso-ansi-language: EN-US; mso-fareast-language: EN-US; mso-bidi-language: AR-SA;"><span style="mso-spacerun: yes;">     </span><span style="color: fuchsia;">RAND</span><span style="color: gray;">(</span><span style="color: fuchsia;">Checksum</span><span style="color: gray;">(</span><span style="color: fuchsia;">NEWID</span><span style="color: gray;">())) </span><span style="color: blue;">AS</span> RandValue</span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.dbtechie.com/sql-server/user-defined-functions-udf/random-thoughts-on-sql-server/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
