<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>remidian.com &#187; mssql</title>
	<atom:link href="http://remidian.com/category/mssql/feed" rel="self" type="application/rss+xml" />
	<link>http://remidian.com</link>
	<description>Braindumps of Remi Visser, freelance Oracle DBA.</description>
	<lastBuildDate>Mon, 10 May 2010 12:33:05 +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>Restore SQL Server database with TSQL</title>
		<link>http://remidian.com/mssql/restore-sql-server-database-with-tsql.html</link>
		<comments>http://remidian.com/mssql/restore-sql-server-database-with-tsql.html#comments</comments>
		<pubDate>Wed, 25 Mar 2009 11:31:56 +0000</pubDate>
		<dc:creator>remivisser</dc:creator>
				<category><![CDATA[T-SQL]]></category>
		<category><![CDATA[mssql]]></category>

		<guid isPermaLink="false">http://remidian.com/?p=98</guid>
		<description><![CDATA[Recently I had troubles restoring a database in my SQL Server 2005 instance (SP3). I used the &#8216;wizard&#8217; in the SQL Server Enterprise manager but after I got the message &#8216;restore completed successfully&#8217; the database hung in &#8216;Restoring&#8217; state, blocking connections. I decided the try to restore the database with T-SQL, with T-SQL the restore [...]]]></description>
			<content:encoded><![CDATA[<p>
Recently I had troubles restoring a database in my SQL Server 2005 instance (SP3). I used the &#8216;wizard&#8217; in the SQL Server Enterprise manager but after I got the message &#8216;restore completed successfully&#8217; the database hung in &#8216;Restoring&#8217; state, blocking connections.<span id="more-98"></span>
</p>
<p>
I decided the try to restore the database with T-SQL, with T-SQL the restore completed successfully! Another advantage of using T-SQL is that T-SQL gave more logging. This logging shows me that the BAK file (delivered from a third party) is not a SQL Server 2005 backup but presumably a SQL Server 2000 database backup &#8230;
</p>
<pre>
DECLARE
&nbsp;&nbsp;@BackupFile varchar(8000),
&nbsp;&nbsp;@sql varchar(8000)
&nbsp;&nbsp;SET @BackupFile = &#039;E:\DB2000.BAK&#039;
&nbsp;&nbsp;SET @sql = &#039;RESTORE DATABASE ADB01 FROM DISK = &#039;&#039;&#039; + @backupfile + &#039;&#039;&#039; WITH FILE = 1,&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;MOVE N&#039;&#039;DB2000_dat&#039;&#039; TO N&#039;&#039;E:\mssql\data\ADB01\ADB01_01.mdf&#039;&#039;,&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;MOVE N&#039;&#039;DB2000_log&#039;&#039; TO N&#039;&#039;F:\mssql\tlog\ADB01\ADB01_01.ldf&#039;&#039;,&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;NOUNLOAD,&nbsp;&nbsp;REPLACE,&nbsp;&nbsp;STATS = 10, RECOVERY&#039;
&nbsp;&nbsp;exec (@sql)
go
&nbsp;
10 percent processed.
20 percent processed.
30 percent processed.
40 percent processed.
50 percent processed.
60 percent processed.
70 percent processed.
80 percent processed.
90 percent processed.
100 percent processed.
Processed 2792 pages for database &#039;ADB01&#039;, file &#039;DB2000_dat&#039; on file 1.
Processed 1 pages for database &#039;ADB01&#039;, file &#039;DB2000_log&#039; on file 1.
Converting database &#039;ADB01&#039; from version 539 to the current version 611.
Database &#039;ADB01&#039; running the upgrade step from version 539 to version 551.
Database &#039;ADB01&#039; running the upgrade step from version 551 to version 552.
Database &#039;ADB01&#039; running the upgrade step from version 552 to version 553.
Database &#039;ADB01&#039; running the upgrade step from version 553 to version 554.
Database &#039;ADB01&#039; running the upgrade step from version 554 to version 589.
Database &#039;ADB01&#039; running the upgrade step from version 589 to version 590.
Database &#039;ADB01&#039; running the upgrade step from version 590 to version 593.
Database &#039;ADB01&#039; running the upgrade step from version 593 to version 597.
Database &#039;ADB01&#039; running the upgrade step from version 597 to version 604.
Database &#039;ADB01&#039; running the upgrade step from version 604 to version 605.
Database &#039;ADB01&#039; running the upgrade step from version 605 to version 606.
Database &#039;ADB01&#039; running the upgrade step from version 606 to version 607.
Database &#039;ADB01&#039; running the upgrade step from version 607 to version 608.
Database &#039;ADB01&#039; running the upgrade step from version 608 to version 609.
Database &#039;ADB01&#039; running the upgrade step from version 609 to version 610.
Database &#039;ADB01&#039; running the upgrade step from version 610 to version 611.
RESTORE DATABASE successfully processed 2793 pages in 1.513 seconds (15.118 MB/sec).
&nbsp;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://remidian.com/mssql/restore-sql-server-database-with-tsql.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>osql switches</title>
		<link>http://remidian.com/mssql/osql.html</link>
		<comments>http://remidian.com/mssql/osql.html#comments</comments>
		<pubDate>Wed, 30 Jan 2008 15:01:52 +0000</pubDate>
		<dc:creator>remivisser</dc:creator>
				<category><![CDATA[mssql]]></category>

		<guid isPermaLink="false">http://remidian.com/mssql/osql.html</guid>
		<description><![CDATA[Some osql switches. Specify non default tcp ports C:\&#62;osql -Stcp:&#60;hostname&#62;,&#60;portname&#62;[\&#60;instance_name&#62;] -E 1&#62; See http://support.microsoft.com/kb/313295]]></description>
			<content:encoded><![CDATA[<p>
Some osql switches.<span id="more-71"></span>
</p>
<h3>
Specify non default tcp ports<br />
</h3>
<pre>
C:\&gt;osql -Stcp:&lt;hostname&gt;,&lt;portname&gt;[\&lt;instance_name&gt;] -E
1&gt;
</pre>
<p>
See http://support.microsoft.com/kb/313295</p>
]]></content:encoded>
			<wfw:commentRss>http://remidian.com/mssql/osql.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Remove SQL Server database from single-user mode</title>
		<link>http://remidian.com/mssql/remove-sql-server-database-from-single-user-mode.html</link>
		<comments>http://remidian.com/mssql/remove-sql-server-database-from-single-user-mode.html#comments</comments>
		<pubDate>Wed, 23 Jan 2008 12:27:53 +0000</pubDate>
		<dc:creator>remivisser</dc:creator>
				<category><![CDATA[mssql]]></category>

		<guid isPermaLink="false">http://remidian.com/mssql/remove-sql-server-database-from-single-user-mode.html</guid>
		<description><![CDATA[Braindump of a session where I had to remove a SQL Server database from single-user mode. execute sp_dboption 1&#62; exec sp_dboption &#039;testdb01&#039;, &#039;single user&#039;, &#039;FALSE&#039;; 2&#62; go Msg 5064, Level 16, State 1, Server REMIDIAN01, Line 1 Changes to the state or options of database &#039;testdb01&#039; cannot be made at this time. The database is [...]]]></description>
			<content:encoded><![CDATA[<p>
Braindump of a session where I had to remove a SQL Server database from single-user mode.<span id="more-69"></span>
</p>
<h6>execute sp_dboption</h6>
<pre>
1&gt; exec sp_dboption &#039;testdb01&#039;, &#039;single user&#039;, &#039;FALSE&#039;;
2&gt; go
Msg 5064, Level 16, State 1, Server REMIDIAN01, Line 1
Changes to the state or options of database &#039;testdb01&#039; cannot be made at this
time. The database is in single-user mode, and a user is currently connected to
it.
Msg 5069, Level 16, State 1, Server REMIDIAN01, Line 1
ALTER DATABASE statement failed.
sp_dboption command failed.
</pre>
<p>
Now I can use TSQL below to find all the database sesions connected to this database.
</p>
<h6>
TSQL script to retrieve client sessions per database.<br />
</h6>
<pre>
select d.name, d.dbid, spid, login_time, nt_domain, nt_username, loginame
&nbsp;&nbsp;from sysprocesses p inner join sysdatabases d on p.dbid = d.dbid
 where d.name = &#039;testdb01&#039;
go
&nbsp;
</pre>
<p>
Kill the particular session(s) with the ;kill&#8217; command.
</p>
<pre>
1&gt; kill 51
2&gt; go
1&gt;
</pre>
<p>
Now I can &#8216;remove&#8217; the database from Sinlge user mode.
</p>
<pre>
1&gt; exec sp_dboption &#039;testdb01&#039;, &#039;single user&#039;, &#039;FALSE&#039;
2&gt; go
1&gt;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://remidian.com/mssql/remove-sql-server-database-from-single-user-mode.html/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Microsoft SQL Server 2005 JDBC Driver (oracle GRID)</title>
		<link>http://remidian.com/mssql/microsoft-sql-server-2005-jdbc-driver-oracle-grid.html</link>
		<comments>http://remidian.com/mssql/microsoft-sql-server-2005-jdbc-driver-oracle-grid.html#comments</comments>
		<pubDate>Wed, 12 Sep 2007 16:14:27 +0000</pubDate>
		<dc:creator>remivisser</dc:creator>
				<category><![CDATA[mssql]]></category>

		<guid isPermaLink="false">http://remidian.com/mssql/mssql-jdbc-connection-test.html</guid>
		<description><![CDATA[Microsoft SQL Server 2005 JDBC Driver java test program. Enterprise Manager Grid Control System Monitoring Plug-in for Microsoft SQL Server I have been busy deploying Microsoft SQL Server in our Oracle Enterpise GRID Control using the &#8216;Oracle Enterprise Manager Grid Control System Monitoring Plug-in for Microsoft SQL Server&#8217; http://www.oracle.com/technology/products/oem/extensions/plugin-ms_sql.html. The final step in the configuration [...]]]></description>
			<content:encoded><![CDATA[<p>
<a href="http://msdn2.microsoft.com/en-us/data/aa937724.aspx">Microsoft SQL Server 2005 JDBC Driver</a> java test program. <span id="more-60"></span>
</p>
<h3>Enterprise Manager Grid Control System Monitoring Plug-in for Microsoft SQL Server</h3>
<p>
I have been busy deploying Microsoft SQL Server in our Oracle Enterpise GRID Control using the &#8216;Oracle Enterprise Manager Grid Control<br />
System Monitoring Plug-in for Microsoft SQL Server&#8217; <a href="http://www.oracle.com/technology/products/oem/extensions/plugin-ms_sql.html">http://www.oracle.com/technology/products/oem/extensions/plugin-ms_sql.html</a>.<br />
<br />
The final step in the configuration is the configuration of your SQL Server target in the GRID Control &#8216;configure target&#8217; page for that SQL Server. One of the fields required is &#8216;JDBC URL&#8217; (and &#8216;JDBC Driver&#8217;).<br />
<br />
The Microsoft SQL Server 2005 JDBC Driver comes with two sample programs in sqljdbc_1.2\enu\help\samples\connections (connectDS.java and connectURL.java). Since one of the prerequisities I read on metalink is &#8216;make sure remote JDBC connections with your SQL Server work&#8217; I left the Oracle GRID console and tried to get this sample program to connect to my SQL Server succesfully.
</p>
<p>
It took my quit some time to get the right connection values (my SQL Server listened on TCP port 1204 instead of 1433), and because I got a little tired of having to compile the provided java test pogram every time I wanted to try new connection details I decided to rewrite this program into a program that &#8216;asks&#8217; for the connection values. This way I can provide the entries connection details at run time (instead of at compile time).
</p>
<h6>jdbcMSSql.java</h6>
<pre>
import java.io.Console;
import java.sql.*;

&nbsp;
public class jdbcMSSql {

&nbsp;&nbsp;public static void main(String[] args) {

&nbsp;&nbsp;&nbsp;&nbsp;// prepare console 
&nbsp;&nbsp;&nbsp;&nbsp;Console c = System.console();
&nbsp;&nbsp;&nbsp;&nbsp;if (c == null) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.err.println(&quot;No console.&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.exit(1);
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;// read from console
&nbsp;&nbsp;&nbsp;&nbsp;String servername = c.readLine(&quot;Enter servername&nbsp;&nbsp;: &quot;);
&nbsp;&nbsp;&nbsp;&nbsp;String portnumber = c.readLine(&quot;Enter portnumber&nbsp;&nbsp;: &quot;);
&nbsp;&nbsp;&nbsp;&nbsp;String database&nbsp;&nbsp; = c.readLine(&quot;Enter database&nbsp;&nbsp;&nbsp;&nbsp;: &quot;);
&nbsp;&nbsp;&nbsp;&nbsp;String username&nbsp;&nbsp; = c.readLine(&quot;Enter username&nbsp;&nbsp;&nbsp;&nbsp;: &quot;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// !! readPassword &#039;needs&#039; JDK 1.6
&nbsp;&nbsp;&nbsp;&nbsp;char [] password&nbsp;&nbsp; = c.readPassword(&quot;Enter password&nbsp;&nbsp;&nbsp;&nbsp;: &quot;);

&nbsp;&nbsp;&nbsp;&nbsp;// Construct connetionurl 
&nbsp;&nbsp;&nbsp;&nbsp;String conUrl = &quot;jdbc:sqlserver://&quot; +
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;servername + &quot;:&quot; + portnumber + &quot;;databaseName=&quot; + database +&quot;;&quot; +
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&quot;integratedSecurity=false;&quot;;

&nbsp;&nbsp;&nbsp;&nbsp;// Declare the JDBC objects.
&nbsp;&nbsp;&nbsp;&nbsp;Connection con = null;
&nbsp;&nbsp;&nbsp;&nbsp;Statement stmt = null;
&nbsp;&nbsp;&nbsp;&nbsp;ResultSet rs = null;
&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;try {&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Establish the connection.
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Class.forName(&quot;com.microsoft.sqlserver.jdbc.SQLServerDriver&quot;);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;con = DriverManager.getConnection( conUrl, username, new String(password));

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;stmt = con.createStatement();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rs = stmt.executeQuery( &quot;SELECT @@servername, @@spid&quot;);
&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// Iterate through the data in the result set and display it.
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while (rs.next()) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(&quot;Connection test OK:&nbsp;&nbsp;&#039;&quot; +
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;rs.getString(1) + &quot;&#039; (pid is &#039;&quot; + rs.getString(2) + &quot;&#039;).&quot;);&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;
&nbsp;&nbsp;&nbsp;&nbsp;// Handle any errors that may have occurred.
&nbsp;&nbsp;&nbsp;&nbsp;catch (Exception e) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;e.printStackTrace();
&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;finally {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (con != null) try { con.close(); } catch(Exception e) {}
&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;}
}

&nbsp;
</pre>
<h3>Compile and Execute</h3>
<p>
To compile this you must of course have the JDBC driver class in you %CLASSPATH% ($CLASSPATH) environment variable. See example below.
</p>
<h6>Compile jdbcMSSql.java</h6>
<pre>
C:\\oracle\\jdbc-test&gt;echo %CLASSPATH%
.;C:\\oracle\\agent10g\\sysman\\jlib\\sqljdbc_1.2\\enu\\sqljdbc.jar
&nbsp;
C:\\oracle\\jdbc-test&gt;javac jdbcMSSql.java
</pre>
<h6>Execute jdbcMSSql.class</h6>
<pre>
C:\\oracle\\jdbc-test&gt;java jdbcMSSql
Enter servername&nbsp;&nbsp;: REMIDIAN01
Enter portnumber&nbsp;&nbsp;: 1204
Enter database&nbsp;&nbsp;&nbsp;&nbsp;: master
Enter username&nbsp;&nbsp;&nbsp;&nbsp;: sa
Enter password&nbsp;&nbsp;&nbsp;&nbsp;:
Connection test OK:&nbsp;&nbsp;&#039;REMIDIAN01\\MSSQL01&#039; (pid is &#039;51&#039;).
&nbsp;
C:\\oracle\\jdbc-test&gt;
</pre>
<h3>Oracle GRID SQL Server Target Configuration</h3>
<p>
Now that I have the correct connection details I can go back the Oracle GRID console and provide the correct connection details in the Target Configuration page for the SQL Server target:</p>
<p><strong>JDBC URL</strong> : jdbc:sqlserver://remidian:1204;integratedSecurity=false;instanceName=MSSQL01<br />
<strong>JDBC Driver</strong>: com.microsoft.sqlserver.jdbc.SQLServerDriver <br />&nbsp;<br />&nbsp;<br />&nbsp;
</p>
<h3>N.B. &#8211; JDK 1.6 required</h3>
<p>
Program requires JDK 1.6 for the &#8216;console.readPassword&#8217; is new in 1.6, read here <a href="http://blogs.sun.com/DaveB/en_US/entry/new_improved_in_java_se1">http://blogs.sun.com/DaveB/en_US/entry/new_improved_in_java_se1</a>. When you have &lt; 1.6 simply change:
 </p>
<pre>
// char [] password&nbsp;&nbsp; = c.readPassword(&quot;Enter password&nbsp;&nbsp;&nbsp;&nbsp;: &quot;);
String password&nbsp;&nbsp; = c.readLine(&quot;Enter password&nbsp;&nbsp;&nbsp;&nbsp;: &quot;);
&nbsp;
// con = DriverManager.getConnection( conUrl, username, new String(password));
con = DriverManager.getConnection( conUrl, username, password);
</pre>
<h6>Java version banner</h6>
<pre>
C:\\oracle\\jdbc-test&gt;java -version
java version &quot;1.6.0_02&quot;
Java(TM) SE Runtime Environment (build 1.6.0_02-b06)
Java HotSpot(TM) Client VM (build 1.6.0_02-b06, mixed mode, sharing)
&nbsp;
C:\\oracle\\jdbc-test&gt;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://remidian.com/mssql/microsoft-sql-server-2005-jdbc-driver-oracle-grid.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Create, drop and rename a database</title>
		<link>http://remidian.com/mssql/administration/create-drop-and-rename-a-database.html</link>
		<comments>http://remidian.com/mssql/administration/create-drop-and-rename-a-database.html#comments</comments>
		<pubDate>Thu, 16 Nov 2006 11:35:51 +0000</pubDate>
		<dc:creator>remivisser</dc:creator>
				<category><![CDATA[Administration]]></category>

		<guid isPermaLink="false">http://remidian.com/mssql/create-rename-and-drop-a-database.html</guid>
		<description><![CDATA[Create, rename and drop a database in sql server &#8230; Create database See syntax below for the CREATE DATABASE statement. Create NORTHWIND database 1&#62; use master 2&#62; go 1&#62; create database [NORTHWIND] 2&#62;&#160;&#160;&#160;&#160; on ( name&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;= N&#039;NORTHWIND_dat&#039;, 3&#62;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;filename&#160;&#160;&#160;&#160;= N&#039;C:\\mssql\\data\\iDEFAULT\\NORTHWIND\\NORTHWIND_01.mdf&#039; , 4&#62;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;size&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;= 10, 5&#62;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;filegrowth&#160;&#160;= 1, 6&#62;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;maxsize&#160;&#160;&#160;&#160; = 1024 7&#62;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;) 8&#62; log on ( name&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;= N&#039;NORTHWIND_log&#039;, 9&#62;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;filename&#160;&#160;&#160;&#160;= [...]]]></description>
			<content:encoded><![CDATA[<p>Create, rename and drop a database in sql server &#8230;<span id="more-36"></span></p>
<h3>Create database</h3>
<p>
See syntax below for the <strong><a href="http://msdn2.microsoft.com/en-us/library/aa258257(SQL.80).aspx">CREATE DATABASE</a></strong> statement.
</p>
<h6>Create NORTHWIND database</h6>
<pre>
1&gt; use master
2&gt; go
1&gt; create database [NORTHWIND]
2&gt;&nbsp;&nbsp;&nbsp;&nbsp; on ( name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= N&#039;NORTHWIND_dat&#039;,
3&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;filename&nbsp;&nbsp;&nbsp;&nbsp;= N&#039;C:\\mssql\\data\\iDEFAULT\\NORTHWIND\\NORTHWIND_01.mdf&#039; ,
4&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;size&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= 10,
5&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;filegrowth&nbsp;&nbsp;= 1,
6&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;maxsize&nbsp;&nbsp;&nbsp;&nbsp; = 1024
7&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)
8&gt; log on ( name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= N&#039;NORTHWIND_log&#039;,
9&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;filename&nbsp;&nbsp;&nbsp;&nbsp;= N&#039;C:\\mssql\\tlog\\iDEFAULT\\NORTHWIND\\NORTHWIND_01.ldf&#039; ,
10&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; size&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= 10,
11&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; filegrowth&nbsp;&nbsp;= 1,
12&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; maxsize&nbsp;&nbsp;&nbsp;&nbsp; = 1024
13&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;)
14&gt;&nbsp;&nbsp;collate Latin1_General_CI_AS
15&gt;
16&gt; go
The CREATE DATABASE process is allocating 10.00 MB on disk &#039;NORTHWIND_dat&#039;.
The CREATE DATABASE process is allocating 10.00 MB on disk &#039;NORTHWIND_log&#039;.
1&gt;
</pre>
<h3>Drop database</h3>
<p>
      A database can be dropped using the <strong><a href="http://msdn2.microsoft.com/en-us/library/aa258843(SQL.80).aspx">DROP DATABASE</a></strong> statement. All database datafiles and logfiles are removed from the os.
</p>
<h6>Dropping a database</h6>
<pre>
1&gt; use master
2&gt; go
1&gt; drop database [NORTHWIND]
2&gt; go
Deleting database file &#039;C:\\mssql\\tlog\\iDEFAULT\\NORTHWIND\\NORTHWIND_01.ldf&#039;.
Deleting database file &#039;C:\\mssql\\data\\iDEFAULT\\NORTHWIND\\NORTHWIND_01.mdf&#039;.
1&gt;
</pre>
<h3>Rename database</h3>
<p>
A database can be renamed with the stored procedure <strong><a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_sp_ra-rz_3yle.asp">SP_RENAMEDB</a></strong>. (VERY easy compared to oracle &#8230;)
</p>
<h6>Rename the NORTHWIND database to &#8216;WESTWIND&#8217;</h6>
<pre>
1&gt; use master
2&gt; go
1&gt; select dbid, left( name, 10) name
2&gt;&nbsp;&nbsp; from sysdatabases
3&gt;&nbsp;&nbsp;where name = &#039;NORTHWIND&#039;
4&gt; go
 dbid&nbsp;&nbsp; name
 &#45;&#45;-&#45;&#45;- &#45;&#45;-&#45;&#45;-&#45;&#45;&#45;-
&nbsp;&nbsp;&nbsp;&nbsp; 17 NORTHWIND
&nbsp;
(1 row affected)
1&gt;
2&gt; exec sp_renamedb &#039;NORTHWIND&#039;, &#039;WESTWIND&#039;
3&gt; go
The database name &#039;WESTWIND&#039; has been set.
1&gt;
2&gt; select dbid, left( name, 10) name
3&gt;&nbsp;&nbsp; from sysdatabases
4&gt;&nbsp;&nbsp;where dbid = 17
5&gt; go
 dbid&nbsp;&nbsp; name
 &#45;&#45;-&#45;&#45;- &#45;&#45;-&#45;&#45;-&#45;&#45;&#45;-
&nbsp;&nbsp;&nbsp;&nbsp; 17 WESTWIND
&nbsp;
(1 row affected)
1&gt;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://remidian.com/mssql/administration/create-drop-and-rename-a-database.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Control Structures</title>
		<link>http://remidian.com/mssql/t-sql/control-structures.html</link>
		<comments>http://remidian.com/mssql/t-sql/control-structures.html#comments</comments>
		<pubDate>Wed, 15 Nov 2006 20:35:20 +0000</pubDate>
		<dc:creator>remivisser</dc:creator>
				<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false">http://remidian.com/uncategorized/control-structures.html</guid>
		<description><![CDATA[Control structures syntax; IF THEN ELSE, CASE, WHILE . IF THEN ELSE The IF THEN ELSE syntax in TSQL is kinda weird if you ask me &#8230; I forget them &#8216;all the time&#8217;, created examples below for my reference. IF THEN if 1 = 1 begin &#160;&#160;print &#039;true&#039; end &#160; go &#160; IF THEN &#8211; [...]]]></description>
			<content:encoded><![CDATA[<p>Control structures syntax; <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_tsqlcon_6lyk.asp">IF THEN ELSE</a>, <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_tsqlcon_6lyk.asp">CASE</a>, <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_wa-wz_6oyt.asp">WHILE</a> .<span id="more-35"></span>
</p>
<h3>IF THEN ELSE</h3>
<p>
The IF THEN ELSE syntax in TSQL is kinda weird if you ask me &#8230; I forget them &#8216;all the time&#8217;, created examples below for my reference.
</p>
<h6>IF THEN</h6>
<pre>
if 1 = 1
begin 
&nbsp;&nbsp;print &#039;true&#039;
end
&nbsp;
go
&nbsp;
</pre>
<h6>IF THEN &#8211; short notation</h6>
<pre>
if 1 = 1
&nbsp;&nbsp;print &#039;true&#039;
&nbsp;
go
&nbsp;
</pre>
<h6>IF THEN ELSE</h6>
<pre>
if 1 = 1
begin
&nbsp;&nbsp;print &#039;true&#039;
end
&nbsp;
else&nbsp;&nbsp;

begin
&nbsp;&nbsp;print &#039;false&#039;
end
&nbsp;
go
&nbsp;
</pre>
<h6>IF THEN ELSE &#8211; short notation</h6>
<pre>
if 1 = 1
&nbsp;&nbsp;print &#039;true&#039;
else&nbsp;&nbsp;
&nbsp;&nbsp;print &#039;false&#039;

go
&nbsp;
</pre>
<h6>Multiple IF THEN ELSE blocks</h6>
<pre>
if 1 = 1
begin
&nbsp;&nbsp;print &#039;1 = 1&#039;
end
else&nbsp;&nbsp;
if 1 = 2
&nbsp;&nbsp;begin
&nbsp;&nbsp;&nbsp;&nbsp;print &#039;1 = 2&#039;
&nbsp;&nbsp;end
else
if 1 = 3
begin
&nbsp;&nbsp;print &#039;1 = 3&#039;
end
&nbsp;
go
&nbsp;
</pre>
<h3>CASE</h3>
<p>TSQL has the CASE control structure &#8230;</p>
<pre>
declare @i int, @r int
&nbsp;
set @i = 1
&nbsp;
set @r = 
&nbsp;&nbsp;case @i
&nbsp;&nbsp;&nbsp;&nbsp;when 1 then 1
&nbsp;&nbsp;&nbsp;&nbsp;when 2 then 2
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else 0
&nbsp;&nbsp;end
&nbsp;
print @r
&nbsp;
go
&nbsp;
</pre>
<h3>WHILE</h3>
<h6>Eternal loop</h6>
<pre>
while 1 = 1 
&nbsp;&nbsp;print &#039;true&#039;
&nbsp;
go
&nbsp;
</pre>
<h6>Simple WHILE loop</h6>
<pre>
declare @i int
set @i = 0
&nbsp;
while @i &lt; 10
begin
&nbsp;&nbsp;print @i
&nbsp;&nbsp;set @i = @i + 1
end
&nbsp;
go
&nbsp;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://remidian.com/mssql/t-sql/control-structures.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Use filters to keep your traces meaningfull</title>
		<link>http://remidian.com/mssql/sql-trace/use-filters-to-keep-your-traces-meaningfull.html</link>
		<comments>http://remidian.com/mssql/sql-trace/use-filters-to-keep-your-traces-meaningfull.html#comments</comments>
		<pubDate>Tue, 14 Nov 2006 17:35:27 +0000</pubDate>
		<dc:creator>remivisser</dc:creator>
				<category><![CDATA[SQL Trace]]></category>

		<guid isPermaLink="false">http://remidian.com/mssql/sql-trace/use-filters-to-keep-your-traces-meaningfull.html</guid>
		<description><![CDATA[Say you have an sql trace for the event &#8216;TSQL SQL:StmtCompleted&#8217; you are, most probably, NOT interested in the sql statements executed through the SQL Server Agent for instance. Filters below remove most of the unwanted &#8216;internal&#8217; tsql. The contents of your tracefile will become more meanigfull and not unimportant small &#8230; Filter out &#8216;internal&#8217; [...]]]></description>
			<content:encoded><![CDATA[<p>
Say you have an sql trace for the event &#8216;TSQL SQL:StmtCompleted&#8217; you are, most probably, NOT interested in the sql statements executed through the SQL Server Agent for instance. <span id="more-34"></span> Filters below remove most of the unwanted &#8216;internal&#8217; tsql.
</p>
<p>
The contents of your tracefile will become more meanigfull and not unimportant small &#8230;
</p>
<h3>Filter out &#8216;internal&#8217; sql server proceses</h3>
<h6>Filter on &#8216;ApplicationName&#8217;</h6>
<pre>
&#45;- &#45;-
&#45;- ApplicationName
&nbsp;
&#45;- SQL Profiler sessions
exec sp_trace_setfilter @TraceID, 10, 0, 7, N&#039;SQL Profiler&#039;
&nbsp;
&#45;- SQL executed by the SQL Agent
exec sp_trace_setfilter @TraceID, 10, 0, 7, N&#039;SQLAgent%&#039;
exec sp_trace_setfilter @TraceID, 10, 0, 7, N&#039;SQLDMO_1%&#039;
&nbsp;
</pre>
<h6>Filter on &#8216;LoginName&#8217;</h6>
<pre>
&#45;- &#45;-
&#45;- LoginName
&nbsp;
exec sp_trace_setfilter @TraceID, 11, 0, 7, N&#039;remivisser&#039;
exec sp_trace_setfilter @TraceID, 11, 0, 7, N&#039;&lt;username&gt;&#039;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://remidian.com/mssql/sql-trace/use-filters-to-keep-your-traces-meaningfull.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>cursors in tsql</title>
		<link>http://remidian.com/mssql/t-sql/cursors-in-tsql.html</link>
		<comments>http://remidian.com/mssql/t-sql/cursors-in-tsql.html#comments</comments>
		<pubDate>Fri, 03 Nov 2006 16:22:17 +0000</pubDate>
		<dc:creator>remivisser</dc:creator>
				<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false">http://remidian.com/mssql/t-sql/cursors-in-tsql.html</guid>
		<description><![CDATA[Cursors in tsql, it can be done &#8230; Simple cursor loop Example below is the most straight forward example of walking through a CURSOR with a WHILE loop in tsql. The cursor is declared, opened and and a FETCH NEXT is done while @@fetch_status equals 0. The behaviour of a FETCH [ NEXT ] call [...]]]></description>
			<content:encoded><![CDATA[<p>
Cursors in tsql, it can be done &#8230; <span id="more-32"></span>
</p>
<h3>Simple cursor loop</h3>
<p>
Example below is the most straight forward example of walking through a <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_cr-cz_7hgy.asp">CURSOR</a> with a <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_wa-wz_6oyt.asp">WHILE</a> loop in tsql. The cursor is declared, opened and and a <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_fa-fz_90rs.asp">FETCH</a> NEXT is done while <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/tsqlref/ts_globals_1c1f.asp">@@fetch_status</a> equals 0.
</p>
<p>
The behaviour of a FETCH [ NEXT ] call is that it returns the value(s) for the column(s) specified in the DECLARE CURSOR as a single-row result set.
</p>
<h6>Basic cursor example with FETCH returning row as single-row result set</h6>
<pre>
1&gt; declare c1 cursor
2&gt; for
3&gt; select left( name, 32)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;as name,
4&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cast( dbid as varchar(10)) as dbid
5&gt;&nbsp;&nbsp; from master.dbo.sysdatabases
6&gt;&nbsp;&nbsp;where dbid &lt; 5
7&gt;
8&gt; open c1
9&gt; fetch next from c1
10&gt;
11&gt; &#45;- while @@fetch_status != -1 and @@fetch_status != -2
12&gt; while @@fetch_status = 0
13&gt;&nbsp;&nbsp;&nbsp;&nbsp;fetch next from c1
14&gt;
15&gt; close c1
16&gt; deallocate c1
17&gt;
18&gt; go
 name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dbid
 &#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;&#45;-- &#45;&#45;-&#45;&#45;-&#45;&#45;&#45;-
 master&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1
&nbsp;
 name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dbid
 &#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;&#45;-- &#45;&#45;-&#45;&#45;-&#45;&#45;&#45;-
 tempdb&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2
&nbsp;
 name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dbid
 &#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;&#45;-- &#45;&#45;-&#45;&#45;-&#45;&#45;&#45;-
 model&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3
&nbsp;
 name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dbid
 &#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;&#45;-- &#45;&#45;-&#45;&#45;-&#45;&#45;&#45;-
 msdb&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 4
&nbsp;
 name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; dbid
 &#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;&#45;-- &#45;&#45;-&#45;&#45;-&#45;&#45;&#45;-
&nbsp;
1&gt;</pre>
<h3>@@fetch_status</h3>
<p>
@@fetch_status is a built in variable returning the exit code of the latest fetch call.
</p>
<table border="1">
<tr>
<th>Return Value</th>
<th>Description</th>
</tr>
<tr>
<td>0</td>
<td> FETCH statement was successful.                              </td>
</tr>
<tr>
<td>-1</td>
<td> FETCH statement failed or the row was beyond the result set.</td>
</tr>
<tr>
<td>-2</td>
<td> Row fetched is missing.</td>
</tr>
</table>
<h3>FETCH INTO</h3>
<p>
The &#8216;FETCH INTO&#8217; option is used to return values from a cursor (instead of getting it returned as a single-row result set) into pre-defined variables.
</p>
<h6 style="margin:0 1px 2px 0;padding:0;border-bottom:1px solid #bbb">Example below displays how to perform a FETCH INTO with a single column.</h6>
<pre>
1&gt; declare @s_name&nbsp;&nbsp;&nbsp;&nbsp;varchar(32)
2&gt; declare c1 cursor
3&gt; for
4&gt; select name
5&gt;&nbsp;&nbsp; from master.dbo.sysdatabases
6&gt;&nbsp;&nbsp;where dbid &lt; 5
7&gt;
8&gt; open c1
9&gt; fetch next from c1 into @s_name
10&gt;
11&gt; while @@fetch_status = 0
12&gt; begin
13&gt;&nbsp;&nbsp; print &#039;name = &#039; + @s_name
14&gt;&nbsp;&nbsp; fetch next from c1 into @s_name
15&gt; end
16&gt;
17&gt; close c1
18&gt; deallocate c1
19&gt;
20&gt; go
name = master
name = tempdb
name = model
name = msdb
1&gt;</pre>
<h6>Example below displays how to perform a FETCH INTO with multiple columns.</h6>
<pre>
1&gt; declare @s_name&nbsp;&nbsp;&nbsp;&nbsp;varchar(32),
2&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; @i_dbid&nbsp;&nbsp;&nbsp;&nbsp;smallint
3&gt; declare c1 cursor
4&gt; for
5&gt; select name,
6&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dbid
7&gt;&nbsp;&nbsp; from master.dbo.sysdatabases
8&gt;&nbsp;&nbsp;where dbid &lt; 5
9&gt;
10&gt; open c1
11&gt; fetch next from c1 into @s_name, @i_dbid
12&gt;
13&gt; while @@fetch_status = 0
14&gt; begin
15&gt;&nbsp;&nbsp; print &#039;DB = &#039; + cast( @i_dbid as varchar(8)) + &#039;:&#039; + @s_name
16&gt;&nbsp;&nbsp; fetch next from c1 into @s_name, @i_dbid
17&gt; end
18&gt;
19&gt; close c1
20&gt; deallocate c1
21&gt;
22&gt; go
DB = 1:master
DB = 2:tempdb
DB = 3:model
DB = 4:msdb
1&gt;</pre>
<h3>Scrollable cursors</h3>
<p>
All examples above are so-called &#8216;forward only cursors&#8217; (the default cursor type). When you want to be able to scroll through your cursor the cursor most be declared through the SCROLL CURSOR declaration.
</p>
<p>Whenever a cursor is declared as a SCROLL CURSOR the following FETCH options become available:
</p>
<h6>Fetch Options</h6>
<ul>
<li>PRIOR                  </li>
<li>FIRST                  </li>
<li>LAST                   </li>
<li>ABSOLUTE { n | @nvar } </li>
<li>RELATIVE { n | @nvar } </li>
</ul>
<p>
All examples below are done with &#8216;demo&#8217; table listed below.
</p>
<h6>demo table</h6>
<pre>
1&gt; select * from demo order by k
2&gt; go
 k
 &#45;&#45;-&#45;&#45;-&#45;&#45;&#45;--
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 2
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 3
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 4
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 5
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 6
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 7
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 8
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 9
&nbsp;
(10 rows affected)
1&gt;
</pre>
<h6>FETCH NEXT and FETCH PRIOR</h6>
<pre>
1&gt; declare @i as int
2&gt; declare c1 scroll cursor
3&gt; for
4&gt; select k
5&gt;&nbsp;&nbsp; from demo
6&gt; order by k
7&gt;
8&gt;
9&gt;
10&gt; open c1
11&gt;
12&gt;
13&gt; print &#039;FETCH NEXT &#039; + char(10)
14&gt; print &#039;==================================&#039;
15&gt;
16&gt; fetch next from c1 into @i
17&gt; while @@fetch_status = 0
18&gt; begin
19&gt;&nbsp;&nbsp; print cast(@i as char(1)) + &#039; @@fetch_status &#039; + cast(@@fetch_status as char)
20&gt;&nbsp;&nbsp; fetch next from c1 into @i
21&gt; end
22&gt;
23&gt; fetch next from c1 into @i
24&gt; print cast(@i as char(1)) + &#039; @@fetch_status &#039; + cast(@@fetch_status as char)
25&gt;
26&gt;
27&gt; print char(10) + &#039;FETCH PRIOR &#039; + char(10)
28&gt; print &#039;==================================&#039;
29&gt;
30&gt; fetch prior from c1 into @i
31&gt; while @@fetch_status = 0
32&gt; begin
33&gt;&nbsp;&nbsp; print cast(@i as char(1)) + &#039; @@fetch_status &#039; + cast(@@fetch_status as char)
34&gt;&nbsp;&nbsp; fetch prior from c1 into @i
35&gt; end
36&gt;
37&gt; fetch prior from c1 into @i
38&gt; print cast(@i as char(1)) + &#039; @@fetch_status &#039; + cast(@@fetch_status as char)
39&gt;
40&gt;
41&gt; close c1
42&gt; deallocate c1
43&gt;
44&gt;
45&gt;
46&gt; go
FETCH NEXT
==================================
0 @@fetch_status 0
1 @@fetch_status 0
2 @@fetch_status 0
3 @@fetch_status 0
4 @@fetch_status 0
5 @@fetch_status 0
6 @@fetch_status 0
7 @@fetch_status 0
8 @@fetch_status 0
9 @@fetch_status 0
9 @@fetch_status -1
&nbsp;
FETCH PRIOR
==================================
9 @@fetch_status 0
8 @@fetch_status 0
7 @@fetch_status 0
6 @@fetch_status 0
5 @@fetch_status 0
4 @@fetch_status 0
3 @@fetch_status 0
2 @@fetch_status 0
1 @@fetch_status 0
0 @@fetch_status 0
0 @@fetch_status -1
1&gt;
</pre>
<h6>FETCH LAST / FETCH PRIOR</h6>
<pre>
1&gt; declare @i as int
2&gt; declare c1 scroll cursor
3&gt; for
4&gt; select k
5&gt;&nbsp;&nbsp; from demo
6&gt; order by k
7&gt;
8&gt;
9&gt;
10&gt; open c1
11&gt;
12&gt;
13&gt; print char(10) + &#039;FIRST LAST PRIOR &#039; + char(10)
14&gt; print &#039;==================================&#039;
15&gt; fetch last from c1 into @i
16&gt; print &#039;FETCH LAST&nbsp;&nbsp;= &#039; + cast(@i as char(1)) +
17&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#039; @@fetch_status = &#039; + cast(@@fetch_status as char)
18&gt; fetch prior from c1 into @i
19&gt; print &#039;FETCH PRIOR = &#039; + cast(@i as char(1)) +
20&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#039; @@fetch_status = &#039; + cast(@@fetch_status as char)
21&gt; fetch first from c1 into @i
22&gt; print &#039;FETCH FIRST = &#039; + cast(@i as char(1)) +
23&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#039; @@fetch_status = &#039; + cast(@@fetch_status as char)
24&gt; fetch prior from c1 into @i
25&gt; print &#039;FETCH PRIOR = &#039; + cast(@i as char(1)) +
26&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#039; @@fetch_status = &#039; + cast(@@fetch_status as char)
27&gt; fetch next from c1 into @i
28&gt; print &#039;FETCH NEXT&nbsp;&nbsp;= &#039; + cast(@i as char(1)) +
29&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#039; @@fetch_status = &#039; + cast(@@fetch_status as char)
30&gt; fetch next from c1 into @i
31&gt; print &#039;FETCH NEXT&nbsp;&nbsp;= &#039; + cast(@i as char(1)) +
32&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#039; @@fetch_status = &#039; + cast(@@fetch_status as char)
33&gt;
34&gt;
35&gt; close c1
36&gt; deallocate c1
37&gt;
38&gt;
39&gt;
40&gt; go
&nbsp;
FIRST LAST PRIOR
==================================
FETCH LAST&nbsp;&nbsp;= 9 @@fetch_status = 0
FETCH PRIOR = 8 @@fetch_status = 0
FETCH FIRST = 0 @@fetch_status = 0
FETCH PRIOR = 0 @@fetch_status = -1
FETCH NEXT&nbsp;&nbsp;= 0 @@fetch_status = 0
FETCH NEXT&nbsp;&nbsp;= 1 @@fetch_status = 0
1&gt;
</pre>
<h6>FETCH ABSOLUTE</h6>
<pre>
1&gt; declare @i as int
2&gt; declare c1 scroll cursor
3&gt; for
4&gt; select k
5&gt;&nbsp;&nbsp; from demo
6&gt; order by k
7&gt;
8&gt; open c1
9&gt;
10&gt; print char(10) + &#039;FETCH ABSOLUTE &#039; + char(10)
11&gt; print &#039;======================================&#039;
12&gt; fetch absolute 2 from c1 into @i
13&gt; print &#039;FETCH ABSOLUTE 2 = &#039; + cast(@i as char(1)) +
14&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#039; @@fetch_status = &#039; + cast(@@fetch_status as char)
15&gt; fetch absolute 8 from c1 into @i
16&gt; print &#039;FETCH ABSOLUTE 8 = &#039; + cast(@i as char(1)) +
17&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#039; @@fetch_status = &#039; + cast(@@fetch_status as char)
18&gt;
19&gt; close c1
20&gt; deallocate c1
21&gt;
22&gt; go
&nbsp;
FETCH ABSOLUTE
======================================
FETCH ABSOLUTE 2 = 1 @@fetch_status = 0
FETCH ABSOLUTE 8 = 7 @@fetch_status = 0
1&gt;</pre>
<h6>FETCH RELATIVE</h6>
<pre>
1&gt; declare @i as int
2&gt; declare c1 scroll cursor
3&gt; for
4&gt; select k
5&gt;&nbsp;&nbsp; from demo
6&gt; order by k
7&gt;
8&gt;
9&gt;
10&gt; open c1
11&gt;
12&gt;
13&gt; print char(10) + &#039;FETCH RELATIVE&#039; + char(10)
14&gt; print &#039;=======================================&#039;
15&gt; fetch relative 1 from c1 into @i
16&gt; print &#039;FETCH RELATIVE 1&nbsp;&nbsp; = &#039; + cast( @i as varchar(1)) +
17&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#039; @@fetch_status = &#039; + cast(@@fetch_status as char)
18&gt;
19&gt; fetch relative -1 from c1 into @i
20&gt; print &#039;FETCH RELATIVE -1&nbsp;&nbsp;= &#039; + cast( @i as varchar(1)) +
21&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#039; @@fetch_status = &#039; + cast(@@fetch_status as char)
22&gt;
23&gt; fetch relative -2 from c1 into @i
24&gt; print &#039;FETCH RELATIVE -2&nbsp;&nbsp;= &#039; + cast( @i as varchar(1)) +
25&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#039; @@fetch_status = &#039; + cast(@@fetch_status as char)
26&gt;
27&gt; fetch relative 3 from c1 into @i
28&gt; print &#039;FETCH RELATIVE 3&nbsp;&nbsp; = &#039; + cast( @i as varchar(1)) +
29&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#039; @@fetch_status = &#039; + cast(@@fetch_status as char)
30&gt;
31&gt; fetch relative 100 from c1 into @i
32&gt; print &#039;FETCH RELATIVE 100 = &#039; + cast( @i as varchar(1)) +
33&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#039; @@fetch_status = &#039; + cast(@@fetch_status as char)
34&gt;
35&gt; fetch relative -1 from c1 into @i
36&gt; print &#039;FETCH RELATIVE -1&nbsp;&nbsp;= &#039; + cast( @i as varchar(1)) +
37&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#039; @@fetch_status = &#039; + cast(@@fetch_status as char)
38&gt;
39&gt;
40&gt; close c1
41&gt; deallocate c1
42&gt;
43&gt;
44&gt;
45&gt; go
&nbsp;
FETCH RELATIVE
=======================================
FETCH RELATIVE 1&nbsp;&nbsp; = 0 @@fetch_status = 0
FETCH RELATIVE -1&nbsp;&nbsp;= 0 @@fetch_status = -1
FETCH RELATIVE -2&nbsp;&nbsp;= 0 @@fetch_status = -1
FETCH RELATIVE 3&nbsp;&nbsp; = 2 @@fetch_status = 0
FETCH RELATIVE 100 = 2 @@fetch_status = -1
FETCH RELATIVE -1&nbsp;&nbsp;= 9 @@fetch_status = 0
1&gt;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://remidian.com/mssql/t-sql/cursors-in-tsql.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>return variables from dynamic sql &#8211; sp_executesql</title>
		<link>http://remidian.com/mssql/t-sql/return-variables-from-dynamic-sql-sp_executesql.html</link>
		<comments>http://remidian.com/mssql/t-sql/return-variables-from-dynamic-sql-sp_executesql.html#comments</comments>
		<pubDate>Wed, 01 Nov 2006 20:31:11 +0000</pubDate>
		<dc:creator>remivisser</dc:creator>
				<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false">http://remidian.com/mssql/return-values-from-dynamic-sql.html</guid>
		<description><![CDATA[Returing values into tsql variables from a dynamic sql statement using sp_executesql stored procedure. Scenario The example below displays the number of objects per database. The following steps are involved. open a cursor for the select * from master.dbo.sysdatabases statement (cursor is a while loop), inside the cursor a dynamic sql statement is defined , [...]]]></description>
			<content:encoded><![CDATA[<p>
        Returing values into tsql variables from a dynamic sql statement using <a href="http://msdn2.microsoft.com/en-us/library/ms188001.aspx">sp_executesql</a> stored procedure. <span id="more-30"></span>
</p>
<h3>Scenario</h3>
<p>
The example below displays the number of objects per database. The following steps are involved.
</p>
<ul>
<li>open a cursor for the <code>select * from master.dbo.sysdatabases</code> statement (cursor is a while loop), </li>
<li>inside the cursor a dynamic sql statement is defined ,</li>
<li>at last the stored procedure sp_executesql is used to retrieve the output of this defined sql statement into tsql variables .</li>
</ul>
<h6>sp_execute example to return values into variables </h6>
<pre>
1&gt; declare @s_dtb&nbsp;&nbsp;sysname
2&gt; declare @s_sql&nbsp;&nbsp;nvarchar(2000)
3&gt; declare @i_obj&nbsp;&nbsp;int
4&gt; declare c1&nbsp;&nbsp;&nbsp;&nbsp; cursor
5&gt; for
6&gt; select name
7&gt;&nbsp;&nbsp; from master.dbo.sysdatabases
8&gt;
9&gt;
10&gt; open c1
11&gt; fetch c1 into @s_dtb
12&gt;
13&gt; while @@fetch_status &gt;= 0
14&gt; begin
15&gt;
16&gt;&nbsp;&nbsp; &#45;- construct sql statement
17&gt;&nbsp;&nbsp; set @s_sql = &#039;select @count = count(*)
18&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from &#039; + QuoteName(@s_dtb) + &#039;.dbo.sysobjects&#039;
19&gt;
20&gt;&nbsp;&nbsp; &#45;- execute dynamic sql and map output variable @count to variable @i_obj
21&gt;&nbsp;&nbsp; exec sp_executesql
22&gt;&nbsp;&nbsp;&nbsp;&nbsp; @s_sql,
23&gt;&nbsp;&nbsp;&nbsp;&nbsp; N&#039;@count int output&#039;,
24&gt;&nbsp;&nbsp;&nbsp;&nbsp; @count = @i_obj output
25&gt;
26&gt;&nbsp;&nbsp; print @s_dtb + &#039; db has &#039; + cast( @i_obj as varchar) + &#039; objects&#039;
27&gt;
28&gt;&nbsp;&nbsp; fetch c1 into @s_dtb
29&gt;
30&gt; end
31&gt;
32&gt; close c1
33&gt; deallocate c1
34&gt;
35&gt;
36&gt; go
master db has 1324 objects
model db has 54 objects
msdb db has 454 objects
Northwind db has 146 objects
tempdb db has 54 objects
1&gt;
</pre>
<p>
        Extremely handy I would say. &#8211; Whenever you want to perform an operation (execute tsql) on all your databases.</p>
]]></content:encoded>
			<wfw:commentRss>http://remidian.com/mssql/t-sql/return-variables-from-dynamic-sql-sp_executesql.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>environment</title>
		<link>http://remidian.com/mssql/t-sql/environment.html</link>
		<comments>http://remidian.com/mssql/t-sql/environment.html#comments</comments>
		<pubDate>Mon, 18 Sep 2006 19:22:48 +0000</pubDate>
		<dc:creator>remivisser</dc:creator>
				<category><![CDATA[T-SQL]]></category>

		<guid isPermaLink="false">http://remidian.com/uncategorized/mssql-environment.html</guid>
		<description><![CDATA[Getting around in the mssql environment Whoami In MSSQL (2000) there&#8217;s not an easy way to see in which database you are. Script below creates a view &#8216;whoami&#8217; that gives you the information; create view dbo.whoami as select @@spid as &#039;@@spid&#039;, &#160;&#160;&#160;&#160;&#160;&#160; cast( user as varchar(12)) as &#039;user&#039;, &#160;&#160;&#160;&#160;&#160;&#160; cast( p.dbid as varchar(6)) + &#039;:&#039; [...]]]></description>
			<content:encoded><![CDATA[<p>Getting around in the mssql environment<span id="more-25"></span></p>
<h3>Whoami</h3>
<p>
In MSSQL (2000) there&#8217;s not an easy way to see in which database you are. Script below creates a view &#8216;whoami&#8217; that gives you the information;
</p>
<pre>
create view dbo.whoami
as
select @@spid as &#039;@@spid&#039;,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cast( user as varchar(12)) as &#039;user&#039;,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cast( p.dbid as varchar(6)) + &#039;:&#039; + cast( d.name as varchar(14)) as &#039;database&#039;,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cast( p.loginame as varchar(16)) loginame,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cast( p.hostname as varchar(16)) as &#039;hostname&#039;,
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cast( p.nt_username as varchar(16)) nt_username
&nbsp;&nbsp; from master.dbo.sysprocesses p left join master.dbo.sysdatabases d
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;on d.dbid = p.dbid
&nbsp;&nbsp; where spid = @@spid
&nbsp;
go
&nbsp;
grant select on dbo.whoami to public
go
&nbsp;
</pre>
<pre>
1&gt; select * from whoami
2&gt; go
 @@spid user&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; database&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;loginame&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; hostname&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; nt_username
 &#45;&#45;-&#45;&#45;- &#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;- &#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;- &#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;&#45;- &#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;&#45;- &#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;-&#45;&#45;&#45;-
&nbsp;&nbsp;&nbsp;&nbsp; 51 dbo&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1:master&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NYC01\\remivisser NYC01&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;remivisser
&nbsp;
(1 row affected)
1&gt;
&nbsp;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://remidian.com/mssql/t-sql/environment.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
