<?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:series="http://unfoldingneurons.com/"
	>

<channel>
	<title>Linglom&#039;s blog &#187; MS SQL Server</title>
	<atom:link href="http://www.linglom.com/category/windows/ms-sql-server/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.linglom.com</link>
	<description>Just another IT weblog</description>
	<lastBuildDate>Thu, 27 May 2010 09:56:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>[Solved] ActiveX component can&#8217;t create object: &#8216;SQLDMO.SQLServer&#8217; on SQL Server 2008</title>
		<link>http://www.linglom.com/2010/01/13/solved-activex-component-cant-create-object-sqldmo-sqlserver-on-sql-server-2008/</link>
		<comments>http://www.linglom.com/2010/01/13/solved-activex-component-cant-create-object-sqldmo-sqlserver-on-sql-server-2008/#comments</comments>
		<pubDate>Wed, 13 Jan 2010 04:22:38 +0000</pubDate>
		<dc:creator>linglom</dc:creator>
				<category><![CDATA[MS SQL Server]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[SQL Server 2008]]></category>
		<category><![CDATA[SQL-DMO]]></category>

		<guid isPermaLink="false">http://www.linglom.com/?p=1129</guid>
		<description><![CDATA[Problem If you execute a vb script which use SQL-DMO on SQL Server 2008, you will notice that the script doesn&#8217;t work at all. You will see the error message as similar to this one when you execute the vb script: Error: ActiveX component can&#8217;t create object: &#8216;SQLDMO.SQLServer&#8217; Code: 800A01AD Source: Microsoft VBScript runtime error [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<div class="KonaBody"><h3>Problem</h3>
<p>If you execute a vb script which use SQL-DMO on SQL Server 2008, you will notice that the script doesn&#8217;t work at all. </p>
<p><span id="more-1129"></span></p>
<p>You will see the error message as similar to this one when you execute the vb script:</p>
<blockquote><p>Error: ActiveX component can&#8217;t create object: &#8216;SQLDMO.SQLServer&#8217;<br />
Code: 800A01AD<br />
Source: Microsoft VBScript runtime error</p></blockquote>
<p><a href="http://www.linglom.com/images/Windows/SQL-Server/SQLDMO.SQLServer-on-SQL-2008/1.png" target="_blank"><img src="http://www.linglom.com/images/Windows/SQL-Server/SQLDMO.SQLServer-on-SQL-2008/_1.png" width="250" height="97" alt="ActiveX component can't create object: 'SQLDMO.SQLServer'" title="ActiveX component can't create object: 'SQLDMO.SQLServer'"  /></a></p>
<h3>Cause</h3>
<p>This is because SQLDMO is deprecated since SQL Server 2005. But SQLDMO is installed on SQL Server 2005 by default so you can run a vb script which uses SQLDMO without any error on SQL Server 2005. On SQL Server 2008, SQLDMO is not installed by default . If you want to use SQLDMO, you have to install it manually.</p>
<p><!-- Start AdLogger Wrapping Code -->
<?php @include_once("/usr/local/psa/home/vhosts/linglom.com/httpdocs/adlogger/ad_check_include.php"); if ($show_ads == 'y') { ?>

<script type="text/javascript"><!--
google_ad_client = "pub-7765165459812980";
google_ad_width = 300;
google_ad_height = 250;
google_ad_format = "300x250_as";
google_ad_type = "text_image";
//2007-02-17: SquareonBlog
google_ad_channel = "6055254908";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "ce6531";
google_color_text = "000000";
google_color_url = "008000";
//--></script>
<script type="text/javascript"
  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>

<?php } ?>
<!-- End AdLogger Wrapping Code --></p>
<h3>Solution</h3>
<p>To use SQL-DMO on SQL Server 2008, you need to download and install <strong>Microsoft SQL Server 2005 Backward Compatibility Components</strong> to solve the problem. You can go to <a href="http://www.microsoft.com/downloads" target="_blank" rel="nofollow">Microsoft Download Center</a> and search for &#8220;Microsoft SQL Server 2008 Feature Pack&#8221; and sort by Release Date to find the latest version. Or you can download at <a href="http://forum.linglom.com/index.php?topic=692" target="_blank">Linglom&#8217;s Download Page</a>.</p>
<h3>Step-by-step</h3>
<ol>
<li>Download <strong>Microsoft SQL Server 2005 Backward Compatibility Components</strong>. Next, execute the setup file.</li>
<li>On <strong>Welcome to the Install Wizard for Microsoft SQL Server 2005 Backward compatibility Setup</strong>, click <strong>Next</strong>.<br />
<a href="http://www.linglom.com/images/Windows/SQL-Server/SQLDMO.SQLServer-on-SQL-2008/2.png" target="_blank"><img src="http://www.linglom.com/images/Windows/SQL-Server/SQLDMO.SQLServer-on-SQL-2008/_2.png" width="250" height="188" alt="Microsoft SQL Server 2005 Backward compatibility Setup" title="Microsoft SQL Server 2005 Backward compatibility Setup"  /></a></li>
<li>On <strong>License Agreement</strong>, select &#8216;<strong>I accept the terms in the license agreement</strong>&#8216;. Then, click <strong>Next</strong>.<br />
<a href="http://www.linglom.com/images/Windows/SQL-Server/SQLDMO.SQLServer-on-SQL-2008/3.png" target="_blank"><img src="http://www.linglom.com/images/Windows/SQL-Server/SQLDMO.SQLServer-on-SQL-2008/_3.png" width="250" height="188" alt="License Agreement" title="License Agreement"  /></a></li>
<li>On <strong>Registration Information</strong>, enter your personal information. Then, click <strong>Next</strong>.<br />
<a href="http://www.linglom.com/images/Windows/SQL-Server/SQLDMO.SQLServer-on-SQL-2008/4.png" target="_blank"><img src="http://www.linglom.com/images/Windows/SQL-Server/SQLDMO.SQLServer-on-SQL-2008/_4.png" width="250" height="188" alt="Registration Information" title="Registration Information"  /></a></li>
<li>On <strong>Feature Selection</strong>, ensure that <strong>SQL Distributed Management Objects</strong> is selected. Then, click <strong>Next</strong>.<br />
<em><strong>Note:</strong></em> SQL-DMO is the feature that is require to run a vb script that uses DMO.<br />
<a href="http://www.linglom.com/images/Windows/SQL-Server/SQLDMO.SQLServer-on-SQL-2008/5.png" target="_blank"><img src="http://www.linglom.com/images/Windows/SQL-Server/SQLDMO.SQLServer-on-SQL-2008/_5.png" width="250" height="188" alt="Feature Selection - SQL Distributed Management Objects (SQL-DMO)" title="Feature Selection - SQL Distributed Management Objects (SQL-DMO)"  /></a></li>
<li>On <strong>Ready to Install the Program</strong>, click <strong>Install</strong>.<br />
<a href="http://www.linglom.com/images/Windows/SQL-Server/SQLDMO.SQLServer-on-SQL-2008/6.png" target="_blank"><img src="http://www.linglom.com/images/Windows/SQL-Server/SQLDMO.SQLServer-on-SQL-2008/_6.png" width="250" height="188" alt="Ready to Install the Program" title="Ready to Install the Program"  /></a></li>
<li>Installing Microsoft SQL Server 2005 Backward compatibility.<br />
<a href="http://www.linglom.com/images/Windows/SQL-Server/SQLDMO.SQLServer-on-SQL-2008/7.png" target="_blank"><img src="http://www.linglom.com/images/Windows/SQL-Server/SQLDMO.SQLServer-on-SQL-2008/_7.png" width="250" height="188" alt="Installing Microsoft SQL Server 2005 Backward compatibility" title="Installing Microsoft SQL Server 2005 Backward compatibility"  /></a></li>
<li>On <strong>Completing the Microsoft SQL Server 2005 Backward compatibility Setup</strong>, click <strong>Finish</strong>. Now try to run vb script again, the problem should be gone!<br />
<a href="http://www.linglom.com/images/Windows/SQL-Server/SQLDMO.SQLServer-on-SQL-2008/8.png" target="_blank"><img src="http://www.linglom.com/images/Windows/SQL-Server/SQLDMO.SQLServer-on-SQL-2008/_8.png" width="250" height="188" alt="Completing the Microsoft SQL Server 2005 Backward compatibility Setup" title="Completing the Microsoft SQL Server 2005 Backward compatibility Setup"  /></a></li>
</ol>
<h3>Reference</h3>
<p><a href="http://blogs.msdn.com/dtjones/archive/2008/08/28/where-the-heck-is-dmo.aspx" target="_blank" rel="nofollow">Where the Heck is DMO?</a></p>
</div>

<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.linglom.com/2010/01/13/solved-activex-component-cant-create-object-sqldmo-sqlserver-on-sql-server-2008/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Restore Database From SQL Server 2008 to SQL Server 2005, Part 3: Export Data Wizard</title>
		<link>http://www.linglom.com/2009/08/17/restore-database-from-sql-server-2008-to-sql-server-2005-part-3-export-data-wizard/</link>
		<comments>http://www.linglom.com/2009/08/17/restore-database-from-sql-server-2008-to-sql-server-2005-part-3-export-data-wizard/#comments</comments>
		<pubDate>Mon, 17 Aug 2009 08:55:06 +0000</pubDate>
		<dc:creator>linglom</dc:creator>
				<category><![CDATA[MS SQL Server]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[SQL Server 2005]]></category>
		<category><![CDATA[SQL Server 2008]]></category>

		<guid isPermaLink="false">http://www.linglom.com/?p=986</guid>
		<description><![CDATA[Export Data Wizard On Part 1: Introduction, I mentioned about error messages when you try to restore a database from SQL Server 2008 to SQL Server 2005. You can see the first solution at Part 2: Generate SQL Server Scripts Wizard. Now let&#8217;s see the second solution to solve the problems. On this post, you [...]


Related posts:<ol><li><a href='http://www.linglom.com/2009/08/14/restore-database-from-sql-server-2008-to-sql-server-2005-part-2-generate-sql-server-scripts-wizard/' rel='bookmark' title='Permanent Link: Restore Database From SQL Server 2008 to SQL Server 2005, Part 2: Generate SQL Server Scripts Wizard'>Restore Database From SQL Server 2008 to SQL Server 2005, Part 2: Generate SQL Server Scripts Wizard</a></li>
<li><a href='http://www.linglom.com/2009/08/11/restore-database-from-sql-server-2008-to-sql-server-2005-part-1-introduction/' rel='bookmark' title='Permanent Link: Restore Database From SQL Server 2008 to SQL Server 2005, Part 1: Introduction'>Restore Database From SQL Server 2008 to SQL Server 2005, Part 1: Introduction</a></li>
<li><a href='http://www.linglom.com/2008/01/12/how-to-backup-and-restore-database-on-microsoft-sql-server-2005/' rel='bookmark' title='Permanent Link: How to backup and restore database on Microsoft SQL Server 2005'>How to backup and restore database on Microsoft SQL Server 2005</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div class="KonaBody"><h3>Export Data Wizard</h3>
<p>On <a href="http://www.linglom.com/2009/08/11/restore-database-from-sql-server-2008-to-sql-server-2005-part-1-introduction/">Part 1: Introduction</a>, I mentioned about error messages when you try to restore a database from SQL Server 2008 to SQL Server 2005. You can see the first solution at <a href="http://www.linglom.com/2009/08/14/restore-database-from-sql-server-2008-to-sql-server-2005-part-2-generate-sql-server-scripts-wizard/">Part 2: Generate SQL Server Scripts Wizard</a>. Now let&#8217;s see the second solution to solve the problems.</p>
<p><span id="more-986"></span></p>
<p>On this post, you see how to export tables on <strong>&#8216;Northwind&#8217;</strong> database from SQL Server 2008 to SQL Server 2005 using export data wizard.</p>
<p><!--adsense#Square--></p>
<h3>Step-by-step</h3>
<ol>
<li>On <strong>Microsoft SQL Server Management Studio</strong>, connects to SQL Server 2008. Right-click on the database that you want to export data -> select <strong>Tasks</strong> -> <strong>Export Data</strong>.<br />
<a href="http://www.linglom.com/images/Windows/SQL-Server/Restore-DB-From-SQL-2008-To-2005/Part-3/1.png" target="_blank"><img src="http://www.linglom.com/images/Windows/SQL-Server/Restore-DB-From-SQL-2008-To-2005/Part-3/_1.png" width="200" height="146" alt="Export Data" title="Export Data"  /></a></li>
<li>On <strong>Welcome to SQL Server Import and Export Wizard</strong>, click <strong>Next</strong>.<br />
<a href="http://www.linglom.com/images/Windows/SQL-Server/Restore-DB-From-SQL-2008-To-2005/Part-3/2.png" target="_blank"><img src="http://www.linglom.com/images/Windows/SQL-Server/Restore-DB-From-SQL-2008-To-2005/Part-3/_2.png" width="200" height="204" alt="Welcome to SQL Server Import and Export Wizard" title="Welcome to SQL Server Import and Export Wizard"  /></a></li>
<li>On <strong>Choose a Data Source</strong>, select the source from which to copy data. Set <strong>Data source</strong> to <strong>SQL Server Native Client 10.0</strong>. Verify that <strong>Server name</strong> is the source of SQL Server 2008 that you want and select <strong>Database</strong> as <strong>&#8216;Northwind&#8217;</strong>. Click <strong>Next</strong>.<br />
<a href="http://www.linglom.com/images/Windows/SQL-Server/Restore-DB-From-SQL-2008-To-2005/Part-3/3.png" target="_blank"><img src="http://www.linglom.com/images/Windows/SQL-Server/Restore-DB-From-SQL-2008-To-2005/Part-3/_3.png" width="200" height="204" alt="Choose a Data Source" title="Choose a Data Source"  /></a></li>
<li>On <strong>Choose a Destination</strong>, specify where to copy data to. Set Destination to <strong>SQL Server Native Client 10.0</strong>. Type the <strong>Server name</strong> to the destination of SQL Server 2005 that you want. You can also click <strong>Refresh</strong> to verify if you can connect to the specify server name. Currently, I don&#8217;t have <strong>&#8216;Northwind&#8217;</strong> database on SQL Server 2005 so I will create a new one, click <strong>New</strong>.<br />
<a href="http://www.linglom.com/images/Windows/SQL-Server/Restore-DB-From-SQL-2008-To-2005/Part-3/4.png" target="_blank"><img src="http://www.linglom.com/images/Windows/SQL-Server/Restore-DB-From-SQL-2008-To-2005/Part-3/_4.png" width="200" height="204" alt="Choose a Destination" title="Choose a Destination"  /></a></li>
<li>On <strong>Create Database</strong>, type name as <strong>&#8216;Northwind&#8217;</strong> and click <strong>OK</strong>.<br />
<a href="http://www.linglom.com/images/Windows/SQL-Server/Restore-DB-From-SQL-2008-To-2005/Part-3/5.png" target="_blank"><img src="http://www.linglom.com/images/Windows/SQL-Server/Restore-DB-From-SQL-2008-To-2005/Part-3/_5.png" width="200" height="194" alt="Create Database" title="Create Database"  /></a></li>
<li>Back to <strong>Choose a Destination</strong>, I have created <strong>&#8216;Northwind&#8217;</strong> database so select it as <strong>Database</strong>. Click <strong>Next</strong>.<br />
<a href="http://www.linglom.com/images/Windows/SQL-Server/Restore-DB-From-SQL-2008-To-2005/Part-3/6.png" target="_blank"><img src="http://www.linglom.com/images/Windows/SQL-Server/Restore-DB-From-SQL-2008-To-2005/Part-3/_6.png" width="200" height="204" alt="Choose a Destination" title="Choose a Destination"  /></a></li>
<li>On <strong>Specify Table Copy or Query</strong>, select <strong>Copy data from one or more tables or views</strong> and click <strong>Next</strong>.<br />
<a href="http://www.linglom.com/images/Windows/SQL-Server/Restore-DB-From-SQL-2008-To-2005/Part-3/7.png" target="_blank"><img src="http://www.linglom.com/images/Windows/SQL-Server/Restore-DB-From-SQL-2008-To-2005/Part-3/_7.png" width="200" height="204" alt="Copy data from one or more tables or views" title="Copy data from one or more tables or views"  /></a></li>
<li>On <strong>Select Sources Tables and Views</strong>, select tables that you want to export. On this example, I select all tables on <strong>&#8216;Northwind&#8217;</strong> database.<br />
<a href="http://www.linglom.com/images/Windows/SQL-Server/Restore-DB-From-SQL-2008-To-2005/Part-3/8.png" target="_blank"><img src="http://www.linglom.com/images/Windows/SQL-Server/Restore-DB-From-SQL-2008-To-2005/Part-3/_8.png" width="200" height="204" alt="Select Sources Tables and Views" title="Select Sources Tables and Views"  /></a></li>
<li>On <strong>Save and Run Package</strong>, click <strong>Next</strong>.<br />
<a href="http://www.linglom.com/images/Windows/SQL-Server/Restore-DB-From-SQL-2008-To-2005/Part-3/9.png" target="_blank"><img src="http://www.linglom.com/images/Windows/SQL-Server/Restore-DB-From-SQL-2008-To-2005/Part-3/_9.png" width="200" height="204" alt="Save and Run Package" title="Save and Run Package"  /></a></li>
<li>On <strong>Complete the Wizard</strong>, you can verify the choices made in the wizard. Then, click <strong>Finish</strong>.<br />
<a href="http://www.linglom.com/images/Windows/SQL-Server/Restore-DB-From-SQL-2008-To-2005/Part-3/10.png" target="_blank"><img src="http://www.linglom.com/images/Windows/SQL-Server/Restore-DB-From-SQL-2008-To-2005/Part-3/_10.png" width="200" height="204" alt="Complete the Wizard" title="Complete the Wizard"  /></a></li>
<li>Wait until the wizard finishes execution.<br />
<a href="http://www.linglom.com/images/Windows/SQL-Server/Restore-DB-From-SQL-2008-To-2005/Part-3/11.png" target="_blank"><img src="http://www.linglom.com/images/Windows/SQL-Server/Restore-DB-From-SQL-2008-To-2005/Part-3/_11.png" width="200" height="204" alt="Export Data Successful" title="Export Data Successful"  /></a></li>
<li>Now I have exported tables of <strong>&#8216;Northwind&#8217;</strong> database from SQL Server 2008 to SQL Server 2005 successfully.<br />
<a href="http://www.linglom.com/images/Windows/SQL-Server/Restore-DB-From-SQL-2008-To-2005/Part-3/12.png" target="_blank"><img src="http://www.linglom.com/images/Windows/SQL-Server/Restore-DB-From-SQL-2008-To-2005/Part-3/_12.png" width="200" height="146" alt="The Exported Tables" title="The Exported Tables"  /></a></li>
</ol>
</div>

<p>Related posts:<ol><li><a href='http://www.linglom.com/2009/08/14/restore-database-from-sql-server-2008-to-sql-server-2005-part-2-generate-sql-server-scripts-wizard/' rel='bookmark' title='Permanent Link: Restore Database From SQL Server 2008 to SQL Server 2005, Part 2: Generate SQL Server Scripts Wizard'>Restore Database From SQL Server 2008 to SQL Server 2005, Part 2: Generate SQL Server Scripts Wizard</a></li>
<li><a href='http://www.linglom.com/2009/08/11/restore-database-from-sql-server-2008-to-sql-server-2005-part-1-introduction/' rel='bookmark' title='Permanent Link: Restore Database From SQL Server 2008 to SQL Server 2005, Part 1: Introduction'>Restore Database From SQL Server 2008 to SQL Server 2005, Part 1: Introduction</a></li>
<li><a href='http://www.linglom.com/2008/01/12/how-to-backup-and-restore-database-on-microsoft-sql-server-2005/' rel='bookmark' title='Permanent Link: How to backup and restore database on Microsoft SQL Server 2005'>How to backup and restore database on Microsoft SQL Server 2005</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.linglom.com/2009/08/17/restore-database-from-sql-server-2008-to-sql-server-2005-part-3-export-data-wizard/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Restore Database From SQL Server 2008 to SQL Server 2005, Part 2: Generate SQL Server Scripts Wizard</title>
		<link>http://www.linglom.com/2009/08/14/restore-database-from-sql-server-2008-to-sql-server-2005-part-2-generate-sql-server-scripts-wizard/</link>
		<comments>http://www.linglom.com/2009/08/14/restore-database-from-sql-server-2008-to-sql-server-2005-part-2-generate-sql-server-scripts-wizard/#comments</comments>
		<pubDate>Fri, 14 Aug 2009 05:30:49 +0000</pubDate>
		<dc:creator>linglom</dc:creator>
				<category><![CDATA[MS SQL Server]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[SQL Server 2005]]></category>
		<category><![CDATA[SQL Server 2008]]></category>

		<guid isPermaLink="false">http://www.linglom.com/?p=982</guid>
		<description><![CDATA[Generate SQL Server Scripts Wizard On Part 1: Introduction, I mentioned about error messages when you try to restore a database from SQL Server 2008 to SQL Server 2005. Now let&#8217;s see a first solution to solve the problems. On this post, you see how to backup &#8216;Northwind&#8217; database by generate a SQL Server script [...]


Related posts:<ol><li><a href='http://www.linglom.com/2009/08/17/restore-database-from-sql-server-2008-to-sql-server-2005-part-3-export-data-wizard/' rel='bookmark' title='Permanent Link: Restore Database From SQL Server 2008 to SQL Server 2005, Part 3: Export Data Wizard'>Restore Database From SQL Server 2008 to SQL Server 2005, Part 3: Export Data Wizard</a></li>
<li><a href='http://www.linglom.com/2009/08/11/restore-database-from-sql-server-2008-to-sql-server-2005-part-1-introduction/' rel='bookmark' title='Permanent Link: Restore Database From SQL Server 2008 to SQL Server 2005, Part 1: Introduction'>Restore Database From SQL Server 2008 to SQL Server 2005, Part 1: Introduction</a></li>
<li><a href='http://www.linglom.com/2008/01/12/how-to-backup-and-restore-database-on-microsoft-sql-server-2005/' rel='bookmark' title='Permanent Link: How to backup and restore database on Microsoft SQL Server 2005'>How to backup and restore database on Microsoft SQL Server 2005</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div class="KonaBody"><h3>Generate SQL Server Scripts Wizard</h3>
<p>On <a href="http://www.linglom.com/2009/08/11/restore-database-from-sql-server-2008-to-sql-server-2005-part-1-introduction/">Part 1: Introduction</a>, I mentioned about error messages when you try to restore a database from SQL Server 2008 to SQL Server 2005. Now let&#8217;s see a first solution to solve the problems.</p>
<p><span id="more-982"></span></p>
<p>On this post, you see how to backup &#8216;Northwind&#8217; database by generate a SQL Server script on SQL Server 2008. Then, restore the &#8216;Northwind&#8217; database by execute the SQL Server script on SQL Server 2005.</p>
<p><!--adsense#Square--></p>
<h3>Step-by-step</h3>
<ol>
<li>On <strong>Microsoft SQL Server Management Studio</strong>, connects to the SQL Server 2008. Right-click on the database that you want to backup and select <strong>Tasks</strong> -> <strong>Generate Scripts</strong>.<br />
<a href="http://www.linglom.com/images/Windows/SQL-Server/Restore-DB-From-SQL-2008-To-2005/Part-2/3.png" target="_blank"><img src="http://www.linglom.com/images/Windows/SQL-Server/Restore-DB-From-SQL-2008-To-2005/Part-2/_3.png" width="200" height="146" alt="Generate Scripts" title="Generate Scripts"  /></a></li>
<li>On <strong>Welcome to the Generate SQL Server Scripts Wizard</strong>, click <strong>Next</strong>.<br />
<a href="http://www.linglom.com/images/Windows/SQL-Server/Restore-DB-From-SQL-2008-To-2005/Part-2/4.png" target="_blank"><img src="http://www.linglom.com/images/Windows/SQL-Server/Restore-DB-From-SQL-2008-To-2005/Part-2/_4.png" width="200" height="179" alt="Welcome to the Generate SQL Server Scripts Wizard" title="Welcome to the Generate SQL Server Scripts Wizard"  /></a></li>
<li>On <strong>Select Database</strong>, select <strong>Northwind</strong> and check <strong>Script all objects in the selected database</strong>. Then, click <strong>Next</strong>.<br />
<a href="http://www.linglom.com/images/Windows/SQL-Server/Restore-DB-From-SQL-2008-To-2005/Part-2/5.png" target="_blank"><img src="http://www.linglom.com/images/Windows/SQL-Server/Restore-DB-From-SQL-2008-To-2005/Part-2/_5.png" width="200" height="179" alt="Select Database" title="Select Database"  /></a></li>
<li>On <strong>Choose Script Options</strong>, set <strong>Script Database Create</strong> to <strong>False</strong> and <strong>Script for Server Version</strong> to <strong>SQL Server 2005</strong>.<br />
<em><strong>Note:</strong></em> You can set <strong>Script Database Create</strong> to <strong>True</strong> if your source and destination for store database files are the same location.<br />
<a href="http://www.linglom.com/images/Windows/SQL-Server/Restore-DB-From-SQL-2008-To-2005/Part-2/6.png" target="_blank"><img src="http://www.linglom.com/images/Windows/SQL-Server/Restore-DB-From-SQL-2008-To-2005/Part-2/_6.png" width="200" height="179" alt="Script for Server Version to SQL Server 2005" title="Script for Server Version to SQL Server 2005"  /></a></li>
<li>Continue on <strong>Choose Script Options</strong>, scroll down and set <strong>Script Data</strong> to <strong>True</strong>. Click <strong>Next</strong><br />
<em><strong>Note:</strong></em> Set this option to true to include data on each table to a script.<br />
<a href="http://www.linglom.com/images/Windows/SQL-Server/Restore-DB-From-SQL-2008-To-2005/Part-2/7.png" target="_blank"><img src="http://www.linglom.com/images/Windows/SQL-Server/Restore-DB-From-SQL-2008-To-2005/Part-2/_7.png" width="200" height="191" alt="Script Data to True" title="Script Data to True"  /></a></li>
<li>On <strong>Output Option</strong>, select a destination for the output script. Select <strong>Script to file</strong> and browse to the location that you want. Click <strong>Next</strong>.<br />
<a href="http://www.linglom.com/images/Windows/SQL-Server/Restore-DB-From-SQL-2008-To-2005/Part-2/8.png" target="_blank"><img src="http://www.linglom.com/images/Windows/SQL-Server/Restore-DB-From-SQL-2008-To-2005/Part-2/_8.png" width="200" height="191" alt="Select Script Output Destination" title="Select Script Output Destination"  /></a></li>
<li>On <strong>Script Wizard Summary</strong>, you can review your selections. Then, click <strong>Finish</strong>.<br />
<a href="http://www.linglom.com/images/Windows/SQL-Server/Restore-DB-From-SQL-2008-To-2005/Part-2/9.png" target="_blank"><img src="http://www.linglom.com/images/Windows/SQL-Server/Restore-DB-From-SQL-2008-To-2005/Part-2/_9.png" width="200" height="191" alt="Script Wizard Summary" title="Script Wizard Summary"  /></a></li>
<li>On <strong>Generate Script Progress</strong>, the wizard is creating a SQL Server script.<br />
<a href="http://www.linglom.com/images/Windows/SQL-Server/Restore-DB-From-SQL-2008-To-2005/Part-2/10.png" target="_blank"><img src="http://www.linglom.com/images/Windows/SQL-Server/Restore-DB-From-SQL-2008-To-2005/Part-2/_10.png" width="200" height="191" alt="Generate Script Progress" title="Generate Script Progress"  /></a></li>
<li>When the script has been completed, you see the output file as similar the figure below.<br />
<a href="http://www.linglom.com/images/Windows/SQL-Server/Restore-DB-From-SQL-2008-To-2005/Part-2/11.png" target="_blank"><img src="http://www.linglom.com/images/Windows/SQL-Server/Restore-DB-From-SQL-2008-To-2005/Part-2/_11.png" width="200" height="140" alt="The Generated Script" title="The Generated Script"  /></a></li>
<li>Connect to SQL Server 2005, create a new database. Right-click <strong>Database</strong> -> <strong>New Database</strong>.<br />
<em><strong>Note:</strong></em> If you have set <strong>Script Database Create</strong> to <strong>True</strong> on step 4, you don&#8217;t have to create a database manually.<br />
<a href="http://www.linglom.com/images/Windows/SQL-Server/Restore-DB-From-SQL-2008-To-2005/Part-2/12.png" target="_blank"><img src="http://www.linglom.com/images/Windows/SQL-Server/Restore-DB-From-SQL-2008-To-2005/Part-2/_12.png" width="200" height="154" alt="Create New Database" title="Create New Database"  /></a></li>
<li>Type <strong>&#8216;Northwind&#8217;</strong> as database name. Click <strong>OK</strong>.<br />
<a href="http://www.linglom.com/images/Windows/SQL-Server/Restore-DB-From-SQL-2008-To-2005/Part-2/13.png" target="_blank" target="_blank"><img src="http://www.linglom.com/images/Windows/SQL-Server/Restore-DB-From-SQL-2008-To-2005/Part-2/_13.png" width="200" height="180" alt="Create Database Northwind" title="Create Database Northwind"  /></a></li>
<li>Execute the SQL Server script file that you have created.<br />
<a href="http://www.linglom.com/images/Windows/SQL-Server/Restore-DB-From-SQL-2008-To-2005/Part-2/14.png" target="_blank"><img src="http://www.linglom.com/images/Windows/SQL-Server/Restore-DB-From-SQL-2008-To-2005/Part-2/_14.png" width="200" height="154" alt="Execute the script" title="Execute the script"  /></a></li>
<li>Now the database <strong>&#8216;Northwind&#8217;</strong> is restored on SQL Server 2005.<br />
<a href="http://www.linglom.com/images/Windows/SQL-Server/Restore-DB-From-SQL-2008-To-2005/Part-2/15.png" target="_blank"><img src="http://www.linglom.com/images/Windows/SQL-Server/Restore-DB-From-SQL-2008-To-2005/Part-2/_15.png" width="200" height="154" alt="Northwind Database" title="Northwind Database"  /></a></li>
</ol>
</div>

<p>Related posts:<ol><li><a href='http://www.linglom.com/2009/08/17/restore-database-from-sql-server-2008-to-sql-server-2005-part-3-export-data-wizard/' rel='bookmark' title='Permanent Link: Restore Database From SQL Server 2008 to SQL Server 2005, Part 3: Export Data Wizard'>Restore Database From SQL Server 2008 to SQL Server 2005, Part 3: Export Data Wizard</a></li>
<li><a href='http://www.linglom.com/2009/08/11/restore-database-from-sql-server-2008-to-sql-server-2005-part-1-introduction/' rel='bookmark' title='Permanent Link: Restore Database From SQL Server 2008 to SQL Server 2005, Part 1: Introduction'>Restore Database From SQL Server 2008 to SQL Server 2005, Part 1: Introduction</a></li>
<li><a href='http://www.linglom.com/2008/01/12/how-to-backup-and-restore-database-on-microsoft-sql-server-2005/' rel='bookmark' title='Permanent Link: How to backup and restore database on Microsoft SQL Server 2005'>How to backup and restore database on Microsoft SQL Server 2005</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.linglom.com/2009/08/14/restore-database-from-sql-server-2008-to-sql-server-2005-part-2-generate-sql-server-scripts-wizard/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Restore Database From SQL Server 2008 to SQL Server 2005, Part 1: Introduction</title>
		<link>http://www.linglom.com/2009/08/11/restore-database-from-sql-server-2008-to-sql-server-2005-part-1-introduction/</link>
		<comments>http://www.linglom.com/2009/08/11/restore-database-from-sql-server-2008-to-sql-server-2005-part-1-introduction/#comments</comments>
		<pubDate>Tue, 11 Aug 2009 03:11:32 +0000</pubDate>
		<dc:creator>linglom</dc:creator>
				<category><![CDATA[MS SQL Server]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[SQL Server 2005]]></category>
		<category><![CDATA[SQL Server 2008]]></category>

		<guid isPermaLink="false">http://www.linglom.com/?p=979</guid>
		<description><![CDATA[Problem When you restore or attach a database which is created from SQL Server 2008 to SQL Server 2005 or SQL Server 2000, you will see some error messages as the examples below. Backup and Restore You have backup a database from SQL Server 2008. If you try to restore the backup database file to [...]


Related posts:<ol><li><a href='http://www.linglom.com/2009/08/14/restore-database-from-sql-server-2008-to-sql-server-2005-part-2-generate-sql-server-scripts-wizard/' rel='bookmark' title='Permanent Link: Restore Database From SQL Server 2008 to SQL Server 2005, Part 2: Generate SQL Server Scripts Wizard'>Restore Database From SQL Server 2008 to SQL Server 2005, Part 2: Generate SQL Server Scripts Wizard</a></li>
<li><a href='http://www.linglom.com/2009/08/17/restore-database-from-sql-server-2008-to-sql-server-2005-part-3-export-data-wizard/' rel='bookmark' title='Permanent Link: Restore Database From SQL Server 2008 to SQL Server 2005, Part 3: Export Data Wizard'>Restore Database From SQL Server 2008 to SQL Server 2005, Part 3: Export Data Wizard</a></li>
<li><a href='http://www.linglom.com/2008/01/12/how-to-backup-and-restore-database-on-microsoft-sql-server-2005/' rel='bookmark' title='Permanent Link: How to backup and restore database on Microsoft SQL Server 2005'>How to backup and restore database on Microsoft SQL Server 2005</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div class="KonaBody"><h3>Problem</h3>
<p>When you restore or attach a database which is created from SQL Server 2008 to SQL Server 2005 or SQL Server 2000, you will see some error messages as the examples below.</p>
<p><span id="more-979"></span></p>
<p><!--adsense#Square--></p>
<h4>Backup and Restore</h4>
<p>You have backup a database from SQL Server 2008. If you try to restore the backup database file to SQL Server 2005, you will receive the error message:</p>
<p><strong>An exception occurred while executing a Transact-SQL statement or batch. (Microsoft.SqlServer.ConnectionInfo)<br />
Additional information:<br />
-> The media family on device &#8216;<em>the backup file</em>&#8216; is incorrectly formed. SQL Server cannot process this media family.<br />
RESTORE HEADERONLY is terminating abnormally. (Microsoft SQL Server, Error: 3241)</strong><br />
<a href="http://www.linglom.com/images/Windows/SQL-Server/Restore-DB-From-SQL-2008-To-2005/Part-1/1.png" target="_blank"><img src="http://www.linglom.com/images/Windows/SQL-Server/Restore-DB-From-SQL-2008-To-2005/Part-1/_1.png" width="200" height="180" alt="Restore Database Error Message" title="Restore Database Error Message"  /></a></p>
<h4>Detach and Attach</h4>
<p>You have detach a database from SQL Server 2008. If you try to attach the detached database file to SQL Server 2005, you will receive the error message:</p>
<p><strong>Attach database failed for Server <em>&#8216;SQL Server name&#8217;</em>. (Microsoft.SqlServer.Smo)<br />
Additional information:<br />
-> An exception occurred while executing a Transact-SQL statement batch. (Microsoft.SqlServer.ConnectionInfo)<br />
&#8211;> The database <em>&#8216;database name&#8217;</em> cannot be opened because it is version 655. This server supports version 611 and earlier. A downgrade path is not supported.<br />
Could not open new database <em>&#8216;database name&#8217;</em>. CREATE DATABASE is aborted. (Microsoft SQL Server, Error: 948)</strong><br />
<a href="http://www.linglom.com/images/Windows/SQL-Server/Restore-DB-From-SQL-2008-To-2005/Part-1/2.png" target="_blank"><img src="http://www.linglom.com/images/Windows/SQL-Server/Restore-DB-From-SQL-2008-To-2005/Part-1/_2.png" width="200" height="180" alt="Attach Database Error Message" title="Attach Database Error Message"  /></a></p>
<h3>Solution</h3>
<p>These problems occur because a backup or detach database file is not backward compatible. You cannot restore or attach a database which is created from a higher version of SQL Server to a lower version of SQL Server. </p>
<p>But there are some alternatives which can help you to restore a database to a lower version of SQL Server. I divide into separate parts.</p>
<ol>
<li><strong><a href="http://www.linglom.com/2009/08/14/restore-database-from-sql-server-2008-to-sql-server-2005-part-2-generate-sql-server-scripts-wizard/">Part 2: Generate SQL Server Scripts Wizard</a></strong>. The solution creates a SQL Server script file using a wizard. Then, you simply execute the script file on SQL Server 2005 or SQL Server 2000. So you will get everything as same as the source database on the destination. But there are some disadvantages:
<ul>
<li>If the source database contains lots of data, you will have a large script file.</li>
<li>The generated file is a plain text. Anyone who has access to the file can read it. So you should delete the script file after the restoration.</li>
</ul>
</li>
<li><strong><a href="http://www.linglom.com/2009/08/17/restore-database-from-sql-server-2008-to-sql-server-2005-part-3-export-data-wizard/">Part 3: Import and Export Wizard</a></strong>. This solution exports data to the targeted SQL Server using a wizard. It is more secure and effective than the first solution. But you can only export tables and views only.</li>
</ol>
</div>

<p>Related posts:<ol><li><a href='http://www.linglom.com/2009/08/14/restore-database-from-sql-server-2008-to-sql-server-2005-part-2-generate-sql-server-scripts-wizard/' rel='bookmark' title='Permanent Link: Restore Database From SQL Server 2008 to SQL Server 2005, Part 2: Generate SQL Server Scripts Wizard'>Restore Database From SQL Server 2008 to SQL Server 2005, Part 2: Generate SQL Server Scripts Wizard</a></li>
<li><a href='http://www.linglom.com/2009/08/17/restore-database-from-sql-server-2008-to-sql-server-2005-part-3-export-data-wizard/' rel='bookmark' title='Permanent Link: Restore Database From SQL Server 2008 to SQL Server 2005, Part 3: Export Data Wizard'>Restore Database From SQL Server 2008 to SQL Server 2005, Part 3: Export Data Wizard</a></li>
<li><a href='http://www.linglom.com/2008/01/12/how-to-backup-and-restore-database-on-microsoft-sql-server-2005/' rel='bookmark' title='Permanent Link: How to backup and restore database on Microsoft SQL Server 2005'>How to backup and restore database on Microsoft SQL Server 2005</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.linglom.com/2009/08/11/restore-database-from-sql-server-2008-to-sql-server-2005-part-1-introduction/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>[Solved] System.Data.SqlClient.SqlError: The backup set holds a backup of a database other than the existing &#8216;dbName&#8217; database</title>
		<link>http://www.linglom.com/2009/07/08/solved-system-data-sqlclient-sqlerror-the-backup-set-holds-a-backup-of-a-database-other-than-the-existing-dbname-database/</link>
		<comments>http://www.linglom.com/2009/07/08/solved-system-data-sqlclient-sqlerror-the-backup-set-holds-a-backup-of-a-database-other-than-the-existing-dbname-database/#comments</comments>
		<pubDate>Wed, 08 Jul 2009 09:09:09 +0000</pubDate>
		<dc:creator>linglom</dc:creator>
				<category><![CDATA[MS SQL Server]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[SQL Server 2005]]></category>

		<guid isPermaLink="false">http://www.linglom.com/?p=917</guid>
		<description><![CDATA[Problem You have backup a database on SQL Server 2005. Then, you try to restore the backup file on an existing database and receive the error message below: Restore failed for Server &#8216;SQL Server name&#8216;. (Microsoft.SqlServer.Smo) Additional information: System.Data.SqlClient.SqlError: The backup set holds a backup of a database other than the existing &#8216;dbName&#8216; database. (Microsoft.SqlServer.Smo) [...]


Related posts:<ol><li><a href='http://www.linglom.com/2009/07/03/solved-system-data-sqlclient-sqlerror-the-tail-of-the-log-for-the-database-dbname-has-not-been-backed-up/' rel='bookmark' title='Permanent Link: [Solved] System.Data.SqlClient.SqlError: The tail of the log for the database &#8220;dbName&#8221; has not been backed up'>[Solved] System.Data.SqlClient.SqlError: The tail of the log for the database &#8220;dbName&#8221; has not been backed up</a></li>
<li><a href='http://www.linglom.com/2008/01/12/how-to-backup-and-restore-database-on-microsoft-sql-server-2005/' rel='bookmark' title='Permanent Link: How to backup and restore database on Microsoft SQL Server 2005'>How to backup and restore database on Microsoft SQL Server 2005</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div class="KonaBody"><h3>Problem</h3>
<p>You have backup a database on SQL Server 2005. Then, you try to restore the backup file on an existing database and receive the error message below:</p>
<p><span id="more-917"></span></p>
<p>Restore failed for Server &#8216;<em>SQL Server name</em>&#8216;. (Microsoft.SqlServer.Smo)<br />
<strong>Additional information:</strong><br />
System.Data.SqlClient.SqlError: The backup set holds a backup of a database other than the existing &#8216;<em>dbName</em>&#8216; database. (Microsoft.SqlServer.Smo)<br />
<a href="http://www.linglom.com/images/Windows/SQL-Server/Backup-Restore/Troubleshoot/Message2/1.png" target="_blank"><img src="http://www.linglom.com/images/Windows/SQL-Server/Backup-Restore/Troubleshoot/Message2/_1.png" width="200" height="53" alt="The backup set holds a backup of a database other than the existing" title="The backup set holds a backup of a database other than the existing"  /></a></p>
<p><!--adsense#Square--></p>
<p>This error message indicates that you are going to restore a database on another database which are not the same database. For example, you have backup Northwind database and try to restore the backup to AdventureWorks database, the error will occur.</p>
<h3>Solution</h3>
<p>To solve the problem, you can use the <strong>overwrite the existing database</strong> option while you&#8217;re restoring.</p>
<ol>
<li>On <strong>Restore Database</strong>, select <strong>Options</strong> tab on the left menu. Then, check <strong>Overwrite the existing database</strong> on Restore options.<br />
<em><strong>Note: </strong></em>This option will overwrite any existing data on the targeted database.<br />
<a href="http://www.linglom.com/images/Windows/SQL-Server/Backup-Restore/Troubleshoot/Message2/2.png" target="_blank"><img src="http://www.linglom.com/images/Windows/SQL-Server/Backup-Restore/Troubleshoot/Message2/_2.png" width="200" height="179" alt="Restore Database Option" title="Restore Database Option"  /></a></li>
<li>Try to restore the database, the problem should be gone now.<br />
<a href="http://www.linglom.com/images/Windows/SQL-Server/Backup-Restore/Troubleshoot/Message2/3.png" target="_blank"><img src="http://www.linglom.com/images/Windows/SQL-Server/Backup-Restore/Troubleshoot/Message2/_3.png" width="200" height="41" alt="Restore Database Successful" title="Restore Database Successful"  /></a></li>
</ol>
<p><em><strong>Note:</strong></em> you can also delete the targeted database before perform restore a database. This way also gives the result as same as the solution above.</p>
</div>

<p>Related posts:<ol><li><a href='http://www.linglom.com/2009/07/03/solved-system-data-sqlclient-sqlerror-the-tail-of-the-log-for-the-database-dbname-has-not-been-backed-up/' rel='bookmark' title='Permanent Link: [Solved] System.Data.SqlClient.SqlError: The tail of the log for the database &#8220;dbName&#8221; has not been backed up'>[Solved] System.Data.SqlClient.SqlError: The tail of the log for the database &#8220;dbName&#8221; has not been backed up</a></li>
<li><a href='http://www.linglom.com/2008/01/12/how-to-backup-and-restore-database-on-microsoft-sql-server-2005/' rel='bookmark' title='Permanent Link: How to backup and restore database on Microsoft SQL Server 2005'>How to backup and restore database on Microsoft SQL Server 2005</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.linglom.com/2009/07/08/solved-system-data-sqlclient-sqlerror-the-backup-set-holds-a-backup-of-a-database-other-than-the-existing-dbname-database/feed/</wfw:commentRss>
		<slash:comments>26</slash:comments>
		</item>
		<item>
		<title>[Solved] System.Data.SqlClient.SqlError: The tail of the log for the database &#8220;dbName&#8221; has not been backed up</title>
		<link>http://www.linglom.com/2009/07/03/solved-system-data-sqlclient-sqlerror-the-tail-of-the-log-for-the-database-dbname-has-not-been-backed-up/</link>
		<comments>http://www.linglom.com/2009/07/03/solved-system-data-sqlclient-sqlerror-the-tail-of-the-log-for-the-database-dbname-has-not-been-backed-up/#comments</comments>
		<pubDate>Fri, 03 Jul 2009 06:33:13 +0000</pubDate>
		<dc:creator>linglom</dc:creator>
				<category><![CDATA[MS SQL Server]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[SQL Server 2005]]></category>

		<guid isPermaLink="false">http://www.linglom.com/?p=922</guid>
		<description><![CDATA[Problem You have backup a database on SQL Server 2005. Then, you try to restore the backup file on the existing database and receive the error message below: Restore failed for Server &#8216;SQL Server name&#8216;. (Microsoft.SqlServer.Smo) Additional information: System.Data.SqlClient.SqlError: The tail of the log for the database &#8220;dbName&#8221; has not been backed up. Use BACKUP [...]


Related posts:<ol><li><a href='http://www.linglom.com/2009/07/08/solved-system-data-sqlclient-sqlerror-the-backup-set-holds-a-backup-of-a-database-other-than-the-existing-dbname-database/' rel='bookmark' title='Permanent Link: [Solved] System.Data.SqlClient.SqlError: The backup set holds a backup of a database other than the existing &#8216;dbName&#8217; database'>[Solved] System.Data.SqlClient.SqlError: The backup set holds a backup of a database other than the existing &#8216;dbName&#8217; database</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div class="KonaBody"><h3>Problem</h3>
<p>You have backup a database on SQL Server 2005. Then, you try to restore the backup file on the existing database and receive the error message below:</p>
<p><span id="more-922"></span></p>
<p>Restore failed for Server &#8216;<em>SQL Server name</em>&#8216;. (Microsoft.SqlServer.Smo)<br />
<strong>Additional information:</strong><br />
System.Data.SqlClient.SqlError: The tail of the log for the database &#8220;<em>dbName</em>&#8221; has not been backed up. Use BACKUP LOG WITH NORECOVERY to backup the log if it contains work you do not want to lose. Use the WITH REPLACE or WITH STOPAT clause of the RESTORE statement to just overwrite the contents of the log. (Microsoft.SqlServer.Smo)<br />
<a href="http://www.linglom.com/images/Windows/SQL-Server/Backup-Restore/Troubleshoot/Message1/1.png" target="_blank"><img src="http://www.linglom.com/images/Windows/SQL-Server/Backup-Restore/Troubleshoot/Message1/_1.png" width="200" height="62" alt="The tail of the log for the database 'dbName' has not been backed up" title="The tail of the log for the database 'dbName' has not been backed up"  /></a></p>
<p>This error message is likely to be a warning message rather than an error message. It indicates that you are going to restore a database to replace the existing database and some data may be lose. For example, you perform backup on this following schedule:</p>
<ul>
<li>database at 10:00 AM</li>
<li>transaction log at 11:00 AM</li>
<li>transaction log at 01:00 PM</li>
</ul>
<p>Now if you are going to restore the backup at 04:00 PM. You will lose any work after 01:00 till 04:00 which is same as the error message above try to warn you.</p>
<p><!--adsense#Square--></p>
<h3>Solution</h3>
<p>To solve the problem, you can either:</p>
<ul>
<li>Replace the existing database with a backup file. This option is fast and easy but you will lose any work after the last backup has been performed.</li>
<li>Do backup transaction log again with NORECOVERY option before restoring a database. With NORECOVERY option, the database will be in restoring state and waiting for restoration.</li>
</ul>
<p>In this post, I will show only the first solution only. For the second solution, see <a href="http://msdn.microsoft.com/en-us/library/dd297499.aspx" target="_blank" rel="nofollow">How to: Back Up the Tail of the Transaction Log (SQL Server Management Studio)</a></p>
<h4>Restore by overwrite an existing database</h4>
<ol>
<li>On <strong>Restore Database</strong>, select <strong>Options</strong> tab on the left menu. Then, check <strong>Overwrite the existing database</strong> on Restore options.<br />
<em><strong>Note: </strong></em>This option will overwrite any existing data on the targeted database.<br />
<a href="http://www.linglom.com/images/Windows/SQL-Server/Backup-Restore/Troubleshoot/Message1/2.png" target="_blank"><img src="http://www.linglom.com/images/Windows/SQL-Server/Backup-Restore/Troubleshoot/Message1/_2.png" width="200" height="179" alt="Restore Database Options" title="Restore Database Options"  /></a></li>
<li>Try to restore the database, the problem should be gone now.<br />
<a href="http://www.linglom.com/images/Windows/SQL-Server/Backup-Restore/Troubleshoot/Message1/3.png" target="_blank"><img src="http://www.linglom.com/images/Windows/SQL-Server/Backup-Restore/Troubleshoot/Message1/_3.png" width="200" height="41" alt="Restore Database Successful" title="Restore Database Successful"  /></a></li>
</ol>
</div>

<p>Related posts:<ol><li><a href='http://www.linglom.com/2009/07/08/solved-system-data-sqlclient-sqlerror-the-backup-set-holds-a-backup-of-a-database-other-than-the-existing-dbname-database/' rel='bookmark' title='Permanent Link: [Solved] System.Data.SqlClient.SqlError: The backup set holds a backup of a database other than the existing &#8216;dbName&#8217; database'>[Solved] System.Data.SqlClient.SqlError: The backup set holds a backup of a database other than the existing &#8216;dbName&#8217; database</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.linglom.com/2009/07/03/solved-system-data-sqlclient-sqlerror-the-tail-of-the-log-for-the-database-dbname-has-not-been-backed-up/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Enable Remote Connection on SQL Server 2008 Express</title>
		<link>http://www.linglom.com/2009/03/28/enable-remote-connection-on-sql-server-2008-express/</link>
		<comments>http://www.linglom.com/2009/03/28/enable-remote-connection-on-sql-server-2008-express/#comments</comments>
		<pubDate>Sat, 28 Mar 2009 12:55:09 +0000</pubDate>
		<dc:creator>linglom</dc:creator>
				<category><![CDATA[MS SQL Server]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[SQL Server 2008]]></category>

		<guid isPermaLink="false">http://www.linglom.com/?p=665</guid>
		<description><![CDATA[Introduction Last time, I wrote an article show how to enable remote connection on SQL Server 2005 Express. Now SQL Server 2008 Express is released for a while, it doesn&#8217;t allow remote connection on default installation as on SQL Server 2005 Express. So you have to enable it manually. If you&#8217;re trying to connect to [...]


Related posts:<ol><li><a href='http://www.linglom.com/2007/08/31/enable-remote-connection-to-sql-server-2005-express/' rel='bookmark' title='Permanent Link: Enable remote connection to SQL Server 2005 Express'>Enable remote connection to SQL Server 2005 Express</a></li>
<li><a href='http://www.linglom.com/2008/10/04/how-to-enable-remote-desktop-web-connection-on-windows/' rel='bookmark' title='Permanent Link: How to Enable Remote Desktop Web Connection on Windows'>How to Enable Remote Desktop Web Connection on Windows</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div class="KonaBody"><h3>Introduction</h3>
<p>Last time, I wrote an article show how to <a href="http://www.linglom.com/2007/08/31/enable-remote-connection-to-sql-server-2005-express/">enable remote connection on SQL Server 2005 Express</a>. Now SQL Server 2008 Express is released for a while, it doesn&#8217;t allow remote connection on default installation as on SQL Server 2005 Express. So you have to enable it manually. </p>
<p><span id="more-665"></span></p>
<p><!--adsense#Square--></p>
<p>If you&#8217;re trying to connect to SQL Server 2008 Express remotely without enable remote connection first, you may see these error messages:</p>
<ul>
<li><strong>&#8220;Cannot connect to <em>SQL-Server-Instance-Name</em><br />
An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: SQL Network Interfaces, error: 28 &#8211; Server doesn&#8217;t support requested protocol) (Microsoft SQL Server)&#8221;</strong><br />
<a href="http://www.linglom.com/images/Windows/Programming/SQLServer/Enable-Remote-SQL-2008/1.png" target="_blank"><img src="http://www.linglom.com/images/Windows/Programming/SQLServer/Enable-Remote-SQL-2008/_1.png" width="200" height="62" alt="Server doesn't support requested protocol" title="Server doesn't support requested protocol"  /></a></li>
<li><strong>&#8220;Cannot connect to <em>SQL-Server-Instance-Name</em><br />
An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: SQL Network Interfaces, error: 26 &#8211; Error Locating Server/Instance Specified) (Microsoft SQL Server)&#8221;</strong><br />
<a href="http://www.linglom.com/images/Windows/Programming/SQLServer/Enable-Remote-SQL-2008/2.png" target="_blank"><img src="http://www.linglom.com/images/Windows/Programming/SQLServer/Enable-Remote-SQL-2008/_2.png" width="200" height="62" alt="Error Locating Server/Instance Specified" title="Error Locating Server/Instance Specified"  /></a></li>
<li><strong>&#8220;Cannot connect to <em>SQL-Server-Instance-Name</em><br />
Login failed for user &#8216;<em>username</em>&#8216;. (Microsoft SQL Server, Error: 18456)&#8221;</strong><br />
<a href="http://www.linglom.com/images/Windows/Programming/SQLServer/Enable-Remote-SQL-2008/3.png" target="_blank"><img src="http://www.linglom.com/images/Windows/Programming/SQLServer/Enable-Remote-SQL-2008/_3.png" width="200" height="49" alt="Login failed for user 'sa'" title="Login failed for user 'sa'"  /></a></li>
</ul>
<p>To enable remote connection on SQL Server 2008 Express, see the step below:</p>
<ol>
<li>Start SQL Server Browser service if it&#8217;s not started yet. SQL Server Browser listens for incoming requests for Microsoft SQL Server resources and provides information about SQL Server instances installed on the computer.</li>
<li>Enable TCP/IP protocol for SQL Server 2008 Express to accept remote connection.</li>
<li><strong>(Optional)</strong> Change Server Authentication to SQL Server and Windows Authentication. By default, SQL Server 2008 Express allows only Windows Authentication mode so you can connect to the SQL Server with current user log-on credential. If you want to specify user for connect to the SQL Server, you have to change Server Authentication to SQL Server and Windows Authentication.</li>
</ol>
<p><em><strong>Note:</strong></em> In SQL Server 2008 Express, there isn&#8217;t <strong>SQL Server Surface Area Configuration</strong> so you have to configure from <strong>SQL Server Configuration Manager</strong> instead.</p>
<h3>Step-by-step</h3>
<ol>
<li>Open SQL Server Configuration Manager. Click <strong>Start</strong> -> <strong>Programs</strong> -> <strong>Microsoft SQL Server 2008</strong> -> <strong>Configuration Tools</strong> -> <strong>SQL Server Configuration Manager</strong>.<br />
<a href="http://www.linglom.com/images/Windows/Programming/SQLServer/Enable-Remote-SQL-2008/4.png" target="_blank"><img src="http://www.linglom.com/images/Windows/Programming/SQLServer/Enable-Remote-SQL-2008/_4.png" width="200" height="91" alt="SQL Server Configuration Manager" title="SQL Server Configuration Manager"  /></a></li>
<li>On SQL Server Configuration Manager, select <strong>SQL Server Services</strong> on the left window. If the state on SQL Server Browser is not running, you have to configure and start the service. Otherwise, you can skip to step 6.<br />
<a href="http://www.linglom.com/images/Windows/Programming/SQLServer/Enable-Remote-SQL-2008/5.png" target="_blank"><img src="http://www.linglom.com/images/Windows/Programming/SQLServer/Enable-Remote-SQL-2008/_5.png" width="200" height="136" alt="SQL Server Browser Service" title="SQL Server Browser Service"  /></a></li>
<li>Double-click on SQL Server Browser, the Properties window will show up. Set the account for start <strong>SQL Server Browser Service</strong>. In this example, I set to <strong>Local Service</strong> account.<br />
<a href="http://www.linglom.com/images/Windows/Programming/SQLServer/Enable-Remote-SQL-2008/6.png" target="_blank"><img src="http://www.linglom.com/images/Windows/Programming/SQLServer/Enable-Remote-SQL-2008/_6.png" width="200" height="224" alt="Set Startup Account" title="Set Startup Account"  /></a></li>
<li>On SQL Server Browser Properties, move to <strong>Service</strong> tab and change <strong>Start Mode</strong> to <strong>Automatic</strong>. Therefore, the service will be start automatically when the computer starts. Click <strong>OK</strong> to apply changes.<br />
<a href="http://www.linglom.com/images/Windows/Programming/SQLServer/Enable-Remote-SQL-2008/7.png" target="_blank"><img src="http://www.linglom.com/images/Windows/Programming/SQLServer/Enable-Remote-SQL-2008/_7.png" width="200" height="224" alt="Set Start Mode to Automatic" title="Set Start Mode to Automatic"  /></a></li>
<li>Back to SQL Server Configuration Manager, right-click on <strong>SQL Server Bowser</strong> on the right window and select <strong>Start</strong> to start the service.<br />
<a href="http://www.linglom.com/images/Windows/Programming/SQLServer/Enable-Remote-SQL-2008/8.png" target="_blank"><img src="http://www.linglom.com/images/Windows/Programming/SQLServer/Enable-Remote-SQL-2008/_8.png" width="200" height="136" alt="Start SQL Server Browser Service" title="Start SQL Server Browser Service"  /></a></li>
<li>On the left window, expand <strong>SQL Server Network Configuration</strong> -> <strong>Protocols for SQLEXPRESS</strong>. You see that TCP/IP protocol status is disabled.<br />
<a href="http://www.linglom.com/images/Windows/Programming/SQLServer/Enable-Remote-SQL-2008/9.png" target="_blank" target="_blank"><img src="http://www.linglom.com/images/Windows/Programming/SQLServer/Enable-Remote-SQL-2008/_9.png" width="200" height="143" alt="Protocols for SQL EXPRESS" title="Protocols for SQL EXPRESS"  /></a></li>
<li>Right-click on <strong>TCP/IP</strong> and select <strong>Enable</strong> to enable the protocol.<br />
<a href="http://www.linglom.com/images/Windows/Programming/SQLServer/Enable-Remote-SQL-2008/10.png" target="_blank"><img src="http://www.linglom.com/images/Windows/Programming/SQLServer/Enable-Remote-SQL-2008/_10.png" width="200" height="143" alt="Enable TCP/IP protocol" title="Enable TCP/IP protocol"  /></a></li>
<li>There is a pop-up shown up that you have to restart the SQL Service to apply changes.<br />
<a href="http://www.linglom.com/images/Windows/Programming/SQLServer/Enable-Remote-SQL-2008/11.png" target="_blank"><img src="http://www.linglom.com/images/Windows/Programming/SQLServer/Enable-Remote-SQL-2008/_11.png" width="200" height="42" alt="Need to Restart SQL Server Service" title="Need to Restart SQL Server Service"  /></a></li>
<li>On the left window, select <strong>SQL Server Services</strong>. Select <strong>SQL Server (SQLEXPRESS)</strong> on the right window -> click <strong>Restart</strong>. The SQL Server service will be restarted.<br />
<a href="http://www.linglom.com/images/Windows/Programming/SQLServer/Enable-Remote-SQL-2008/12.png" target="_blank"><img src="http://www.linglom.com/images/Windows/Programming/SQLServer/Enable-Remote-SQL-2008/_12.png" width="200" height="143" alt="Restart SQL Server Service" title="Restart SQL Server Service"  /></a></li>
<li>Open Microsoft SQL Server Management Studio and connect to the SQL Server 2008 Express.<br />
<a href="http://www.linglom.com/images/Windows/Programming/SQLServer/Enable-Remote-SQL-2008/13.png" target="_blank"><img src="http://www.linglom.com/images/Windows/Programming/SQLServer/Enable-Remote-SQL-2008/_13.png" width="200" height="154" alt="Open Microsoft SQL Server Management Studio" title="Open Microsoft SQL Server Management Studio"  /></a></li>
<li>Right-click on the SQL Server Instance and select <strong>Properties</strong>.<br />
<a href="http://www.linglom.com/images/Windows/Programming/SQLServer/Enable-Remote-SQL-2008/14.png" target="_blank"><img src="http://www.linglom.com/images/Windows/Programming/SQLServer/Enable-Remote-SQL-2008/_14.png" width="200" height="154" alt="Open Server Properties" title="Open Server Properties"  /></a></li>
<li>On Server Properties, select <strong>Security</strong> on the left window. Then, select <strong>SQL Server and Windows Authentication</strong> mode.<br />
<a href="http://www.linglom.com/images/Windows/Programming/SQLServer/Enable-Remote-SQL-2008/15.png" target="_blank"><img src="http://www.linglom.com/images/Windows/Programming/SQLServer/Enable-Remote-SQL-2008/_15.png" width="200" height="180" alt="Change Authentication to SQL Server and Windows Authentication" title="Change Authentication to SQL Server and Windows Authentication"  /></a></li>
<li>Again, there is a pop-up shown up that you have to restart the SQL Service to apply changes.<br />
<a href="http://www.linglom.com/images/Windows/Programming/SQLServer/Enable-Remote-SQL-2008/16.png" target="_blank"><img src="http://www.linglom.com/images/Windows/Programming/SQLServer/Enable-Remote-SQL-2008/_16.png" width="200" height="43" alt="Need to Restart SQL Server Service" title="Need to Restart SQL Server Service"  /></a></li>
<li>Right-click on the SQL Server Instance and select <strong>Restart</strong>.<br />
<a href="http://www.linglom.com/images/Windows/Programming/SQLServer/Enable-Remote-SQL-2008/17.png" target="_blank"><img src="http://www.linglom.com/images/Windows/Programming/SQLServer/Enable-Remote-SQL-2008/_17.png" width="200" height="154" alt="Restart SQL Server Service" title="Restart SQL Server Service"  /></a></li>
<li>That&#8217;s it. Now you should be able to connect to the SQL Server 2008 Express remotely.</li>
</ol>
</div>

<p>Related posts:<ol><li><a href='http://www.linglom.com/2007/08/31/enable-remote-connection-to-sql-server-2005-express/' rel='bookmark' title='Permanent Link: Enable remote connection to SQL Server 2005 Express'>Enable remote connection to SQL Server 2005 Express</a></li>
<li><a href='http://www.linglom.com/2008/10/04/how-to-enable-remote-desktop-web-connection-on-windows/' rel='bookmark' title='Permanent Link: How to Enable Remote Desktop Web Connection on Windows'>How to Enable Remote Desktop Web Connection on Windows</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.linglom.com/2009/03/28/enable-remote-connection-on-sql-server-2008-express/feed/</wfw:commentRss>
		<slash:comments>118</slash:comments>
		</item>
		<item>
		<title>Optional parameters in Stored Procedure</title>
		<link>http://www.linglom.com/2008/09/12/optional-parameters-in-stored-procedure/</link>
		<comments>http://www.linglom.com/2008/09/12/optional-parameters-in-stored-procedure/#comments</comments>
		<pubDate>Fri, 12 Sep 2008 01:52:35 +0000</pubDate>
		<dc:creator>linglom</dc:creator>
				<category><![CDATA[MS SQL Server]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[Stored Procedure]]></category>

		<guid isPermaLink="false">http://www.linglom.com/?p=194</guid>
		<description><![CDATA[Introduction When you create a store procedure on Microsoft SQL Server with some input parameters, sometimes you don&#8217;t want to assign all of parameters (optional parameters) so you need to specify default value for the parameters. Otherwise, it shows error when execute the store procedure. Let&#8217;s see some example, I have created a new store [...]


No related posts.]]></description>
			<content:encoded><![CDATA[<div class="KonaBody"><h3>Introduction</h3>
<p>When you create a store procedure on Microsoft SQL Server with some input parameters, sometimes you don&#8217;t want to assign all of parameters (optional parameters) so you need to specify default value for the parameters. Otherwise, it shows error when execute the store procedure. </p>
<p><span id="more-194"></span></p>
<p><!--adsense#Square--></p>
<p>Let&#8217;s see some example, I have created a new store procedure to query product name from Northwind database which require 2 input parameters. The script to create a sample store procedure is below:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> PROCEDURE sp_getproduct
		<span style="color: #66cc66;">&#40;</span>@UnitPrice money<span style="color: #66cc66;">,</span>
		 @UnitsInStock smallint <span style="color: #66cc66;">=</span> <span style="color: #ff0000;">'10'</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">AS</span>
BEGIN
	<span style="color: #993333; font-weight: bold;">SET</span> NOCOUNT <span style="color: #993333; font-weight: bold;">ON</span>;
&nbsp;
	<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">&#91;</span>ProductName<span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">FROM</span> Products
		<span style="color: #993333; font-weight: bold;">WHERE</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span>UnitPrice<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&lt;</span> @UnitPrice<span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AND</span>
			  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span>UnitsInStock<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&gt;</span> @UnitsInStock<span style="color: #66cc66;">&#41;</span>
END
GO</pre></div></div>

<p><a href="http://www.linglom.com/images/Windows/Programming/SQLServer/OptionalSP/1.png" target="_blank"><img src="http://www.linglom.com/images/Windows/Programming/SQLServer/OptionalSP/_1.png" width="250" height="166" alt="" title=""  /></a></p>
<p>So as long as I specify both input parameters, the query is OK.<br />
<a href="http://www.linglom.com/images/Windows/Programming/SQLServer/OptionalSP/2.png" target="_blank"><img src="http://www.linglom.com/images/Windows/Programming/SQLServer/OptionalSP/_2.png" width="250" height="216" alt="" title=""  /></a></p>
<p>But if I miss an input parameter, the error will be shown as the figure below:<br />
<a href="http://www.linglom.com/images/Windows/Programming/SQLServer/OptionalSP/3.png" target="_blank"><img src="http://www.linglom.com/images/Windows/Programming/SQLServer/OptionalSP/_3.png" width="250" height="140" alt="" title=""  /></a></p>
<h3>Solution</h3>
<ol>
<li>You can define the default value for parameters to avoid this error message. The default value generally is NULL. But you also can define other value as you want.</li>
<li>In this example, I&#8217;ve altered the store procedure by assign default value for these parameters to be NULL and also in WHERE clause.

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">ALTER</span> PROCEDURE sp_getproduct
		<span style="color: #66cc66;">&#40;</span>@UnitPrice money <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
		 @UnitsInStock smallint <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">AS</span>
BEGIN
	<span style="color: #993333; font-weight: bold;">SET</span> NOCOUNT <span style="color: #993333; font-weight: bold;">ON</span>;
&nbsp;
	<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">&#91;</span>ProductName<span style="color: #66cc66;">&#93;</span> <span style="color: #993333; font-weight: bold;">FROM</span> Products
		<span style="color: #993333; font-weight: bold;">WHERE</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>@UnitPrice <span style="color: #993333; font-weight: bold;">IS</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">OR</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span>UnitPrice<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&lt;</span> @UnitPrice<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AND</span>
			  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>@UnitsInStock <span style="color: #993333; font-weight: bold;">IS</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">OR</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span>UnitsInStock<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&gt;</span> @UnitsInStock<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
END
GO</pre></div></div>

<p><strong>Code Explanation:</strong></p>
<ul>
<li>I&#8217;ve altered the parameters declaration to assign the NULL value as the default value.

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">		<span style="color: #66cc66;">&#40;</span>@UnitPrice money <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
		 @UnitsInStock smallint <span style="color: #66cc66;">=</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

</li>
<li>Next, I&#8217;ve altered in WHERE clause to be TRUE if the parameters are NULL. So if the both parameters are NULL, the SELECT statement will return all ProductName from Products table.

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;">		<span style="color: #993333; font-weight: bold;">WHERE</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>@UnitPrice <span style="color: #993333; font-weight: bold;">IS</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">OR</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span>UnitPrice<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&lt;</span> @UnitPrice<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">AND</span>
			  <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#40;</span>@UnitsInStock <span style="color: #993333; font-weight: bold;">IS</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">OR</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#91;</span>UnitsInStock<span style="color: #66cc66;">&#93;</span> <span style="color: #66cc66;">&gt;</span> @UnitsInStock<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

</li>
</ul>
<p><a href="http://www.linglom.com/images/Windows/Programming/SQLServer/OptionalSP/4.png" target="_blank"><img src="http://www.linglom.com/images/Windows/Programming/SQLServer/OptionalSP/_4.png" width="250" height="158" alt="" title=""  /></a></li>
<li>Re-execute the query again, you&#8217;ll see the error message is gone.<br />
<a href="http://www.linglom.com/images/Windows/Programming/SQLServer/OptionalSP/5.png" target="_blank"><img src="http://www.linglom.com/images/Windows/Programming/SQLServer/OptionalSP/_5.png" width="250" height="237" alt="" title=""  /></a></li>
</ol>
</div>

<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://www.linglom.com/2008/09/12/optional-parameters-in-stored-procedure/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>SQL Server 2005 Training, Day 1, Part II</title>
		<link>http://www.linglom.com/2008/02/29/sql-server-2005-training-day-1-part-ii/</link>
		<comments>http://www.linglom.com/2008/02/29/sql-server-2005-training-day-1-part-ii/#comments</comments>
		<pubDate>Fri, 29 Feb 2008 02:51:25 +0000</pubDate>
		<dc:creator>linglom</dc:creator>
				<category><![CDATA[MS SQL Server]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[SQL Server 2005]]></category>
		<category><![CDATA[traning]]></category>

		<guid isPermaLink="false">http://www.linglom.com/2008/02/29/sql-server-2005-training-day-1-part-ii/</guid>
		<description><![CDATA[Introduction This part contents mostly concerns coding on VB.NET which will interacts with SQL server on pubs database. The sample database can be download at SQL2000SampleDb. You do not need to complete Part I before. Topics Define a connection string Query table sales by year Call a store procedure Get return value from store procedure [...]


Related posts:<ol><li><a href='http://www.linglom.com/2007/09/22/sql-server-2005-training-day-1-part-i/' rel='bookmark' title='Permanent Link: SQL Server 2005 Training, Day 1, Part I'>SQL Server 2005 Training, Day 1, Part I</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div class="KonaBody"><h3>Introduction</h3>
<p>	This part contents mostly concerns coding on VB.NET which will interacts with SQL server on pubs database. The sample database can be download at <a href="http://www.linglom.com/downloads/sampleDB/SQL2000SampleDb.msi" target="_blank">SQL2000SampleDb</a>. You do not need to complete Part I before. </p>
<p><span id="more-52"></span></p>
<p><a name="top"></a></p>
<h3>Topics</h3>
<ol>
<li><a href="#1">Define a connection string</a></li>
<li><a href="#2">Query table sales by year</a></li>
<li><a href="#3">Call a store procedure</a></li>
<li><a href="#4">Get return value from store procedure</a></li>
<li><a href="#5">Get return values from store procedure</a></li>
<li><a href="#6">Using max, min, and average</a></li>
</ol>
<p><!--adsense#Square--></p>
<h3>Let’s start</h3>
<ol>
<a name="1"></a></p>
<li>
<h4>Define a connection string module</h4>
<p>Create new VB.NET Windows Application project and add new module to the project for add a connection string to SQL server. You need to alter this string to match your environment and keep database pointing to pubs.</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;">Module Module1
    <span style="color: #000080;">Public</span> Sqlcon <span style="color: #000080;">As</span> <span style="color: #000080;">String</span> = <span style="color: #800000;">&quot;Server=BKKSQL2005;uid=sa;password=123456;database=pubs&quot;</span>
<span style="color: #000080;">End</span> Module</pre></div></div>

<p><a href="http://www.linglom.com/images/howto/sqlServer/training/day1part2/1.png" target="_blank"><img src="http://www.linglom.com/images/howto/sqlServer/training/day1part2/_1.png" width="250" height="55" alt="A connection string" title="A connection string"  /></a>
</li>
<p><a href="#top">Back to top</a></p>
<p><a name="2"></a></p>
<li>
<h4>Query table sales by year</h4>
<p><strong>Design form1.vb</strong></p>
<ul>
<li>Back to form1.vb.</li>
<li>Add new Textbox as TextBox1, TextBox2, TextBox3, TextBox4 and TextBox5 to the form.</li>
<li>Add new Button as Button1 and Button2 to the form.</li>
<li>Add DataGridView to the form.</li>
<p><a href="http://www.linglom.com/images/howto/sqlServer/training/day1part2/2.png" target="_blank" target="_blank"><img src="http://www.linglom.com/images/howto/sqlServer/training/day1part2/_2.png" width="250" height="174" alt="Form1's Design" title="Form1's Design"  /></a>
</ul>
<p><strong>Coding form1.vb</strong></p>
<ul>
<li>Add code below on this form.</li>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;">Imports System.Data.SqlClient</pre></div></div>

<li>Double click Button1 and put below code into it.</li>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;">        <span style="color: #000080;">Dim</span> strsql <span style="color: #000080;">As</span> <span style="color: #000080;">String</span> = <span style="color: #800000;">&quot;select title_id &quot;</span>
        <span style="color: #000080;">For</span> i <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span> = <span style="color: #000080;">CInt</span>(TextBox1.Text) <span style="color: #000080;">To</span> <span style="color: #000080;">CInt</span>(TextBox2.Text)
            strsql &amp;= <span style="color: #800000;">&quot;, sum(case year(ord_date) when &quot;</span> &amp; i &amp; <span style="color: #800000;">&quot; then qty else 0 end) as Y&quot;</span> &amp; i
        <span style="color: #000080;">Next</span>
        strsql &amp;= <span style="color: #800000;">&quot; from sales group by title_id with rollup&quot;</span>
&nbsp;
        <span style="color: #000080;">Dim</span> DA <span style="color: #000080;">As</span> <span style="color: #000080;">New</span> SqlDataAdapter(strsql, Module1.Sqlcon)
        <span style="color: #000080;">Dim</span> DT <span style="color: #000080;">As</span> <span style="color: #000080;">New</span> DataTable
        DA.Fill(DT)
&nbsp;
        <span style="color: #000080;">Dim</span> str1 <span style="color: #000080;">As</span> <span style="color: #000080;">String</span> = <span style="color: #800000;">&quot;&quot;</span>
        <span style="color: #000080;">For</span> j <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span> = 1 <span style="color: #000080;">To</span> DT.Columns.Count - 1
            <span style="color: #000080;">Dim</span> dc <span style="color: #000080;">As</span> DataColumn = DT.Columns(j)
            <span style="color: #000080;">If</span> str1 &lt;&gt; <span style="color: #800000;">&quot;&quot;</span> <span style="color: #000080;">Then</span> str1 = str1 &amp; <span style="color: #800000;">&quot;+&quot;</span>
            str1 = str1 &amp; dc.ColumnName
        <span style="color: #000080;">Next</span>
&nbsp;
        DT.Columns.Add(<span style="color: #800000;">&quot;Sum1&quot;</span>, GetType(<span style="color: #000080;">Integer</span>), str1)
&nbsp;
        <span style="color: #000080;">Dim</span> dr <span style="color: #000080;">As</span> DataRow = DT.Rows(DT.Rows.Count - 1)
        dr(0) = <span style="color: #800000;">&quot;Total&quot;</span>
        DataGridView1.DataSource = DT</pre></div></div>

<li>Try compile and run the project, enter &#8217;1992&#8242; on TextBox1 as start year and &#8217;1994&#8242; on TextBox2 as end year and click the &#8216;View by Year&#8217; button. You’ll see the result as below.</li>
<p><a href="http://www.linglom.com/images/howto/sqlServer/training/day1part2/3.png" target="_blank"><img src="http://www.linglom.com/images/howto/sqlServer/training/day1part2/_3.png" width="250" height="166" alt="SQL result on DataGridView1" title="SQL result on DataGridView1"  /></a></p>
<li>You can download source code at here &#8211; <a href="http://www.linglom.com/downloads/sqlServerTraining/day1part2/TrainingSQL1.zip" target="_blank">TrainingSQL1.zip</a></li>
</ul>
</li>
<p><a href="#top">Back to top</a></p>
<p><a name="3"></a></p>
<li>
<h4>Call a store procedure</h4>
<p>This example will modify code from previous example and you need a store procedure ‘test_a’ which can be found in Part I or download sql to create a procedure at here &#8211; <a href="http://www.linglom.com/downloads/sqlServerTraining/day1part2/test_a.sql" target="_blank">test_a.sql</a></p>
<p>There are 3 different coding styles that will give the same result for calling store procedure. The example will try to call a store procedure ‘test_a’ which receive two inputs (start year and end year).</p>
<p>From previous Form1, modify Button1_Click event to be one of these code.</p>
<ol>
<li>Sending parameters along with execute string.

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;">        <span style="color: #000080;">Dim</span> strsql <span style="color: #000080;">As</span> <span style="color: #000080;">String</span> = <span style="color: #800000;">&quot;exec test_a '&quot;</span> &amp; TextBox1.Text &amp; <span style="color: #800000;">&quot;', '&quot;</span> &amp; TextBox2.Text &amp; <span style="color: #800000;">&quot;'&quot;</span>
        <span style="color: #000080;">Dim</span> DA <span style="color: #000080;">As</span> <span style="color: #000080;">New</span> SqlDataAdapter(strsql, Module1.Sqlcon)
        <span style="color: #000080;">Dim</span> DT <span style="color: #000080;">As</span> <span style="color: #000080;">New</span> DataTable
        DA.Fill(DT)
        DataGridView1.DataSource = DT</pre></div></div>

</li>
<li>Sending parameters by adding as SqlParameters.

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;">        <span style="color: #000080;">Dim</span> cn <span style="color: #000080;">As</span> <span style="color: #000080;">New</span> SqlConnection(Module1.Sqlcon)
        <span style="color: #000080;">Dim</span> cmd <span style="color: #000080;">As</span> <span style="color: #000080;">New</span> SqlCommand(<span style="color: #800000;">&quot;test_a&quot;</span>, cn)
        cmd.CommandType = CommandType.StoredProcedure
        <span style="color: #000080;">Dim</span> P1 <span style="color: #000080;">As</span> SqlParameter = cmd.Parameters.Add(<span style="color: #800000;">&quot;@Year1&quot;</span>, SqlDbType.NVarChar)
        P1.Value = TextBox1.Text
        P1 = cmd.Parameters.Add(<span style="color: #800000;">&quot;@Year2&quot;</span>, SqlDbType.NVarChar)
        P1.Value = TextBox2.Text
&nbsp;
        <span style="color: #000080;">Dim</span> DA <span style="color: #000080;">As</span> <span style="color: #000080;">New</span> SqlDataAdapter(cmd)
        <span style="color: #000080;">Dim</span> dt <span style="color: #000080;">As</span> <span style="color: #000080;">New</span> DataTable
        DA.Fill(dt)
        DataGridView1.DataSource = dt</pre></div></div>

</li>
<li>Using OleDB

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;">Imports System.Data.OleDb
.
.
        <span style="color: #000080;">Dim</span> cn <span style="color: #000080;">As</span> <span style="color: #000080;">New</span> OleDbConnection(<span style="color: #800000;">&quot;Provider=SQLOLEDB;&quot;</span> &amp; Module1.Sqlcon)
        <span style="color: #000080;">Dim</span> cmd <span style="color: #000080;">As</span> <span style="color: #000080;">New</span> OleDbCommand(<span style="color: #800000;">&quot;{call test_a (?,?)}&quot;</span>, cn)
        <span style="color: #000080;">Dim</span> P1 <span style="color: #000080;">As</span> OleDbParameter = cmd.Parameters.Add(<span style="color: #800000;">&quot;@Year1&quot;</span>, OleDbType.VarWChar)
        P1.Value = TextBox1.Text
        P1 = cmd.Parameters.Add(<span style="color: #800000;">&quot;@Year2&quot;</span>, OleDbType.VarWChar)
        P1.Value = TextBox2.Text
        <span style="color: #000080;">Dim</span> DA <span style="color: #000080;">As</span> <span style="color: #000080;">New</span> OleDbDataAdapter(cmd)
        <span style="color: #000080;">Dim</span> dt <span style="color: #000080;">As</span> <span style="color: #000080;">New</span> DataTable
        DA.Fill(dt)
        DataGridView1.DataSource = dt</pre></div></div>

</li>
</ol>
<p>The result will look like previous example. You can download the modified source code at here &#8211; <a href="http://www.linglom.com/downloads/sqlServerTraining/day1part2/TrainingSQL2.zip" target="_blank">TrainingSQL2.zip</a>.<br />
<a href="http://www.linglom.com/images/howto/sqlServer/training/day1part2/4.png" target="_blank"><img src="http://www.linglom.com/images/howto/sqlServer/training/day1part2/_4.png" width="250" height="166" alt="Execute 'test_ ' store procedure and result on DataGridView1" title="Execute 'test_a' store procedure and result on DataGridView1"  /></a>
</li>
<p><a href="#top">Back to top</a></p>
<p><a name="4"></a></p>
<li>
<h4>Get return value from store procedure</h4>
<p>There are 3 different coding styles as like in previous example. Modify source in Form1.Button1_Click method with the following code again.</p>
<ol>
<li>Sending parameters along with execute string

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;">        <span style="color: #000080;">Dim</span> strsql <span style="color: #000080;">As</span> <span style="color: #000080;">String</span> = <span style="color: #800000;">&quot;declare @i int exec @i = test_a '&quot;</span> &amp; TextBox1.Text &amp; <span style="color: #800000;">&quot;', '&quot;</span> &amp; TextBox2.Text &amp; <span style="color: #800000;">&quot;' select @i as num1&quot;</span>
        <span style="color: #000080;">Dim</span> DA <span style="color: #000080;">As</span> <span style="color: #000080;">New</span> SqlDataAdapter(strsql, Module1.Sqlcon)
        <span style="color: #000080;">Dim</span> DS <span style="color: #000080;">As</span> <span style="color: #000080;">New</span> DataSet
        DA.Fill(DS)
        DataGridView1.DataSource = DS.Tables(0)
        <span style="color: #000080;">Dim</span> i <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span> = DS.Tables(1).Rows(0)(<span style="color: #800000;">&quot;num1&quot;</span>)
        MsgBox(i)</pre></div></div>

</li>
<li>Sending parameters by using SqlParameter

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;">        <span style="color: #000080;">Dim</span> cn <span style="color: #000080;">As</span> <span style="color: #000080;">New</span> SqlConnection(Module1.Sqlcon)
        <span style="color: #000080;">Dim</span> cmd <span style="color: #000080;">As</span> <span style="color: #000080;">New</span> SqlCommand(<span style="color: #800000;">&quot;test_a&quot;</span>, cn)
        cmd.CommandType = CommandType.StoredProcedure
        <span style="color: #000080;">Dim</span> P2 <span style="color: #000080;">As</span> SqlParameter = cmd.Parameters.Add(<span style="color: #800000;">&quot;@Return&quot;</span>, SqlDbType.Int)
        P2.Direction = ParameterDirection.ReturnValue
        <span style="color: #000080;">Dim</span> P1 <span style="color: #000080;">As</span> SqlParameter = cmd.Parameters.Add(<span style="color: #800000;">&quot;@Year1&quot;</span>, SqlDbType.NVarChar)
        P1.Value = TextBox1.Text
        P1 = cmd.Parameters.Add(<span style="color: #800000;">&quot;@Year2&quot;</span>, SqlDbType.NVarChar)
        P1.Value = TextBox2.Text
        <span style="color: #000080;">Dim</span> DA <span style="color: #000080;">As</span> <span style="color: #000080;">New</span> SqlDataAdapter(cmd)
        <span style="color: #000080;">Dim</span> dt <span style="color: #000080;">As</span> <span style="color: #000080;">New</span> DataTable
        DA.Fill(dt)
        DataGridView1.DataSource = dt
        MsgBox(P2.Value)</pre></div></div>

</li>
<li>Using OleDB

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;">        <span style="color: #000080;">Dim</span> cn <span style="color: #000080;">As</span> <span style="color: #000080;">New</span> OleDbConnection(<span style="color: #800000;">&quot;Provider=SQLOLEDB;&quot;</span> &amp; Module1.Sqlcon)
        <span style="color: #000080;">Dim</span> cmd <span style="color: #000080;">As</span> <span style="color: #000080;">New</span> OleDbCommand(<span style="color: #800000;">&quot;{? = call test_a (?,?)}&quot;</span>, cn)
        <span style="color: #000080;">Dim</span> P2 <span style="color: #000080;">As</span> OleDbParameter = cmd.Parameters.Add(<span style="color: #800000;">&quot;@Return&quot;</span>, OleDbType.<span style="color: #000080;">Integer</span>)
        P2.Direction = ParameterDirection.ReturnValue
        <span style="color: #000080;">Dim</span> P1 <span style="color: #000080;">As</span> OleDbParameter = cmd.Parameters.Add(<span style="color: #800000;">&quot;@Year1&quot;</span>, OleDbType.VarWChar)
        P1.Value = TextBox1.Text
        P1 = cmd.Parameters.Add(<span style="color: #800000;">&quot;@Year2&quot;</span>, OleDbType.VarWChar)
        P1.Value = TextBox2.Text
        <span style="color: #000080;">Dim</span> DA <span style="color: #000080;">As</span> <span style="color: #000080;">New</span> OleDbDataAdapter(cmd)
        <span style="color: #000080;">Dim</span> dt <span style="color: #000080;">As</span> <span style="color: #000080;">New</span> DataTable
        DA.Fill(dt)
        DataGridView1.DataSource = dt
        MsgBox(P2.Value)</pre></div></div>

</li>
</ol>
<p>The result will look like previous example but add a pop-up message box the return value from the store procedure. You can download the source code at here &#8211; <a href="http://www.linglom.com/downloads/sqlServerTraining/day1part2/TrainingSQL3.zip" target="_blank">TrainingSQL3.zip</a>.<br />
<a href="http://www.linglom.com/images/howto/sqlServer/training/day1part2/5.png" target="_blank"><img src="http://www.linglom.com/images/howto/sqlServer/training/day1part2/_5.png" width="250" height="166" alt="Execute 'test_a' store procedure and result on DataGridView1 with return value" title="Execute 'test_a' store procedure and result on DataGridView1 with return value"  /></a>
</li>
<p><a href="#top">Back to top</a></p>
<p><a name="5"></a></p>
<li>
<h4>Get return values from store procedure</h4>
<p>This example, you need a store procedure ‘test_a2’ which can be found here &#8211; <a href="http://www.linglom.com/downloads/sqlServerTraining/day1part2/test_a2.sql" target="_blank">test_a2.sql</a>. This store procedure receives two inputs as the store procedure &#8216;test_a&#8217; but it returns more additional values which are @sum1, @avg1 and a return value.<br />
There are 3 different coding styles as like in previous example. Modify source in Form1.Button1_Click method with the following code again. The additional values that are returned from the store procedure will be place into TextBox3, TextBox4 and TextBox5.</p>
<ol>
<li>Sending parameters along with execute string

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;">        <span style="color: #000080;">Dim</span> strsql <span style="color: #000080;">As</span> <span style="color: #000080;">String</span> = <span style="color: #800000;">&quot;declare @i int,@j int, @k int exec @i = test_a2 '&quot;</span> &amp; TextBox1.Text &amp; <span style="color: #800000;">&quot;', '&quot;</span> &amp; TextBox2.Text &amp; <span style="color: #800000;">&quot;',@j out, @k out select @i as num1,@j,@k&quot;</span>
        <span style="color: #000080;">Dim</span> DA <span style="color: #000080;">As</span> <span style="color: #000080;">New</span> SqlDataAdapter(strsql, Module1.Sqlcon)
        <span style="color: #000080;">Dim</span> DS <span style="color: #000080;">As</span> <span style="color: #000080;">New</span> DataSet
        DA.Fill(DS)
        DataGridView1.DataSource = DS.Tables(0)
        <span style="color: #000080;">Dim</span> dr <span style="color: #000080;">As</span> DataRow = DS.Tables(1).Rows(0)
        TextBox3.Text = dr(0)
        TextBox4.Text = dr(1)
        TextBox5.Text = dr(2)</pre></div></div>

</li>
<li>Sending parameters by using SqlParameter<br />
<em><strong>Note:</strong></em> The parameter&#8217;s name must match as in the store procedure.</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;">        <span style="color: #000080;">Dim</span> cn <span style="color: #000080;">As</span> <span style="color: #000080;">New</span> SqlConnection(Module1.Sqlcon)
        <span style="color: #000080;">Dim</span> cmd <span style="color: #000080;">As</span> <span style="color: #000080;">New</span> SqlCommand(<span style="color: #800000;">&quot;test_a2&quot;</span>, cn)
        cmd.CommandType = CommandType.StoredProcedure
        <span style="color: #000080;">Dim</span> P2 <span style="color: #000080;">As</span> SqlParameter = cmd.Parameters.Add(<span style="color: #800000;">&quot;@Return&quot;</span>, SqlDbType.Int)
        P2.Direction = ParameterDirection.ReturnValue
        <span style="color: #000080;">Dim</span> P1 <span style="color: #000080;">As</span> SqlParameter = cmd.Parameters.Add(<span style="color: #800000;">&quot;@Year1&quot;</span>, SqlDbType.NVarChar)
        P1.Value = TextBox1.Text
        P1 = cmd.Parameters.Add(<span style="color: #800000;">&quot;@Year2&quot;</span>, SqlDbType.NVarChar)
        P1.Value = TextBox2.Text
        <span style="color: #000080;">Dim</span> P3 <span style="color: #000080;">As</span> SqlParameter = cmd.Parameters.Add(<span style="color: #800000;">&quot;@sum1&quot;</span>, SqlDbType.Int)
        P3.Direction = ParameterDirection.<span style="color: #000080;">Output</span>
        <span style="color: #000080;">Dim</span> P4 <span style="color: #000080;">As</span> SqlParameter = cmd.Parameters.Add(<span style="color: #800000;">&quot;@avg1&quot;</span>, SqlDbType.Int)
        P4.Direction = ParameterDirection.<span style="color: #000080;">Output</span>
        <span style="color: #000080;">Dim</span> DA <span style="color: #000080;">As</span> <span style="color: #000080;">New</span> SqlDataAdapter(cmd)
        <span style="color: #000080;">Dim</span> dt <span style="color: #000080;">As</span> <span style="color: #000080;">New</span> DataTable
        DA.Fill(dt)
        DataGridView1.DataSource = dt
        TextBox3.Text = P2.Value
        TextBox4.Text = P3.Value
        TextBox5.Text = P4.Value</pre></div></div>

</li>
<li>Using OleDB

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;">        <span style="color: #000080;">Dim</span> cn <span style="color: #000080;">As</span> <span style="color: #000080;">New</span> OleDbConnection(<span style="color: #800000;">&quot;Provider=SQLOLEDB;&quot;</span> &amp; Module1.Sqlcon)
        <span style="color: #000080;">Dim</span> cmd <span style="color: #000080;">As</span> <span style="color: #000080;">New</span> OleDbCommand(<span style="color: #800000;">&quot;{? = call test_a2 (?,?,?,?)}&quot;</span>, cn)
        <span style="color: #000080;">Dim</span> P2 <span style="color: #000080;">As</span> OleDbParameter = cmd.Parameters.Add(<span style="color: #800000;">&quot;@Return&quot;</span>, OleDbType.<span style="color: #000080;">Integer</span>)
        P2.Direction = ParameterDirection.ReturnValue
        <span style="color: #000080;">Dim</span> P1 <span style="color: #000080;">As</span> OleDbParameter = cmd.Parameters.Add(<span style="color: #800000;">&quot;@Year1&quot;</span>, OleDbType.VarWChar)
        P1.Value = TextBox1.Text
        P1 = cmd.Parameters.Add(<span style="color: #800000;">&quot;@Year2&quot;</span>, OleDbType.VarWChar)
        P1.Value = TextBox2.Text
        <span style="color: #000080;">Dim</span> P3 <span style="color: #000080;">As</span> OleDbParameter = cmd.Parameters.Add(<span style="color: #800000;">&quot;@sum1&quot;</span>, OleDbType.<span style="color: #000080;">Integer</span>)
        P3.Direction = ParameterDirection.<span style="color: #000080;">Output</span>
        <span style="color: #000080;">Dim</span> P4 <span style="color: #000080;">As</span> OleDbParameter = cmd.Parameters.Add(<span style="color: #800000;">&quot;@avg1&quot;</span>, OleDbType.<span style="color: #000080;">Integer</span>)
        P4.Direction = ParameterDirection.<span style="color: #000080;">Output</span>
        <span style="color: #000080;">Dim</span> DA <span style="color: #000080;">As</span> <span style="color: #000080;">New</span> OleDbDataAdapter(cmd)
        <span style="color: #000080;">Dim</span> dt <span style="color: #000080;">As</span> <span style="color: #000080;">New</span> DataTable
        DA.Fill(dt)
        DataGridView1.DataSource = dt
        TextBox3.Text = P2.Value
        TextBox4.Text = P3.Value
        TextBox5.Text = P4.Value</pre></div></div>

</li>
</ol>
<p>The result will look like previous example but add a pop-up message box the return value from the store procedure. You can download the source code at here &#8211; <a href="http://www.linglom.com/downloads/sqlServerTraining/day1part2/TrainingSQL4.zip" target="_blank">TrainingSQL4.zip</a>.<br />
<a href="http://www.linglom.com/images/howto/sqlServer/training/day1part2/6.png" target="_blank"><img src="http://www.linglom.com/images/howto/sqlServer/training/day1part2/_6.png" width="250" height="166" alt="Execute 'test_a2' store procedure with multiple return values" title="Execute 'test_a2' store procedure with multiple return values"  /></a>
</li>
<p><a href="#top">Back to top</a></p>
<p><a name="6"></a></p>
<li>
<h4>Using max, min, and average</h4>
<p>This example shows how to using function max, min and average by compute from qty field on sales table. </p>
<p><strong>Design form2.vb</strong></p>
<ul>
<li>Add new form on the project.</li>
<li>Add new ListBox1 to the form.</li>
<li>Add DataGridView1, DataGridView2 and DataGridView3 to the form.</li>
<p><a href="http://www.linglom.com/images/howto/sqlServer/training/day1part2/7.png" target="_blank"><img src="http://www.linglom.com/images/howto/sqlServer/training/day1part2/_7.png" width="250" height="165" alt="Form2's design" title="Form2's design"  /></a>
</ul>
<p><strong>Coding form2.vb</strong><br />
Add below code to the form.</p>

<div class="wp_syntax"><div class="code"><pre class="vb" style="font-family:monospace;">Imports System.Data.SqlClient
&nbsp;
<span style="color: #000080;">Public</span> Class Form2
    <span style="color: #000080;">Dim</span> ds <span style="color: #000080;">As</span> <span style="color: #000080;">New</span> DataSet
    <span style="color: #000080;">Private</span> <span style="color: #000080;">Sub</span> Form2_Load(<span style="color: #000080;">ByVal</span> sender <span style="color: #000080;">As</span> System.<span style="color: #000080;">Object</span>, <span style="color: #000080;">ByVal</span> e <span style="color: #000080;">As</span> System.EventArgs) Handles MyBase.Load
        <span style="color: #000080;">Dim</span> strsql <span style="color: #000080;">As</span> <span style="color: #000080;">String</span> = <span style="color: #800000;">&quot;select stor_id,ord_num, qty from sales order by stor_id &quot;</span> &amp; _
                            <span style="color: #800000;">&quot; compute max(qty),min(qty), avg(qty) by stor_id&quot;</span>
        <span style="color: #000080;">Dim</span> da <span style="color: #000080;">As</span> <span style="color: #000080;">New</span> SqlDataAdapter(strsql, Module1.Sqlcon)
&nbsp;
        da.Fill(ds)
        <span style="color: #008000;">'MsgBox(ds.Tables.Count)
</span>        <span style="color: #000080;">For</span> <span style="color: #000080;">Each</span> dt <span style="color: #000080;">As</span> DataTable <span style="color: #000080;">In</span> ds.Tables
            ListBox1.Items.Add(dt.TableName)
        <span style="color: #000080;">Next</span>
        <span style="color: #000080;">Dim</span> dt3 <span style="color: #000080;">As</span> DataTable = ds.Tables(0).Clone
        <span style="color: #000080;">Dim</span> dt4 <span style="color: #000080;">As</span> DataTable = ds.Tables(1).Clone
        dt4.Columns.Add(<span style="color: #800000;">&quot;Stor_id&quot;</span>, GetType(<span style="color: #000080;">Integer</span>))
        <span style="color: #000080;">For</span> i <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span> = 0 <span style="color: #000080;">To</span> ds.Tables.Count - 1 <span style="color: #000080;">Step</span> 2
            <span style="color: #000080;">Dim</span> dt <span style="color: #000080;">As</span> DataTable = ds.Tables(i)
            <span style="color: #000080;">Dim</span> Stor_ID <span style="color: #000080;">As</span> <span style="color: #000080;">Integer</span> = dt.Rows(0)(<span style="color: #800000;">&quot;stor_id&quot;</span>)
            <span style="color: #000080;">For</span> <span style="color: #000080;">Each</span> dr <span style="color: #000080;">As</span> DataRow <span style="color: #000080;">In</span> dt.Rows
                <span style="color: #008000;">'dt3.ImportRow(dr)
</span>                <span style="color: #000080;">Dim</span> dr2 <span style="color: #000080;">As</span> DataRow = dt3.NewRow
                dr2.ItemArray = dr.ItemArray
                dt3.Rows.Add(dr2)
            <span style="color: #000080;">Next</span>
&nbsp;
            <span style="color: #000080;">Dim</span> dr5 <span style="color: #000080;">As</span> DataRow = ds.Tables(i + 1).Rows(0)
            dt4.ImportRow(dr5)
            <span style="color: #000080;">Dim</span> dr4 <span style="color: #000080;">As</span> DataRow = dt4.Rows(dt4.Rows.Count - 1)
            dr4(<span style="color: #800000;">&quot;stor_id&quot;</span>) = Stor_ID
&nbsp;
        <span style="color: #000080;">Next</span>
        DataGridView2.DataSource = dt3
        DataGridView3.DataSource = dt4
&nbsp;
&nbsp;
    <span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span>
&nbsp;
    <span style="color: #000080;">Private</span> <span style="color: #000080;">Sub</span> ListBox1_SelectedIndexChanged(<span style="color: #000080;">ByVal</span> sender <span style="color: #000080;">As</span> System.<span style="color: #000080;">Object</span>, <span style="color: #000080;">ByVal</span> e <span style="color: #000080;">As</span> System.EventArgs) Handles ListBox1.SelectedIndexChanged
        <span style="color: #000080;">Dim</span> DT <span style="color: #000080;">As</span> DataTable
        DT = ds.Tables(ListBox1.Text)
        DataGridView1.DataSource = DT
    <span style="color: #000080;">End</span> <span style="color: #000080;">Sub</span>
<span style="color: #000080;">End</span> Class</pre></div></div>

<p>Change startup form on the project by right-click on the project&#8217;s name and select properties and change startup form from &#8216;Form1&#8242; to &#8216;Form2&#8242;. Compile and run the project.<br />
<a href="http://www.linglom.com/images/howto/sqlServer/training/day1part2/8.png" target="_blank"><img src="http://www.linglom.com/images/howto/sqlServer/training/day1part2/_8.png" width="250" height="181" alt="Change startup form" title="Change startup form"  /></a></p>
<p>You&#8217;ll see that on DataGridView1 will be changed when you change value on ListBox1. On DataGridView2 shows result from sql query command and DataGridView3 shows computed values (max, min and average) of each stor_id. You can download the source code at here &#8211; <a href="http://www.linglom.com/downloads/sqlServerTraining/day1part2/TrainingSQL5.zip" target="_blank">TrainingSQL5.zip</a>.<br />
<a href="http://www.linglom.com/images/howto/sqlServer/training/day1part2/9.png" target="_blank"><img src="http://www.linglom.com/images/howto/sqlServer/training/day1part2/_9.png" width="250" height="165" alt="Result of compute functions" title="Result of compute functions"  /></a></p>
<p><a href="#top">Back to top</a>
</li>
</ol>
</div>

<p>Related posts:<ol><li><a href='http://www.linglom.com/2007/09/22/sql-server-2005-training-day-1-part-i/' rel='bookmark' title='Permanent Link: SQL Server 2005 Training, Day 1, Part I'>SQL Server 2005 Training, Day 1, Part I</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.linglom.com/2008/02/29/sql-server-2005-training-day-1-part-ii/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
<enclosure url="http://www.linglom.com/downloads/sqlServerTraining/day1part2/test_a.sql" length="1390" type="audio/mpeg" />
<enclosure url="http://www.linglom.com/downloads/sqlServerTraining/day1part2/test_a2.sql" length="1778" type="audio/mpeg" />
		</item>
		<item>
		<title>How to backup and restore database on Microsoft SQL Server 2005</title>
		<link>http://www.linglom.com/2008/01/12/how-to-backup-and-restore-database-on-microsoft-sql-server-2005/</link>
		<comments>http://www.linglom.com/2008/01/12/how-to-backup-and-restore-database-on-microsoft-sql-server-2005/#comments</comments>
		<pubDate>Fri, 11 Jan 2008 17:59:13 +0000</pubDate>
		<dc:creator>linglom</dc:creator>
				<category><![CDATA[MS SQL Server]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[Backup]]></category>
		<category><![CDATA[Restore]]></category>
		<category><![CDATA[SQL Server 2005]]></category>

		<guid isPermaLink="false">http://www.linglom.com/2008/01/12/how-to-backup-and-restore-database-on-microsoft-sql-server-2005/</guid>
		<description><![CDATA[Introduction This post shows a step-by-step guide to backup and restore a database between two Microsoft SQL Server 2005 instances. By using backup, you can backup a database without interrupt any transactions on the database. In the example below, I will backup a database from SQL Server 2005 and restore the database to another SQL [...]


Related posts:<ol><li><a href='http://www.linglom.com/2009/08/14/restore-database-from-sql-server-2008-to-sql-server-2005-part-2-generate-sql-server-scripts-wizard/' rel='bookmark' title='Permanent Link: Restore Database From SQL Server 2008 to SQL Server 2005, Part 2: Generate SQL Server Scripts Wizard'>Restore Database From SQL Server 2008 to SQL Server 2005, Part 2: Generate SQL Server Scripts Wizard</a></li>
<li><a href='http://www.linglom.com/2009/08/17/restore-database-from-sql-server-2008-to-sql-server-2005-part-3-export-data-wizard/' rel='bookmark' title='Permanent Link: Restore Database From SQL Server 2008 to SQL Server 2005, Part 3: Export Data Wizard'>Restore Database From SQL Server 2008 to SQL Server 2005, Part 3: Export Data Wizard</a></li>
<li><a href='http://www.linglom.com/2009/08/11/restore-database-from-sql-server-2008-to-sql-server-2005-part-1-introduction/' rel='bookmark' title='Permanent Link: Restore Database From SQL Server 2008 to SQL Server 2005, Part 1: Introduction'>Restore Database From SQL Server 2008 to SQL Server 2005, Part 1: Introduction</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div class="KonaBody"><h3>Introduction</h3>
<p>This post shows a step-by-step guide to backup and restore a database between two Microsoft SQL Server 2005 instances. By using backup, you can backup a database without interrupt any transactions on the database.<br />
In the example below, I will backup a database from SQL Server 2005 and restore the database to another SQL Server 2005 Express Edition.</p>
<p><span id="more-66"></span></p>
<p><!--adsense#Square--></p>
<h3>Step-by-step</h3>
<h4>Backup a database.</h4>
<p>Now I will backup AdventureWorks database on BKKSQL2005 which runs Microsoft SQL Server 2005 to a file.</p>
<ol>
<li>Connect to source server. Open Microsoft SQL Server Management Studio and connect to BKKSQL2005.</li>
<li>Right-click on the AdventureWorks database. Select Tasks -> Backup&#8230;<br />
<img src="http://www.linglom.com/images/howto/sqlServer/backup/1.png" width="454" height="533" alt="Backup a SQL Server database" title="Backup a SQL Server database" /></li>
<li>On Back Up Database window, you can configure about backup information. If you&#8217;re not familiar these configurations, you can leave default values. Here are some short descriptions.
<ol>
<li>Database &#8211; a database that you want to backup.</li>
<li>Backup type &#8211; you can select 2 options: Full and Differential. If this is the first time you backup the database, you must select Full. </li>
<li>Name &#8211; Name of this backup, you can name anything as you want.</li>
<li>Destination &#8211; the file that will be backup to. You can leave as default. Default will backup to &#8220;C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Backup&#8221;.</li>
<li>Click OK to proceed backup.</li>
</ol>
<p><img src="http://www.linglom.com/images/howto/sqlServer/backup/2.png" width="550" height="494" alt="Select source and destination to backup" title="Select source and destination to backup" />
</li>
<li>Wait for a while and you&#8217;ll see a pop-up message when backup is finished.<br />
<img src="http://www.linglom.com/images/howto/sqlServer/backup/3.png" width="550" height="112" alt="Backup success pop-up message" title="Backup success pop-up message" /></li>
<li>Browse to the destination, you&#8217;ll see a backup file (.bak format) which you can copy to other server for restore in the next step. Default backup directory is &#8220;C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Backup&#8221;.<br />
<img src="http://www.linglom.com/images/howto/sqlServer/backup/4.png" width="402" height="140" alt="The backup file from SQL Server 2005 Server" title="The backup file from SQL Server 2005 Server" /></li>
</ol>
<h4>Restore the database.</h4>
<p>Next, I will restore the AdventureWorks database from a file that I&#8217;ve created above to BK01BIZ001 which runs Microsoft SQL Server Express Edition.</p>
<ol>
<li>Copy the backup file from source server to destination server. I&#8217;ve copied into the same directory as source server.<br />
<img src="http://www.linglom.com/images/howto/sqlServer/backup/8.png" width="403" height="146" alt="The backup file" title="The backup file" /></li>
<li>Connect to destination server. Open Microsoft SQL Server Management Studio Express and connect to BK01BIZ001.</li>
<li>Right-click on Databases. Select Restore Database&#8230;<br />
<img src="http://www.linglom.com/images/howto/sqlServer/backup/5.png" width="345" height="361" alt="Restore the database" title="Restore the database" /></li>
<li>Restore Database window appears. On Source for restore, select From device and click [...] buttton to browse file.<img src="http://www.linglom.com/images/howto/sqlServer/backup/6.png" width="550" height="494" alt="Select source device" title="Select source device" /></li>
<li>On Specify Backup, ensure that Backup media is &#8220;File&#8221; and click Add.<br />
<img src="http://www.linglom.com/images/howto/sqlServer/backup/7.png" width="492" height="353" alt="Select the backup media to restore" title="Select the backup media to restore" /></li>
<li>On Locate Backup File, select the backup file. This is the backup file that was created in Backup a database section and was copied to this server. Click OK. OK.<br />
<img src="http://www.linglom.com/images/howto/sqlServer/backup/9.png" width="427" height="604" alt="Select the backup file to restore" title="Select the backup file to restore" /></li>
<li>Back to Restore Database window.
<ol>
<li>On Destination for restore, select &#8220;AdventureWorks&#8221;.<br />
<em>Note: If you haven&#8217;t added the backup file on Source before (step 4-6), you won&#8217;t see the database name on Destination</em>.</li>
<li>On Source for restore, check the box in front of the backup name (in Restore column). </li>
<li>Click OK.</li>
</ol>
<p><img src="http://www.linglom.com/images/howto/sqlServer/backup/10.png" width="550" height="494" alt="Select a destination database to restore" title="Select a destination database to restore" />
</li>
<li>Wait until restore finish and there&#8217;ll be a pop-up message notify.<br />
<img src="http://www.linglom.com/images/howto/sqlServer/backup/11.png" width="550" height="112" alt="Restore success pop-up message" title="Restore success pop-up message" /></li>
<li>Now you&#8217;ll see the restored database on the destination SQL Server.<br />
<img src="http://www.linglom.com/images/howto/sqlServer/backup/12.png" width="312" height="216" alt="The restored database on SQL Server 2005" title="The restored database on SQL Server 2005" /></li>
</ol>
</div>

<p>Related posts:<ol><li><a href='http://www.linglom.com/2009/08/14/restore-database-from-sql-server-2008-to-sql-server-2005-part-2-generate-sql-server-scripts-wizard/' rel='bookmark' title='Permanent Link: Restore Database From SQL Server 2008 to SQL Server 2005, Part 2: Generate SQL Server Scripts Wizard'>Restore Database From SQL Server 2008 to SQL Server 2005, Part 2: Generate SQL Server Scripts Wizard</a></li>
<li><a href='http://www.linglom.com/2009/08/17/restore-database-from-sql-server-2008-to-sql-server-2005-part-3-export-data-wizard/' rel='bookmark' title='Permanent Link: Restore Database From SQL Server 2008 to SQL Server 2005, Part 3: Export Data Wizard'>Restore Database From SQL Server 2008 to SQL Server 2005, Part 3: Export Data Wizard</a></li>
<li><a href='http://www.linglom.com/2009/08/11/restore-database-from-sql-server-2008-to-sql-server-2005-part-1-introduction/' rel='bookmark' title='Permanent Link: Restore Database From SQL Server 2008 to SQL Server 2005, Part 1: Introduction'>Restore Database From SQL Server 2008 to SQL Server 2005, Part 1: Introduction</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.linglom.com/2008/01/12/how-to-backup-and-restore-database-on-microsoft-sql-server-2005/feed/</wfw:commentRss>
		<slash:comments>54</slash:comments>
		</item>
	</channel>
</rss>
