<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>DOT NET RULES</title>
	<atom:link href="http://dotnetstories.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://dotnetstories.wordpress.com</link>
	<description>Yes, to dance beneath the diamond sky with one hand waving free</description>
	<lastBuildDate>Sat, 04 May 2013 09:34:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='dotnetstories.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://1.gravatar.com/blavatar/1a14469d4b7ba9e0f89bffef42cef6b5?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>DOT NET RULES</title>
		<link>http://dotnetstories.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://dotnetstories.wordpress.com/osd.xml" title="DOT NET RULES" />
	<atom:link rel='hub' href='http://dotnetstories.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Looking into Transaction Log size and clearing and database backups</title>
		<link>http://dotnetstories.wordpress.com/2013/01/04/looking-into-transaction-log-size-and-clearing-and-database-backups/</link>
		<comments>http://dotnetstories.wordpress.com/2013/01/04/looking-into-transaction-log-size-and-clearing-and-database-backups/#comments</comments>
		<pubDate>Fri, 04 Jan 2013 19:49:20 +0000</pubDate>
		<dc:creator>fofo</dc:creator>
				<category><![CDATA[Sql Server]]></category>
		<category><![CDATA[Sql Server 2005]]></category>
		<category><![CDATA[SQL Server 2008]]></category>
		<category><![CDATA[SQL Server 2012]]></category>
		<category><![CDATA[database backups]]></category>
		<category><![CDATA[recovery models]]></category>
		<category><![CDATA[transaction log]]></category>

		<guid isPermaLink="false">http://dotnetstories.wordpress.com/?p=2339</guid>
		<description><![CDATA[In this post I would like to talk about the size of the transaction log and under what circumstances it clears in relation to recovery models and database backup types (Full, Differential, Log). I will also give a brief overview of recovery models, database backup types.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=2339&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>In this post I would like to talk about the size of the transaction log and under what circumstances it clears in relation to recovery models and database backup types (<strong>Full, Differential, Log</strong>). I will also give a brief overview of <strong>recovery models</strong>, <strong>database backup types</strong>.</p>
<p>When I usually write a blog post that is often due to questions I receive in my mailbox or real problems my clients face and I have to resolve. A few days ago I got an email from a friend of mine. He was telling me that his website was down and was not functioning. He said the error he was seeing in was</p>
<p><strong> &#8220;The transaction log for database &#8216;mydb&#8217; is full. To find out why space in the log cannot be reused, see the <span style="text-decoration:underline;"><em>log_reuse_wait_desc</em> </span>column in sys.databases&#8221;</strong></p>
<p>It is obvious that there is a problem with the transaction log and that is why his site is not functioning.I will also provide hands-on examples when needed.</p>
<p>Before we proceed I would like to explain the various recovery models.We have 3 recovery models available in SQL Server</p>
<ul>
<li>Simple</li>
<li>Full</li>
<li>Bulk-Logged</li>
</ul>
<p>In the <strong>Simple</strong> <strong>Recovery Model</strong> every time I have a <strong>checkpoint operation or the .ldf file is full, the .ldf file is automatically truncated</strong>.In this model the <strong>transaction log is not backed up</strong>.You can <strong>not have point in time data recovery with this model</strong>.Data is recoverable only to the <strong>most recent backup</strong>.</p>
<p>The <strong>Simple</strong> <strong>Recovery Model</strong>  is only suitable for <strong>small databases and databases with few transactions</strong>.We could use this recovery model if we want to have a <strong>small transaction log and for databases that are read-only or in development stage</strong>.</p>
<p>The <strong>Full Recovery Model</strong> is the <strong>default recovery model</strong>.In this model the <strong>transaction log grows rapidly since all database transactions are logged in at a very detailed level</strong>.If I have a catastrophe in my database I can rollback to <strong>the time where the catastrophe occurred</strong>.This recovery model is suitable for<strong> databases with thousands of transactions per day and where no loss of data is tolerated</strong>.</p>
<p>The <strong>Bulk Recovery Model</strong> works almost identically with the <strong>Full Recovery Model</strong>.<strong>It logs everything in extents=8pages and all bulk tasks (bcp,bulk insert) are logged minimally</strong>.We can go back to the time of failure but it will take more time to get the database back on-line.This model would be very suitable for <strong>databases with large bulk tasks</strong>.The disadvantage of this model is that you <strong>cannot go back at a specific point in time</strong>.</p>
<p>Having looked into the recovery models,let’s have a look on the various <strong>SQL Server backup types</strong>.</p>
<p>Let’s see what happens when we take a <strong>full database backup</strong>.The first thing to say is that you need always a full database backup.This is the base for all subsequent backups. You cannot have a <strong>differential backup,transaction log backup if you do not have at least one full backup</strong>.With a full database backup you <strong>backup all your data</strong>.You can have a <strong>full backup with</strong> <strong>all the recovery models</strong> that you choose to have for your database.<strong>Every full backup I perform in a database contains in it all the data previously fully backed up</strong>.<br />
The backup operation is an online operation and the first thing it happens is to log that a backup was started. Then we write the data in the backup file.Then we do log any changes that occur as we backup.Finally the backing up of the data is finished and we log that fact.If you’re performing only full backups, you might lose some data in the event of a system crash—specifically, any changes made since the last full backup.</p>
<p>A <strong>differential backup</strong> is cumulative backup. It includes all data and structures that have changed since the last full backup, regardless of when that last full backup was made, or how many previous differential backups have been run.<strong>You can have a differential backup with all the recovery models</strong>.</p>
<p>The <strong>Transaction Log backup</strong> only works for full or <strong>bulk-logged recovery models</strong>.Each T-Log backup contains all the transactions that are in my database <strong>since the last T-Log backup</strong>.Always perform a <strong>full back up before you take a T-log backup</strong>.The time taken for a T-log backup is little and there is no performance penalty.<strong>An important thing to remember is that for every t-log backup I need the previous backup to have a restore</strong>.</p>
<p>Let me come back to the original problem which is that my friend&#8217;s website <strong>run out of transaction log space</strong>.<br />
This happens when the transaction log fills up to the point where it has to grow but either <strong>autogrow is not turned</strong> <strong>on</strong> or the volume on which the transaction log is placed <strong>has no more space for the file to grow</strong>.</p>
<p>That is profound. But what has caused the transaction log to fill up in the first place? It could be a number of different things.</p>
<p>The two most common ones I&#8217;ve seen are:</p>
<ol>
<li>The database is in <strong>full recovery model </strong>and subsequent full database backups were taken but no <strong>log backups have been taken</strong>.</li>
</ol>
<p>This commonly happens when an application goes into production and someone decides to take a backup, without realizing the consequences. Log truncation is not automatic in such a case &#8211; a log backup is required to allow the log to truncate.</p>
<p>2. The database is in any recovery mode but there&#8217;s a very <strong>long-running, uncommitted transaction</strong> that prevents log truncation.</p>
<p>In this case I was called to examine, the problem was that the database was in <strong>full recovery mode,</strong> subsequent full database backups were taken but no <strong>log backups have been taken</strong>.</p>
<p>Some people believe that a full database backup in the <strong>full</strong> or <strong>bulk logged</strong> will clear/truncate the transaction log.</p>
<p>Well, these people are wrong. Simple as that. They are wrong.This is not true. Only a <strong>transaction log backup will clear the log under these recovery models</strong>.</p>
<p>On the other hand, in the <strong>SIMPLE recovery model</strong>, a checkpoint clears the transaction log. I see people that have critical data in their databases and need to restore it (in case of a catastrophe) to the point of failure, to use <strong>Simple recovery model</strong> in their databases because the transaction log is getting too large. So in order to keep it small, they switch recovery models. This is so wrong!!!! In case of a failure these people will lose data they cannot afford to lose.</p>
<p>Let me go on with the actual example</p>
<p>I will be using <strong>SQL Server 2012 Εnterprise</strong> edition in this post but feel free to use SQL Server 2008/R2 versions and any other edition you might have installed (Standard, Express e.t.c).</p>
<p>1) Launch SSMS and connect to the local (or any other instance) of SQL Server</p>
<p>2) I will be looking at the column <strong>log_reuse_wait_desc </strong>of the<strong> sys.databases</strong></p>
<p>3) In a new query window in SSMS , type (or copy paste) the following t-sql script and then execute it (substitute database <strong>football </strong>with any other you have attached in the instance of SQL Server.)</p>
<pre class="brush: sql; title: ; notranslate">

SELECT  log_reuse_wait_desc
FROM    sys.databases
WHERE   name = 'football'

</pre>
<p>In my case I receive<strong> &#8220;Nothing&#8221; </strong>and that is why you will receive yourself if you execute the T-SQL script above but in my friend&#8217;s case when I connected to the SQL hosting the database I go the following:</p>
<p><strong><em>Log Backup</em></strong></p>
<p>That is all I needed to know. SQL Server tells me what the problem is.The database was in <strong>Full recovery model</strong>, only <strong>full backups</strong> were taken and no <strong>log backups have been taken.</strong></p>
<p>4) Now I will show you why a full database backup does not clear the transaction log</p>
<p>Type (copy and paste) the following T-SQL script but do not execute it yet.</p>
<pre class="brush: sql; title: ; notranslate">

USE MASTER;
GO

CREATE DATABASE [Football] ON  PRIMARY
( NAME = N'Football_Data', FILENAME = N'D:\DATA\Football_Data.mdf' , SIZE = 80MB , MAXSIZE = UNLIMITED, FILEGROWTH = 20MB )
LOG ON
( NAME = N'Football_Log', FILENAME = N'D:\DATA\Football_log.ldf' , SIZE = 20MB , MAXSIZE = UNLIMITED, FILEGROWTH = 5MB)
GO

USE FOOTBALL;
GO

CREATE TABLE Footballer
(
FootballerID int identity(1,1),
Footballer_No varchar(20),
Footballer_lastname nvarchar(50),
Footballer_firstname nvarchar(50),
Footballer_middlename nvarchar(50)
)
GO

CREATE UNIQUE CLUSTERED INDEX cx_Foo_IX ON Footballer (FootballerID)
GO

declare @CTR INT
declare @ctrstr varchar(7)
select @ctr=0
WHILE @CTR&lt;4000
BEGIN
select @ctr=@ctr+1
select @ctrstr=Str(@ctr)
insert footballer(Footballer_No ,Footballer_lastname,Footballer_firstname, Footballer_middlename) values('F_No'+@ctrstr,'Footlname'+@ctrstr,'Footfname'+@ctrstr,'Footmname'+@ctrstr)
END

BACKUP DATABASE [Football] TO  DISK = N'D:\DATA\backup\football_full.bak' WITH INIT,STATS;
GO

BACKUP LOG [Football] TO  DISK = N'D:\DATA\backup\football_log.bak' WITH INIT,STATS;
GO

ALTER INDEX cx_Foo_IX ON Footballer REBUILD;
GO

BACKUP LOG [Football] TO  DISK = N'D:\DATA\backup\football1_log.bak' WITH INIT,STATS;
GO

ALTER INDEX cx_Foo_IX ON Footballer REBUILD;
GO

BACKUP DATABASE [Football] TO  DISK = N'D:\DATA\backup\football1_full.bak' WITH INIT,STATS;
GO

BACKUP LOG [Football] TO  DISK = N'D:\DATA\backup\football2_log.bak' WITH INIT,STATS;
GO

</pre>
<p>Let me explain what I am doing.</p>
<p>In lines <strong>1-8</strong> I create a  sample database <strong>Football.</strong></p>
<p>In lines <strong>10-24</strong>, I create a table, <strong>Footballer </strong>and a unique clustered index on column <strong>FootballerID.</strong></p>
<p>In lines <strong>26-34</strong> I populate the <strong>Footballer</strong> table.</p>
<p>In lines <strong>36-37</strong> I take full database backup.</p>
<p>In lines <strong>39-40</strong> I take log database backup.</p>
<p>In lines <strong>42-43 </strong>I rebuild the clustered index.</p>
<p>In lines <strong>46-47 </strong>I take another log backup.</p>
<p>In lines <strong>49-50 </strong>I rebuild the clustered index.</p>
<p>In lines <strong>52-53 </strong>I take another full database backup. If a database backup clears the log, the next transaction log , should be small.</p>
<p>In lines <strong>55-56 </strong>I take another log backup.</p>
<p>Let&#8217;s now compare the size of the last two transaction logs. Have a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2013/01/logs.png"><img class="aligncenter size-full wp-image-2342" alt="logs" src="http://dotnetstories.files.wordpress.com/2013/01/logs.png?w=460&#038;h=247" width="460" height="247" /></a></p>
<p>As you can see the size of the transaction logs is more or less the same.So the last transactional log is not smaller,thus the full database backup did not clear the log.</p>
<p>The way I solved my friend&#8217;s problem was to free disk space so that the log can automatically grow and then take a full backup following a <strong>log backup.</strong></p>
<p>Then I explained him the importance of log backups in log truncation and size management. He has incorporated log backups in his database backup strategy.</p>
<p>Hope it helps!!!!</p>
<br />Filed under: <a href='http://dotnetstories.wordpress.com/category/sql-server/'>Sql Server</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2005/'>Sql Server 2005</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2008/'>SQL Server 2008</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2012/'>SQL Server 2012</a> Tagged: <a href='http://dotnetstories.wordpress.com/tag/database-backups/'>database backups</a>, <a href='http://dotnetstories.wordpress.com/tag/recovery-models/'>recovery models</a>, <a href='http://dotnetstories.wordpress.com/tag/transaction-log/'>transaction log</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dotnetstories.wordpress.com/2339/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dotnetstories.wordpress.com/2339/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=2339&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dotnetstories.wordpress.com/2013/01/04/looking-into-transaction-log-size-and-clearing-and-database-backups/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/ecbfd3584f3b2c0c3f528bacdfc15e0a?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96" medium="image">
			<media:title type="html">fofo</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2013/01/logs.png" medium="image">
			<media:title type="html">logs</media:title>
		</media:content>
	</item>
		<item>
		<title>Looking into the DBCC LOGINFO and DBCC SQLPERF commands</title>
		<link>http://dotnetstories.wordpress.com/2012/12/28/looking-into-the-dbcc-loginfo-and-dbcc-sqlperf-commands/</link>
		<comments>http://dotnetstories.wordpress.com/2012/12/28/looking-into-the-dbcc-loginfo-and-dbcc-sqlperf-commands/#comments</comments>
		<pubDate>Fri, 28 Dec 2012 19:49:55 +0000</pubDate>
		<dc:creator>fofo</dc:creator>
				<category><![CDATA[Sql Server]]></category>
		<category><![CDATA[Sql Server 2005]]></category>
		<category><![CDATA[SQL Server 2008]]></category>
		<category><![CDATA[SQL Server 2012]]></category>
		<category><![CDATA[dbcc commands]]></category>
		<category><![CDATA[DBCC LOGINFO]]></category>
		<category><![CDATA[DBCC SQLPERF]]></category>

		<guid isPermaLink="false">http://dotnetstories.wordpress.com/?p=2323</guid>
		<description><![CDATA[In this post I would like to talk a bit about Transaction Log,its various parts and its architecture. I will not go into details about what Transaction Log is because it is well documented elsewhere. I would like also to highlight a few important points. Ι will provide hands-on examples for the DBCC LOGINFO and DBCC SQLPERF commands.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=2323&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>In this post I would like to talk a bit about <strong>Transaction Log</strong>,its various parts and its architecture. I will not go into details about what <strong>Transaction Log</strong> is because it is well documented elsewhere. I would like also to highlight a few important points. Ι will provide hands-on examples for the <strong>DBCC LOGINFO</strong> and <strong>DBCC SQLPERF</strong> commands.</p>
<p>I have been helping out a friend to understand various important issues regarding <strong>Transaction Logs</strong> in SQL Server databases.The transaction log <strong>records all the transactions and the modifications on the database those transactions have</strong>. Usually you have one <strong>transaction log file</strong> per database but you can have multiple files.The default size of a transaction log (if not specified) is the 25% of the total size of all data files created for the database.It is always a good idea to have the size of the transaction log planned beforehand.Bear in mind that the transaction log is zero initialized and that is a costly operation in both time and resources.The transaction log is divided into smaller parts that are called <strong>VLFs (Virtual Log Files). </strong>It is easier to manage the transaction log like that.  You have a active and inactive <strong>VLFs</strong> . One <strong>VLF</strong> must be active at any time in the database. You cannot configure the number and the size of the VLFs in the transaction log. As transaction log grows, new <strong>VLFs</strong> are created and their size is determined by SQL Server. There is a <em><strong>formula</strong></em> that determines the number and the size of the <strong>VLFs. </strong>If the transaction log grows by <strong>64 MBytes</strong>, <strong>4</strong> new <strong>VLFs</strong> are created each <strong>1/4</strong> of the growth size.If the transaction log grows by <strong>64 MBytes to 1Gbytes</strong>, <strong>8</strong> new <strong>VLFs</strong> are created each <strong>1/8</strong> of the growth size.If the transaction log grows by <strong> 1Gbytes or more</strong>, <strong>16</strong> new <strong>VLFs</strong> are created each <strong>1/16</strong> of the growth size.Also bear in mind that each <strong>VLF</strong> is uniquely identified by a <strong>sequence number</strong>.</p>
<p>Let&#8217;s have a look at the actual hands-on demos.</p>
<p>I will be using <strong>SQL Server 2012 Εnterprise</strong>  edition in this post but feel free to use SQL Server 2008/R2 versions and any other edition you might have installed (Standard, Express e.t.c).</p>
<p>1) Launch SSMS and connect to the local (or any other instance) of SQL Server</p>
<p>2) I will be looking firstly at the <strong>DBCC LOGINFO</strong> command which provides us with important information about the structure of the transaction log.</p>
<p>3) In a new query window in SSMS , type (or copy paste) the following t-sql script and then execute it</p>
<pre class="brush: sql; title: ; notranslate">

USE MASTER;
GO

CREATE DATABASE [Football] ON  PRIMARY
( NAME = N'Football_Data', FILENAME = N'D:\DATA\Football_Data.mdf' , SIZE = 180MB , MAXSIZE = UNLIMITED, FILEGROWTH = 20MB )
LOG ON
( NAME = N'Football_Log', FILENAME = N'D:\DATA\Football_log.ldf' , SIZE = 40MB , MAXSIZE = UNLIMITED, FILEGROWTH = 20MB)
GO

DBCC LOGINFO(N'Football');
GO

</pre>
<p>Let me explain what I do in this example. I create a database <strong>Football.</strong>Then I run the <strong>DBCC LOGINFO(N&#8217;Football&#8217;) </strong>to understand a bit about the log structure and the number of <strong>VLFs</strong></p>
<p>Have a look at the photo below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/12/loginfo1.png"><img class="aligncenter size-full wp-image-2330" alt="loginfo" src="http://dotnetstories.files.wordpress.com/2012/12/loginfo1.png?w=460&#038;h=247" width="460" height="247" /></a></p>
<p>As you can see there are 4 <strong>VLFs.</strong>Each one of them is <strong>10 Mbytes</strong>.So we can see that the <em><strong>formula</strong></em> applies in this case. The Log file size is <strong>40 Mbytes</strong> so we expect to have <strong>4</strong> <strong>VLFs.</strong></p>
<p>4) Now let&#8217;s have a look at the <strong>DBCC SQLPERF(LOGSPACE)</strong> command. This command provides transaction log space usage statistics for all databases. Have a look</p>
<p>In a new query window type and execute the following T-Sql command.</p>
<pre class="brush: sql; title: ; notranslate">

USE MASTER;
GO

DBCC SQLPERF(LOGSPACE);
GO

</pre>
<p>Have a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/12/sqlperf-1.png"><img class="aligncenter size-full wp-image-2333" alt="sqlperf-1" src="http://dotnetstories.files.wordpress.com/2012/12/sqlperf-1.png?w=460&#038;h=247" width="460" height="247" /></a></p>
<p>5) Now I am going to increase the size of the transaction log by inserting sample data in the database and then check again the number and the size of the <strong>VLFs</strong> in the transaction log. I will also check the transaction log file size and the part of it that is used.</p>
<p>Type (copy and paste) the following T-SQL script. Execute it</p>
<pre class="brush: sql; title: ; notranslate">


USE FOOTBALL;
GO

ALTER DATABASE Football MODIFY FILE (NAME =N'Football_Log',SIZE=60MB);

DBCC LOGINFO(N'Football');
GO

DBCC SQLPERF(LOGSPACE);
GO

</pre>
<p>6) We add more space in the transaction log and then execute the <strong>DBCC LOGINFO</strong> and <strong>DBCC SQLPERF </strong>commands. Have a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/12/sqladdlog.png"><img class="aligncenter size-full wp-image-2335" alt="sqladdlog" src="http://dotnetstories.files.wordpress.com/2012/12/sqladdlog.png?w=460&#038;h=247" width="460" height="247" /></a></p>
<p>We can see now that the size of the transaction log is more than <strong>64Mbytes</strong> we have <strong>8 VLFs </strong></p>
<p>7) Now in a new query window type (copy paste) and execute the following script.</p>
<pre class="brush: sql; title: ; notranslate">

USE FOOTBALL;
GO

CREATE TABLE Footballer
(
FootballerID int identity(1,1),
Footballer_No varchar(20),
Footballer_lastname nvarchar(50),
Footballer_firstname nvarchar(50),
Footballer_middlename nvarchar(50)
)
GO

CREATE UNIQUE CLUSTERED INDEX cx_Foo_IX ON Footballer (FootballerID)
GO

declare @CTR INT
declare @ctrstr varchar(7)
select @ctr=0
WHILE @CTR&lt;100.000
BEGIN
select @ctr=@ctr+1
select @ctrstr=Str(@ctr)
insert footballer(Footballer_No ,Footballer_lastname,Footballer_firstname, Footballer_middlename) values('F_No'+@ctrstr,'Footlname'+@ctrstr,'Footfname'+@ctrstr,'Footmname'+@ctrstr)
END

SELECT * FROM Footballer;
GO


</pre>
<p>I create a table in the database.Then I create a clustered index and insert 100.000 rows in it. That will increase the size of the used space in the transaction log.</p>
<p>Type and execute the T-SQL code below.</p>
<pre class="brush: sql; title: ; notranslate">
DBCC LOGINFO(N'Football');
GO


DBCC SQLPERF(LOGSPACE);
GO


</pre>
<p>Have a look at the picture below to see results I have got when I run the T-SQL code above.</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/12/transaction-log.png"><img class="aligncenter size-full wp-image-2336" alt="transaction-log" src="http://dotnetstories.files.wordpress.com/2012/12/transaction-log.png?w=460&#038;h=247" width="460" height="247" /></a></p>
<p>Hope you learned about transaction log architecture and the importance of <strong>DBCC LOGINFO</strong> and <strong>DBCC SQLPERF</strong> commands.</p>
<p>Hope it helps!!!!</p>
<br />Filed under: <a href='http://dotnetstories.wordpress.com/category/sql-server/'>Sql Server</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2005/'>Sql Server 2005</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2008/'>SQL Server 2008</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2012/'>SQL Server 2012</a> Tagged: <a href='http://dotnetstories.wordpress.com/tag/dbcc-commands/'>dbcc commands</a>, <a href='http://dotnetstories.wordpress.com/tag/dbcc-loginfo/'>DBCC LOGINFO</a>, <a href='http://dotnetstories.wordpress.com/tag/dbcc-sqlperf/'>DBCC SQLPERF</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dotnetstories.wordpress.com/2323/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dotnetstories.wordpress.com/2323/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=2323&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dotnetstories.wordpress.com/2012/12/28/looking-into-the-dbcc-loginfo-and-dbcc-sqlperf-commands/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/ecbfd3584f3b2c0c3f528bacdfc15e0a?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96" medium="image">
			<media:title type="html">fofo</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/12/loginfo1.png" medium="image">
			<media:title type="html">loginfo</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/12/sqlperf-1.png" medium="image">
			<media:title type="html">sqlperf-1</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/12/sqladdlog.png" medium="image">
			<media:title type="html">sqladdlog</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/12/transaction-log.png" medium="image">
			<media:title type="html">transaction-log</media:title>
		</media:content>
	</item>
		<item>
		<title>How to close common nopCommerce Gaps for .NET e-commerce providers</title>
		<link>http://dotnetstories.wordpress.com/2012/12/06/how-to-close-common-nopcommerce-gaps-for-net-e-commerce-providers/</link>
		<comments>http://dotnetstories.wordpress.com/2012/12/06/how-to-close-common-nopcommerce-gaps-for-net-e-commerce-providers/#comments</comments>
		<pubDate>Wed, 05 Dec 2012 23:54:17 +0000</pubDate>
		<dc:creator>fofo</dc:creator>
				<category><![CDATA[asp.net]]></category>
		<category><![CDATA[ASP.NET 4.0]]></category>
		<category><![CDATA[ASP.Net MVC]]></category>
		<category><![CDATA[Nop Anywhere Sliders]]></category>
		<category><![CDATA[NopCommerce]]></category>
		<category><![CDATA[NopTemplates]]></category>

		<guid isPermaLink="false">http://dotnetstories.wordpress.com/?p=2295</guid>
		<description><![CDATA[For those of you, who do not know nopCommerce, let me first say that it is probably the best .NET e-commerce system out there with a comprehensive set of features and extensible architecture. .NET developers interested in e-commerce will certainly be more than happy to learn that nopCommerce is developed using .NET 4.5, EF 5 and MVC 4.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=2295&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>For those of you, who do not know <strong>nopCommerce</strong>, let me first say that it is probably the best <strong>.NET e-commerce system</strong> out there with a comprehensive set of features and extensible architecture. .NET developers interested in e-commerce will certainly be more than happy to learn that <strong>nopCommerce</strong> is developed using <strong>.NET 4.5</strong>, <strong>EF 5</strong> and <strong>MVC 4</strong>.</p>
<p><strong>NopCommerce</strong> should certainly be in the toolbox of every .NET web company but as feature-rich and as extensible as it might be, you cannot offer the system to clients as it is, at least not with the default <strong>Dark Orange</strong> theme. But this is a normal part of the open source model and that is why every such system has a network of third party vendors to fill the gaps in the free software.</p>
<p><strong>NopCommerce</strong> has quite a lot of customization partners and a few product vendors. But one particular set of products stand out from the rest.</p>
<p>In this blog post and the posts to follow I will try to show you how you can use the <strong>nopCommerce</strong> themes and extensions from <a href="http://www.nop-templates.com/p/4/nop-anywhere-sliders">Nop-Templates.com</a> to build a solution for your e-commerce clients in literary minutes.</p>
<p>Looking at the default <strong>nopCommerce</strong> installation , <a href="http://demo.nopcommerce.com/">http://demo.nopcommerce.com/</a>,the first thing that I would like to have on my e-shop and probably the first thing that my e-commerce client will ask me for is how to put banners on the web site.  As a .NET web developer you have two options. Go out and find a sliding <strong>JQuery</strong> script, write some <strong>MVC</strong> code to integrate the <strong>JQuery</strong> script in wherever is appropriate in <strong>nopCommerce</strong>, and hard code some sliding images. This solution while possible will certainly require time researching and developing the code. If you are not familiar with <strong>nopCommerce</strong> you will have to factor in the learning curve too.</p>
<p>Fortunately we have the <a href="http://www.nop-templates.com/p/4/nop-anywhere-sliders">Nop Anywhere Sliders</a> from <strong>Nop-Templates</strong> which will not only do all this but will also offer you a set of features, which will empower your <strong>nopCommerce</strong> with the possibility to put sliding images on any page and on as many pages as you need.</p>
<p>As a <strong>nopCommerce</strong> savvy user I have found working with the <strong>Nop Anywhere Sliders</strong> quite easy and intuitive. First of all you need to install the plugin via the nopCommerce administration:<a href="http://dotnetstories.files.wordpress.com/2012/12/installplugins.png"><img class="aligncenter size-full wp-image-2298" alt="installPlugins" src="http://dotnetstories.files.wordpress.com/2012/12/installplugins.png?w=460&#038;h=142" height="142" width="460" /></a></p>
<p>It is important that the SevenSpikes.Core plugin is installed before the<strong> Nop Anywhere Sliders</strong>.</p>
<p>Once you have the Nop Anywhere Sliders installed you can go to the administration of the plugin by clicking on the <strong>Plugins -&gt; Nop  Anywhere Sliders -&gt; Manage Sliders</strong>:</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/12/manageslidersmenu.png"><img class="aligncenter size-full wp-image-2300" alt="manageSlidersMenu" src="http://dotnetstories.files.wordpress.com/2012/12/manageslidersmenu.png?w=460&#038;h=259" height="259" width="460" /></a></p>
<p>A slider according to the <strong>Nop-Templates</strong> terminology is a set of images and settings, which are setup to slide in one place. You can define as many of these sliders as you need and add them to any part of your web site. For example you can define a slider called <strong>HomePageMainSlider</strong>:</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/12/slidersettings.png"><img class="aligncenter size-full wp-image-2301" alt="sliderSettings" src="http://dotnetstories.files.wordpress.com/2012/12/slidersettings.png?w=460&#038;h=407" height="407" width="460" /></a></p>
<p>Make sure that the slider dimensions are the same as the dimensions of your images. Most of the Slider Settings are self-explanatory and have a tooltip. If you need clarification on some of them you can visit the documentation page of the plugin which is also accessible via the Plugins -&gt; Nop Anywhere Sliders -&gt; Help Menu:</p>
<p><a href="http://www.nop-templates.com/Help/NopAnywhereSliders/Version_2_7/lessons/Working_with_Nop_Anywhere_Sliders.html">http://www.nop-templates.com/Help</a><a href="http://www.nop-templates.com/Help/NopAnywhereSliders/Version_2_7/lessons/Working_with_Nop_Anywhere_Sliders.html">/NopAnywhereSliders/Version_2_7/lessons/Working_with_Nop_Anywhere_Sliders.html</a></p>
<p>Once you have configured your slider you can add some images to it by going to the <strong>Pictures</strong> tab:</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/12/sliderimages.png"><img class="aligncenter size-full wp-image-2303" alt="sliderImages" src="http://dotnetstories.files.wordpress.com/2012/12/sliderimages.png?w=460&#038;h=158" height="158" width="460" /></a></p>
<p>For every image you can set the following settings:</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/12/sliderimagesettings.png"><img class="aligncenter size-full wp-image-2305" alt="sliderImageSettings" src="http://dotnetstories.files.wordpress.com/2012/12/sliderimagesettings.png?w=460&#038;h=283" height="283" width="460" /></a></p>
<p>When you have you slider and images set up, what is left to do is to add it to your web site. You have several ways of doing this. The easiest way is add it via the predefined widget zones:</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/12/sliderwidgets.png"><img class="aligncenter size-full wp-image-2307" alt="sliderWidgets" src="http://dotnetstories.files.wordpress.com/2012/12/sliderwidgets.png?w=460&#038;h=148" height="148" width="460" /></a></p>
<p>If we add the slider to the home_page_slider_top widget zone, it will appear on our home page just below the main menu. You have more than 20 predefined widget zones by default which should be plenty to position your sliders wherever you need on your nopCommerce web site. However if you do not find an appropriate widget zone you can manually integrate the slider. To do this you need to add the following line of razor code on the page where you want you slider to appear:</p>
<p><strong>@Html.Action(&#8220;Slider&#8221;, &#8220;AnywhereSliders&#8221;, new { systemName = &#8220;HomePageMainSlider&#8221;})</strong></p>
<p>Alternatively you can define your own widget zones and add them to the plugin so that you can use them for your sliders. For more information on this you can read the documentation:</p>
<p><a href="http://www.nop-templates.com/Help/NopAnywhereSliders/Version_2_7/lessons/Integration.html">http://www.nop-templates.com/Help/NopAnywhereSliders/Version_2_7/lessons/Integration.html</a></p>
<p>One of the coolest features that the <strong>Nop Anywhere Sliders</strong> is the ability to map sliders to categories:</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/12/slidercategories.png"><img class="aligncenter size-full wp-image-2308" alt="sliderCategories" src="http://dotnetstories.files.wordpress.com/2012/12/slidercategories.png?w=460&#038;h=166" height="166" width="460" /></a></p>
<p>This is an important functionality for cases where you need a different slider for every category. The category page is the same for every category. It pulls the data for the category dynamically based on the id or name specified in the query string. Therefore you need this mapping so that the plugin knows when a specific category is being loaded which slider to add to the page. The category mapping is also supported by the manual integration of the plugin. If you are to add a slider manually to your category page you need to use the following line of Razor code:</p>
<p><strong>@Html.Action(&#8220;CategorySlider&#8221;,&#8221;AnywhereSliders&#8221;, new { categoryId = Model.Id })</strong></p>
<p>The category mapping of the plugin is quite a nifty feature, which can save you tons of coding and integration if you are to implement this from scratch.</p>
<p>I have covered the basics of working with the <strong>Nop Anywhere Sliders</strong> and I hope you will find this useful. The plugin is quite powerful and flexible and just does the job that it is supposed to do with very little effort required from the <strong>nopCommerce</strong> store owner or developer.</p>
<p>For more information do visit the product page:</p>
<p><a href="http://www.nop-templates.com/p/4/nop-anywhere-sliders">http://www.nop-templates.com/p/4/nop-anywhere-sliders</a></p>
<br />Filed under: <a href='http://dotnetstories.wordpress.com/category/aspnet/'>asp.net</a>, <a href='http://dotnetstories.wordpress.com/category/asp-net-4-0/'>ASP.NET 4.0</a>, <a href='http://dotnetstories.wordpress.com/category/asp-net-mvc/'>ASP.Net MVC</a> Tagged: <a href='http://dotnetstories.wordpress.com/tag/nop-anywhere-sliders/'>Nop Anywhere Sliders</a>, <a href='http://dotnetstories.wordpress.com/tag/nopcommerce/'>NopCommerce</a>, <a href='http://dotnetstories.wordpress.com/tag/noptemplates/'>NopTemplates</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dotnetstories.wordpress.com/2295/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dotnetstories.wordpress.com/2295/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=2295&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dotnetstories.wordpress.com/2012/12/06/how-to-close-common-nopcommerce-gaps-for-net-e-commerce-providers/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/ecbfd3584f3b2c0c3f528bacdfc15e0a?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96" medium="image">
			<media:title type="html">fofo</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/12/installplugins.png" medium="image">
			<media:title type="html">installPlugins</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/12/manageslidersmenu.png" medium="image">
			<media:title type="html">manageSlidersMenu</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/12/slidersettings.png" medium="image">
			<media:title type="html">sliderSettings</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/12/sliderimages.png" medium="image">
			<media:title type="html">sliderImages</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/12/sliderimagesettings.png" medium="image">
			<media:title type="html">sliderImageSettings</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/12/sliderwidgets.png" medium="image">
			<media:title type="html">sliderWidgets</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/12/slidercategories.png" medium="image">
			<media:title type="html">sliderCategories</media:title>
		</media:content>
	</item>
		<item>
		<title>Configuring SQL Server after installation</title>
		<link>http://dotnetstories.wordpress.com/2012/10/28/configuring-sql-server-after-installation/</link>
		<comments>http://dotnetstories.wordpress.com/2012/10/28/configuring-sql-server-after-installation/#comments</comments>
		<pubDate>Sun, 28 Oct 2012 18:40:18 +0000</pubDate>
		<dc:creator>fofo</dc:creator>
				<category><![CDATA[Sql Server]]></category>
		<category><![CDATA[Sql Server 2005]]></category>
		<category><![CDATA[SQL Server 2008]]></category>
		<category><![CDATA[SQL Server 2012]]></category>
		<category><![CDATA[SQL Server Configuration Manager]]></category>

		<guid isPermaLink="false">http://dotnetstories.wordpress.com/?p=2241</guid>
		<description><![CDATA[In this post I will demonstrate how to configure SQL Server after installation. I have a relevant post in this blog on how to install SQL Server.

I have installed SQL Server Developer edition in my machine. Developer edition of SQL Server 2012 can be installed in a client operating system.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=2241&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>In this post I will demonstrate how to configure SQL Server after installation. I have a relevant <a href="https://dotnetstories.wordpress.com/2012/07/14/%CE%B9nstalling-a-named-instance-of-sql-server-2012/" target="_blank">post</a> in this blog on how to install SQL Server.</p>
<p>I have installed SQL Server Developer edition in my machine. <strong>Developer edition of SQL Server 2012</strong> can be installed in a client operating system.</p>
<p>1) I will use <strong>SQL Server Configuration Manager</strong> to change some initial configurations.We can manage the various services that are running and are related to SQL Server.We can manage the various network protocols.</p>
<p>Have a look at the picture below to see how you can open <strong>SQL Server Configuration Manager</strong>.</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/10/configuration-manager.png"><img class="aligncenter  wp-image-2242" title="configuration-manager" alt="" src="http://dotnetstories.files.wordpress.com/2012/10/configuration-manager.png?w=574&#038;h=262" height="262" width="574" /></a></p>
<p>2) After firing up this tool, I can see all the <strong>SQL Server Services </strong>that are installed in this machine.You can see their names, their state, their start mode, that security account they are running under,the <strong>Process ID</strong> and the <strong>Service Type</strong>.You can see that there are 3 <strong>Database SQL Server Services</strong> running in my machine.One service for the named instance for the <strong>SQL Server 2008 R2 Enterprise Edition</strong>, one named instance for <strong>SQL Server 2012 Express Edition</strong> and one service for the default instance of <strong>SQL Server 2012 Developer Edition.</strong></p>
<p>Have a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/10/conf1.png"><img class="aligncenter size-large wp-image-2243" title="conf1" alt="" src="http://dotnetstories.files.wordpress.com/2012/10/conf1.png?w=1024&#038;h=550" height="550" width="1024" /></a></p>
<p>3) We can <strong>Start</strong>, <strong>Stop</strong>, <strong>Pause</strong>, <strong>Resume</strong>, <strong>Restart</strong> each service. Have a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/10/conf-start.jpg"><img class="aligncenter size-large wp-image-2245" title="conf-start" alt="" src="http://dotnetstories.files.wordpress.com/2012/10/conf-start.jpg?w=1024&#038;h=550" height="550" width="1024" /></a></p>
<p>4) We can also set/change the various properties by clicking on <strong>Properties.</strong></p>
<p>We can change the <strong>Start Mode</strong> of a particular service e.g From <strong>Automatic</strong> to <strong>Manual</strong>.Have a look at the picture below.</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/10/conf-2.jpg"><img class="aligncenter size-large wp-image-2247" title="conf-2" alt="" src="http://dotnetstories.files.wordpress.com/2012/10/conf-2.jpg?w=1024&#038;h=550" height="550" width="1024" /></a></p>
<p>5) We can also configure settings that are relevant with network protocols.These are going to be the protocols that SQL Server listens to for client connections.</p>
<p>When I select the protocols for the MSSQLSERVER I see there are 3 protocols:<strong>Shared Memory</strong>,<strong>Named Pipes</strong>,<strong>TCP/IP</strong>.If you want to allow remote connections from clients over a network like the Internet that supports TCP/IP we must enable that protocol.</p>
<p>Have a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/10/conf-3.jpg"><img class="aligncenter size-large wp-image-2248" title="conf-3" alt="" src="http://dotnetstories.files.wordpress.com/2012/10/conf-3.jpg?w=1024&#038;h=550" height="550" width="1024" /></a></p>
<p>We can also change some settings regarding the TCP/IP protocol by clicking on <strong>Properties</strong>.We can change the default  TCP/IP port number <strong>1433</strong>, to something else.</p>
<p>After changing these settings you may need to restart the service.</p>
<p>Have a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/10/conf-4.jpg"><img class="aligncenter size-large wp-image-2249" title="conf-4" alt="" src="http://dotnetstories.files.wordpress.com/2012/10/conf-4.jpg?w=1024&#038;h=550" height="550" width="1024" /></a></p>
<p>6) Another thing you can do with <strong>SQL Server Configuration Manager</strong> is configuring client connections to other SQL Servers, meaning how this SQL Server instance is going to make outbound database connections.</p>
<p>Have a look at the picture below.Please take a note on the order of the protocols.You can see that all the protocols are enabled.</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/10/conf-5.jpg"><img class="aligncenter size-large wp-image-2252" title="conf-5" alt="" src="http://dotnetstories.files.wordpress.com/2012/10/conf-5.jpg?w=1024&#038;h=550" height="550" width="1024" /></a></p>
<p>These are the protocols that SQL Server uses to connect to other instances of SQL Server. We can also set <strong>aliases</strong> from <strong>SQL Server Configuration Manager</strong>.A client will connect to the alias ( a name <strong>other</strong> the server instance name ).You can set the alias name, the port number,the protocol and the server it will connect to.</p>
<p>Have a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/10/conf-7.jpg"><img class="aligncenter size-large wp-image-2253" title="conf-7" alt="" src="http://dotnetstories.files.wordpress.com/2012/10/conf-7.jpg?w=1024&#038;h=550" height="550" width="1024" /></a></p>
<p>7) In order to show you the rest of SQL Server configurations I will switch to the familiar environment of SQL Server Management Studio &#8211; SSMS.Make sure you close the SQL Server Configuration Manager snap-in.</p>
<p>I will connect through SSMS to my SQL Server 2012 Developer Edition using <strong>Windows Authentication</strong>.Then I select the SQL Server instance and right-click on it. From the available options I choose <strong>Properties. </strong>In this new window I can set  various options regarding memory,processors and security.Do not make changes that affect the whole SQL Server instance unless you know exactly what you are doing.You should consult the product documentation. I will be going through most of these options in the next steps.In the <strong>General</strong> tab you can see information about the name of SQL Server instance, the language, the number of available processors,the amount of memory.</p>
<p>Have a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/10/conf-8.jpg"><img class="aligncenter size-large wp-image-2259" title="conf-8" alt="" src="http://dotnetstories.files.wordpress.com/2012/10/conf-8.jpg?w=1024&#038;h=550" height="550" width="1024" /></a></p>
<p>This information is read only.</p>
<p>8) We can also click on the <strong>Connection Properties</strong> in the <strong>Server Properties Window</strong>.<br />
You can see the <strong>Authentication Mode</strong> and the username I used to log in.I am connected to the master database. You can also see the product name and version of SQL Server. There is also adequate information regarding the operating system.This information is also read only.</p>
<p>Have a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/10/conf-9.jpg"><img class="aligncenter size-large wp-image-2262" title="conf-9" alt="" src="http://dotnetstories.files.wordpress.com/2012/10/conf-9.jpg?w=1024&#038;h=550" height="550" width="1024" /></a></p>
<p>9) The next option in the <strong>Server Properties</strong> window is the <strong>Memory </strong>tab<strong>.<br />
</strong></p>
<p>Have a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/10/conf-10.jpg"><img class="aligncenter size-large wp-image-2264" title="conf-10" alt="" src="http://dotnetstories.files.wordpress.com/2012/10/conf-10.jpg?w=1024&#038;h=550" height="550" width="1024" /></a></p>
<p>Most of the default options in this tab will be ok for most SQL Server configurations , unless you have heavy loads.<strong>Minimum </strong>and<strong> maximum</strong> server memory settings let you control how much memory SQL Server can use. We all know SQL Server is very demanding when it comes to memory.You should change these settings if you have another server application running in the same machine e.g Exchange Server. By the way you should never do that, having another server application running in the same machine where our production SQL Server lives. Some people ask me how much memory SQL Server is using.Well the answer to that should be &#8220;All of it&#8221;.</p>
<p>If you have 32 Gbytes of RAM in the box that SQL Server lives and there is only one database attached on this server that has a size of 32 Gbytes,SQL Server will cache the entire database in memory.This will improve performance significantly.If you add more memory to the server, make sure you will increase the <strong>Maximum server memory</strong> as well.</p>
<p>To recap the default memory settings and in particular the <strong>maximum server memory</strong> setting,if unchanged allows SQL Server to use as much memory as it wants. Well SQL Server listen to calls from the OS and it releases memory back to it so the operating system is not starved.You can change the settings from the <strong>Server Properties</strong> window.You can also use T-Sql to do that</p>
<p>Have a look at the picture below to see the statements I am using to set the maximum memory to <strong>3800</strong> Mbytes. In my machine the available Physical RAM is 4Gbytes.</p>
<p>&nbsp;</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/10/conf-12.jpg"><img class="aligncenter size-large wp-image-2266" title="conf-12" alt="" src="http://dotnetstories.files.wordpress.com/2012/10/conf-12.jpg?w=1024&#038;h=550" height="550" width="1024" /></a></p>
<p>Some people suggest that we should limit this number (maximum server memory) to 80-85% of the actual physical memory on a server that has only the Database Engine running.</p>
<p>You could use T-SQL to get the values for minimum and maximum memory settings.</p>
<p>In a new query window in SSMS, type and execute the following statement.</p>
<p><strong>SELECT</strong><br />
<strong>    name, </strong><br />
<strong>    minimum, </strong><br />
<strong>    maximum, </strong><br />
<strong>    value_in_use</strong><br />
<strong>FROM master.sys.configurations</strong><br />
<strong>WHERE name IN (&#8216;min server memory (MB)&#8217;, &#8216;max server memory (MB)&#8217;);</strong></p>
<p>You can also use a DBCC command to get a snapshot of the current memory status of SQL Server.</p>
<p>In a new query window you could type <strong>DBCC MEMORYSTATUS. </strong>Have a look in this <a href="http://support.microsoft.com/kb/907877" target="_blank">link</a> for more information.</p>
<p>In the <strong>Memory</strong> tab  you could see another option if you are run a 32-bit server.This option that does not show up in my case since I am running a 64-bit version of SQL Server (as we saw earlier) is <strong>Use AWE to Allocate Memory</strong> .Do check this option for 32-bit servers with &gt;4GB of memory, and keep it unchecked in all other cases.</p>
<p>Some of the changes I mentioned above will take effect immediately but in some cases you must restart the service.</p>
<p>10) The next set of options I will be looking into are the ones located in the <strong>Processors</strong> tab.</p>
<p>Have a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/10/conf-11.jpg"><img class="aligncenter size-large wp-image-2265" title="conf-11" alt="" src="http://dotnetstories.files.wordpress.com/2012/10/conf-11.jpg?w=1024&#038;h=550" height="550" width="1024" /></a></p>
<p>You can see that there are 4 available processors in this computer.SQL Server supports many processors and takes advantage of their power.</p>
<p>If you want to change the <strong>I/O Affinity</strong> or the <strong>Processor Affinity </strong> you must first <strong>uncheck</strong> the two boxes under the label <strong>Enable processors.</strong></p>
<p>Let me explain what <strong>I/O Affinity</strong> and <strong>Processor Affinity</strong> mean. By setting the <strong>Processor Affinity</strong> to one or more processors you basically dedicate SQL Server to use one or more of these processors.</p>
<p>By setting the <strong>I/O Affinity</strong> to a specific processor you basically bind I/O threads to this  processor.</p>
<p>I would advise against changing the values for <strong>I/O Affinity</strong> and <strong>Processor Affinity</strong> . Leave the default settings.</p>
<p>Another option I want to talk about is <strong>Boost SQL Server priority</strong>.BY enabling this option, SQL Server process will run as <strong>High Priority</strong> instead of the usual <strong>Normal priority</strong>. In most cases this will not bring any benefit to your database applications so leave the default value.</p>
<p>Another option I want to talk about is <strong>Maximum worker threads.</strong>This helps limit the resources consumed on an SQL Server instance by client connections. A new Windows thread is created for each connection.If you set a maximum it can hurt performance.Once that limit  is reached any new connection must wait for another connection to close in order to have access to the server.</p>
<p>By default, the <strong>max worker threads</strong> setting is <strong>0</strong> in SQL Server 2005,SQL Server 2008 and SQL Server 2012.Do not change the default value.</p>
<p>11) In the next tab we will look into settings regarding <strong>Security</strong> in SQL Server.I have already blogged about SQL Server Security in this <a href="https://dotnetstories.wordpress.com/2011/10/17/looking-into-security-in-sql-server/" target="_blank">post</a>.</p>
<p>Have a look at the picture below.</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/10/conf-14.jpg"><img class="aligncenter size-large wp-image-2272" title="conf-14" alt="" src="http://dotnetstories.files.wordpress.com/2012/10/conf-14.jpg?w=1024&#038;h=550" height="550" width="1024" /></a></p>
<p>We have two authentication modes in SQL Server,<strong>Windows Authentication</strong> <strong>Mode</strong> and<strong> Mixed Mode</strong>.</p>
<p><strong>Windows Authentication</strong> basically means that a user must log in to a <strong>windows domain</strong> or a windows local account first before logging into an SQL Server system.Basically a security token is created by Windows when someone logs in and this is passed to SQL Server that “validates” the account  name and password based on that token. In a sentence the user logged in SQL Server is checked and identified by Windows.</p>
<p>SQL Server validates the account name and password using the Windows principal token in the operating system.</p>
<p><strong>Windows Authentication</strong> disables SQL Server Authentication.I have been asked if you can disable <strong>Windows Authentication</strong> and the short answer to that is <strong>NO</strong>.</p>
<p>When possible (Microsoft recommends it) use <strong>Windows Authentication</strong> only since it is inherently more secure.For example when using Active Directory you can use <strong>Group Policies</strong> which means we can enforce certain policies like “enforce users to use strong passwords”.Another very useful policy is the “Account Lockout” which locks accounts when too many failed login attempts occur.</p>
<p>We also have <strong>Mixed Authentication Mode</strong> which basically means <strong>Windows Authentication</strong> &amp; <strong>SQL Server Authentication</strong>.</p>
<p>With <strong>SQL Server Authentication</strong> mode SQL Server manages a seperate set of users and groups to give permissions to.SQL Server logins are not based on Windows accounts,usernames and passwords are created by SQL Server and also stored there.</p>
<p>If you choose to have <strong>SQL Server Authentication </strong>you would have to keep credentials for your windows domain and SQL Server.When using the <strong>SQL Server Authentication</strong> make sure you create a long <strong>sa</strong> password and hide it.</p>
<p>In any case you need to have this authentication mode enabled especially if you need to provide access to your users outside the organisation.SQL Server Authentication does not require a domain controller.</p>
<p>We can obviously change the authentication mode from SSMS but we can also type the following in a new query window</p>
<pre><strong>USE master
EXEC xp_instance_regwrite N'HKEY_LOCAL_MACHINE',
N'Software\Microsoft\MSSQLServer\MSSQLServer', N'LoginMode', REG_DWORD, 2

--Windows Authentication mode
USE master
EXEC xp_instance_regwrite N'HKEY_LOCAL_MACHINE',
N'Software\Microsoft\MSSQLServer\MSSQLServer', N'LoginMode', REG_DWORD, 1</strong></pre>
<p>In the <strong>Login auditing </strong>section we can select what kind of logins we will audit.</p>
<p>We can also use <strong>C2 Audit</strong> mode. It configures SQL Server to record attempts to access statements and objects.</p>
<p><strong>C2 Audit</strong> mode can still be configured in SQL Server but is now rarely used. It has a negative performance impact through the generation of large volumes of event information.</p>
<p>When you set this option you should be prepared to run out of disk space.If this happens SQL Server will <strong>shut itself down</strong>.That is why I think it is imperative for everyone to check through jobs and alerts the remaining disk space where our databases live.</p>
<p>12) The next set of options I will be looking into are the ones located in the <strong>Connections</strong> tab.</p>
<p>Have a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/10/conf-15.jpg"><img class="aligncenter size-large wp-image-2278" title="conf-15" alt="" src="http://dotnetstories.files.wordpress.com/2012/10/conf-15.jpg?w=1024&#038;h=550" height="550" width="1024" /></a></p>
<p>You can set the maximum number of concurrent connections.That means if you set the number to anything other zero,then it is going to limit server resources that can service client connections.Be very careful with that setting.</p>
<p>You can also change the setting &#8220;Use query governor to prevent long-running queries&#8221;. This means that if we have long-running queries that use all the resources and put huge pressure on SQL Server resources.</p>
<p>We can limit the execution time of every query be checking this option. The default value is <strong>300.</strong>That is 300 seconds by the way.That means that if the query does not complete within <strong>300</strong> seconds it will be aborted and will rollback the transaction.</p>
<p>You can also a<strong>llow remote connections to the server</strong>.The default value is <strong>600</strong> seconds, that means 10 minutes.This option is enabled by default in SQL Server 2012 but not in previous versions. Do enable this option if you need it. Do not change the default value.</p>
<p>13) The next set of options I will be looking into are the ones located in the <strong>Database Settings</strong> tab.</p>
<p>Have a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/10/conf-17.jpg"><img class="aligncenter size-large wp-image-2281" title="conf-17" alt="" src="http://dotnetstories.files.wordpress.com/2012/10/conf-17.jpg?w=1024&#038;h=550" height="550" width="1024" /></a></p>
<p>The first option is the setting for the <strong>index fill factor.</strong>This specifies the amount of empty space that SQL Server leaves when an index is rebuild or created.This setting should be low (this is a percentage value) for databases that data does not change much and higher in OLTP databases where data is constantly updated,inserted or deleted.</p>
<p>We also have the option to use <strong>compressed backups</strong>.</p>
<p><strong>Backup compression</strong> is available in the <strong>SQL Server 2008 </strong><strong>R2</strong> Standard edition . When we compress backups we<strong> save</strong> <strong>backup space and also reduce backup times</strong>.Obviously we will need more CPU resources to perform compression during a backup or decompression during a restore.One thing to keep in mind is that if we have databases that use data compression or are encrypted, backup compression will not be very efficient. Backup compression does not use row-level or page-level data compression. Backup compression happens at the time of a backup, and it uses its own compression algorithms.</p>
<p>We can also set the  &#8220;<strong>Default backup media retention</strong>&#8221; option.This option specifies the length of time to retain each backup set.</p>
<p>You can change this setting using T-SQL.</p>
<p>In a new query window type and execute the following statement</p>
<pre><strong>USE AdventureWorks2012 ;
GO
EXEC sp_configure 'show advanced options', 1;
GO
RECONFIGURE ;
GO
EXEC sp_configure 'media retention', 90 ;
GO
RECONFIGURE;
GO</strong></pre>
<p>Another option we can set is <strong>Recovery Interval. </strong>This option sets the maximum number of minutes for a database that Microsoft SQL Server needs to recover databases. The default value is <strong>0</strong>, which lets SQL Server to automatically configure the recovery interval.</p>
<p>We can also change the <strong>Data Default locations </strong>option if we choose to.I do not see any reason why you want to change that.This is a setting that you change without fear that you will break something.</p>
<p>14) The next set of options I will be looking into are the ones located in the <strong>Advanced</strong> tab.</p>
<p>Have a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/10/conf-18.jpg"><img class="aligncenter size-large wp-image-2282" title="conf-18" alt="" src="http://dotnetstories.files.wordpress.com/2012/10/conf-18.jpg?w=1024&#038;h=550" height="550" width="1024" /></a></p>
<p>There are many options we can set in this tab. One of them is &#8220;<strong>Allow triggers to fire others</strong>&#8220;.This option is <strong>on</strong> by default. What actually means is that a trigger will allow to fire another trigger.Sometimes if we are not careful with that option we can get in an infinite loop. Another very important setting is the <strong>Optimize for Ad hoc Workloads.</strong>You can find more about this very useful option <a href="http://dotnetstories.wordpress.com/2012/08/10/optimising-sql-server-for-ad-hoc-workloads/" target="_blank">here</a>.</p>
<p>Another option is <strong>Max Degree of Parallelism </strong> and sets the number of processors used for the execution of a query with a parallel plan.</p>
<p>For best practices and recommendations on <strong>Max Degree of Parallelism</strong> have a look at this <a href="http://support.microsoft.com/default.aspx?scid=kb;EN-US;329204" target="_blank">link</a>.</p>
<p>Bear in mind that these are advanced settings and I would not touch them after the initial installation of SQL Server.</p>
<p>15) The next set of options I will be looking into are the ones located in the <strong>Permissions</strong> tab.</p>
<p>Have a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/10/conf-20.jpg"><img class="aligncenter size-large wp-image-2286" title="conf-20" alt="" src="http://dotnetstories.files.wordpress.com/2012/10/conf-20.jpg?w=1024&#038;h=550" height="550" width="1024" /></a></p>
<p>In this tab we see a list of principals and their permissions they have on the server level.I have selected the user fofo-pc\Nikos. This user is administrator in Windows. This user/principal is also in the fixed server role of <strong>sysadmin</strong>.You can see the permissions it has on the server.</p>
<p>Make sure you know exactly what each of the options I mentioned does and only then change them.</p>
<p>Hope it helps!!!</p>
<br />Filed under: <a href='http://dotnetstories.wordpress.com/category/sql-server/'>Sql Server</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2005/'>Sql Server 2005</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2008/'>SQL Server 2008</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2012/'>SQL Server 2012</a> Tagged: <a href='http://dotnetstories.wordpress.com/tag/sql-server-configuration-manager/'>SQL Server Configuration Manager</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dotnetstories.wordpress.com/2241/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dotnetstories.wordpress.com/2241/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=2241&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dotnetstories.wordpress.com/2012/10/28/configuring-sql-server-after-installation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/ecbfd3584f3b2c0c3f528bacdfc15e0a?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96" medium="image">
			<media:title type="html">fofo</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/10/configuration-manager.png" medium="image">
			<media:title type="html">configuration-manager</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/10/conf1.png?w=1024" medium="image">
			<media:title type="html">conf1</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/10/conf-start.jpg?w=1024" medium="image">
			<media:title type="html">conf-start</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/10/conf-2.jpg?w=1024" medium="image">
			<media:title type="html">conf-2</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/10/conf-3.jpg?w=1024" medium="image">
			<media:title type="html">conf-3</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/10/conf-4.jpg?w=1024" medium="image">
			<media:title type="html">conf-4</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/10/conf-5.jpg?w=1024" medium="image">
			<media:title type="html">conf-5</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/10/conf-7.jpg?w=1024" medium="image">
			<media:title type="html">conf-7</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/10/conf-8.jpg?w=1024" medium="image">
			<media:title type="html">conf-8</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/10/conf-9.jpg?w=1024" medium="image">
			<media:title type="html">conf-9</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/10/conf-10.jpg?w=1024" medium="image">
			<media:title type="html">conf-10</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/10/conf-12.jpg?w=1024" medium="image">
			<media:title type="html">conf-12</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/10/conf-11.jpg?w=1024" medium="image">
			<media:title type="html">conf-11</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/10/conf-14.jpg?w=1024" medium="image">
			<media:title type="html">conf-14</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/10/conf-15.jpg?w=1024" medium="image">
			<media:title type="html">conf-15</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/10/conf-17.jpg?w=1024" medium="image">
			<media:title type="html">conf-17</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/10/conf-18.jpg?w=1024" medium="image">
			<media:title type="html">conf-18</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/10/conf-20.jpg?w=1024" medium="image">
			<media:title type="html">conf-20</media:title>
		</media:content>
	</item>
		<item>
		<title>Looking into the Resource database in SQL Server</title>
		<link>http://dotnetstories.wordpress.com/2012/10/27/looking-into-the-resource-database-in-sql-server/</link>
		<comments>http://dotnetstories.wordpress.com/2012/10/27/looking-into-the-resource-database-in-sql-server/#comments</comments>
		<pubDate>Sat, 27 Oct 2012 10:32:34 +0000</pubDate>
		<dc:creator>fofo</dc:creator>
				<category><![CDATA[Sql Server]]></category>
		<category><![CDATA[Sql Server 2005]]></category>
		<category><![CDATA[SQL Server 2008]]></category>
		<category><![CDATA[SQL Server 2012]]></category>
		<category><![CDATA[Resource database]]></category>

		<guid isPermaLink="false">http://dotnetstories.wordpress.com/?p=2236</guid>
		<description><![CDATA[In this post I will be looking into the Resource database in SQL Server and what it contains and why it is very important for the normal operation of SQL Server.

I was talking with SQL Server developers the other day and they have never heard or looked into the Resource Database.

The Resource database is available since the SQL Server 2005 version<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=2236&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>In this post I will be looking into the <strong>Resource</strong> database in SQL Server and what it contains and why it is very important for the normal operation of SQL Server.</p>
<p>I was talking with SQL Server developers the other day and they have never heard or looked into the <strong>Resource Database.</strong></p>
<p>The <strong>Resource</strong> database is available since the SQL Server 2005 version.</p>
<p>I assume that you have installed a version of SQL Server 2008/2012 in your machine.</p>
<p>I also assume that you are familiar with the SQL Server system databases &#8211; <strong>master,model,tempdb,msdb.</strong></p>
<p>Most people have not heard about the <strong>Resource</strong> database because it is sort of &#8220;secret&#8221;,&#8221;hidden&#8221;.Some people look to find it in the SSMS. It won&#8217;t appear there.</p>
<p>You cannot backup the <strong>Resource</strong> database.Each SQL Server instance has one <strong>Resource</strong> database.</p>
<p>It is a <strong>read-only</strong> database that contains all SQL Server system objects.The actual objects are stored in the <strong>Resource</strong> database.These objects appear logically in the <strong>sys schema</strong> of each database attached in our SQL Server instance.</p>
<p>Have a look at the picture below. I have logged into my local SQL Server 2012 Developer Edition and expanded the <strong>System Views</strong> node of the <strong>AdventureWorks2012 database</strong>.</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/10/sysviews.png"><img class="aligncenter size-large wp-image-2238" title="sysviews" alt="" src="http://dotnetstories.files.wordpress.com/2012/10/sysviews.png?w=1024&#038;h=550" height="550" width="1024" /></a></p>
<p>The <strong>Resource</strong> database makes upgrades to new versions of SQL Server easier, faster more efficient.The actual name of the database is <strong>mssqlsystemresource.mdf. </strong>It is located in the <strong>C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQLSERVER\MSSQL\Binn </strong>path. It is the same path that SQL Server was installed in my machine. If you want to back up this database simply copy and paste it somewhere else using the Windows Explorer.</p>
<p>If we want to find the version of the Resource database, in a new query window type,</p>
<p><strong>SELECT SERVERPROPERTY(&#8216;ResourceVersion&#8217;) AS VersionOfResourceDatabase</strong><br />
<strong>GO</strong></p>
<p>In my case the output is</p>
<p><strong>VersionOfResourceDatabase</strong><br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br />
11.00.2845</p>
<p>As you see I am using the built in system function <strong>SERVERPROPERTY</strong></p>
<p>If you want to find out when it was the last time the <strong>Resource</strong> database was updated you can type this bit of TSQL code in a query window in the SSMS.</p>
<p><strong>SELECT SERVERPROPERTY(&#8216;ResourceLastUpdateDateTime&#8217;) AS ResourceDatabaseLastUpdated</strong><br />
<strong>GO</strong></p>
<p>Ιn my case the output is</p>
<p>ResourceDatabaseLastUpdated<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-<br />
2012-09-01 04:02:07.380</p>
<p>If you want to get the SQL definition for the <strong>sys.indexes </strong>object, type the following TSQL code in a new query in SSMS</p>
<p><strong>SELECT OBJECT_DEFINITION(OBJECT_ID(&#8216;sys.indexes&#8217;))AS Sys_Indexes_Definition</strong><br />
<strong>GO</strong></p>
<p>The output is</p>
<p><strong>CREATE VIEW sys.indexes AS</strong><br />
<strong>    SELECT i.id AS object_id,</strong><br />
<strong>        i.name AS name,</strong><br />
<strong>        i.indid AS index_id,</strong><br />
<strong>        i.type,</strong><br />
<strong>        n.name AS type_desc,</strong><br />
<strong>        sysconv(bit, i.status &amp; 0&#215;8) AS is_unique,            &#8211; IS_IND_UNIQUE</strong><br />
<strong>        isnull(ds.indepid, 1 &#8211; (i.status &amp; 0&#215;100)/0&#215;100) AS dat</strong></p>
<p>Hope it helps!!!</p>
<br />Filed under: <a href='http://dotnetstories.wordpress.com/category/sql-server/'>Sql Server</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2005/'>Sql Server 2005</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2008/'>SQL Server 2008</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2012/'>SQL Server 2012</a> Tagged: <a href='http://dotnetstories.wordpress.com/tag/resource-database/'>Resource database</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dotnetstories.wordpress.com/2236/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dotnetstories.wordpress.com/2236/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=2236&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dotnetstories.wordpress.com/2012/10/27/looking-into-the-resource-database-in-sql-server/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/ecbfd3584f3b2c0c3f528bacdfc15e0a?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96" medium="image">
			<media:title type="html">fofo</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/10/sysviews.png?w=1024" medium="image">
			<media:title type="html">sysviews</media:title>
		</media:content>
	</item>
		<item>
		<title>Looking into the SQL Server Log</title>
		<link>http://dotnetstories.wordpress.com/2012/10/17/looking-into-the-sql-server-log/</link>
		<comments>http://dotnetstories.wordpress.com/2012/10/17/looking-into-the-sql-server-log/#comments</comments>
		<pubDate>Wed, 17 Oct 2012 18:35:47 +0000</pubDate>
		<dc:creator>fofo</dc:creator>
				<category><![CDATA[Sql Server]]></category>
		<category><![CDATA[Sql Server 2005]]></category>
		<category><![CDATA[SQL Server 2008]]></category>
		<category><![CDATA[SQL Server 2012]]></category>
		<category><![CDATA[SQL Server Log]]></category>

		<guid isPermaLink="false">http://dotnetstories.wordpress.com/?p=2221</guid>
		<description><![CDATA[In this post I will be looking into the SQL Server Log and how we can use it so we can identify and troubleshoot potential problems.

Recently I asked someone that gave me a call regarding an SQL Server issue, to have a look at the SQL Server Log and he just could not locate it. That is why I decided to write a post for this great tool.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=2221&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>In this post I will be looking into the <strong>SQL Server Log</strong> and how we can use it so we can identify and troubleshoot potential problems.</p>
<p>Recently I asked someone that gave me a call regarding an SQL Server issue, to have a look at the <strong>SQL Server Log</strong> and he just could not locate it. That is why I decided to write a post for this great tool.</p>
<p>I will show you how to access the <strong>SQL Server Log</strong> using various ways.</p>
<p>I will be using SQL Server 2012 Developer Edition but the Express edition will suffice .</p>
<p>I connect to the SQL Server instance using SSMS. I log in as sysadmin.In order to access the <strong>SQL Server Log</strong> I expand the <strong>Management</strong> node and then the <strong>SQL Server Logs</strong> node. One can see all the available logs.We have 7 logs in total. The <strong>Current</strong> and <strong>6 Archived</strong> ones.These logs are for the <strong>SQL Service Service</strong> and not for (e.g) the <strong>Agent Service</strong>.The <strong>Current Log</strong> is the log that SQL Server is currently using.</p>
<p>Please have a look at the picture below.</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/10/sqllog1.png"><img class="aligncenter size-large wp-image-2223" title="sqllog1" alt="" src="http://dotnetstories.files.wordpress.com/2012/10/sqllog1.png?w=1024&#038;h=550" height="550" width="1024" /></a></p>
<p>We can use the <strong>Log File Viewer</strong> to view the contents of the current Log or any archived log.We can filter,search  the log.</p>
<p>Please have a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/10/sqllog3.png"><img class="aligncenter size-large wp-image-2224" title="sqllog3" alt="" src="http://dotnetstories.files.wordpress.com/2012/10/sqllog3.png?w=1024&#038;h=550" height="550" width="1024" /></a></p>
<p>When you stop and start the <strong>SQL Server Service</strong> it archives all of the logs.<strong>Archive #6</strong> means 6 times ago SQL Server was stopped.</p>
<p>I am going to stop and restart SQL Server Service.What happens now is that the oldest log is deleted and a new one is added. <strong>Archive #5</strong> becomes <strong>Archive #6.</strong></p>
<p>Please have a look at the picture below.</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/10/sqllog5.png"><img class="aligncenter size-large wp-image-2227" title="sqllog5" alt="" src="http://dotnetstories.files.wordpress.com/2012/10/sqllog5.png?w=1024&#038;h=550" height="550" width="1024" /></a></p>
<p>We can configure the number of archives that SQL Server will have.Select the <strong>SQL Server Logs</strong> node, right-click on it and select &#8220;<strong>Configure</strong>&#8220;. There you can set the maximum number of error log files.</p>
<p>Have a look at the picture below.</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/10/sqllog2.png"><img class="aligncenter size-large wp-image-2228" title="sqllog2" alt="" src="http://dotnetstories.files.wordpress.com/2012/10/sqllog2.png?w=1024&#038;h=550" height="550" width="1024" /></a></p>
<p>We can use the <strong>xp_ReadErrorLog </strong>stored procedure to read the contents of the <strong>SQL Server Log</strong>.</p>
<p>In a new query window I type</p>
<p><strong>USE master</strong><br />
<strong>GO</strong></p>
<p><strong>EXEC xp_ReadErrorLog</strong></p>
<p>Have a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/10/sqllog7.png"><img class="aligncenter size-large wp-image-2230" title="sqllog7" alt="" src="http://dotnetstories.files.wordpress.com/2012/10/sqllog7.png?w=1024&#038;h=550" height="550" width="1024" /></a></p>
<p>We can access the <strong>SQL Server Log </strong>by going to the installation directory of SQL Server. Then I locate the <strong>Log</strong> folder. Then I open with some sort of  application (Notepad ++) the log I want.</p>
<p>Have a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/10/sqllog10.png"><img class="aligncenter size-large wp-image-2231" title="sqllog10" alt="" src="http://dotnetstories.files.wordpress.com/2012/10/sqllog10.png?w=1024&#038;h=550" height="550" width="1024" /></a></p>
<p>&nbsp;</p>
<p>We can use the <strong>Event Viewer</strong> to have a look at the contents of <strong>SQL Server Log</strong>.</p>
<p>Have a look at the picture below.</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/10/sqllog11.png"><img class="aligncenter size-large wp-image-2233" title="sqllog11" alt="" src="http://dotnetstories.files.wordpress.com/2012/10/sqllog11.png?w=1024&#038;h=550" height="550" width="1024" /></a></p>
<p>I am sure that you realise that the best way to view the SQL Server Log is the <strong>SQL Server Log File Viewer</strong>.</p>
<p>&nbsp;</p>
<p>Hope it helps!!!</p>
<br />Filed under: <a href='http://dotnetstories.wordpress.com/category/sql-server/'>Sql Server</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2005/'>Sql Server 2005</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2008/'>SQL Server 2008</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2012/'>SQL Server 2012</a> Tagged: <a href='http://dotnetstories.wordpress.com/tag/sql-server-log/'>SQL Server Log</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dotnetstories.wordpress.com/2221/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dotnetstories.wordpress.com/2221/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=2221&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dotnetstories.wordpress.com/2012/10/17/looking-into-the-sql-server-log/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/ecbfd3584f3b2c0c3f528bacdfc15e0a?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96" medium="image">
			<media:title type="html">fofo</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/10/sqllog1.png?w=1024" medium="image">
			<media:title type="html">sqllog1</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/10/sqllog3.png?w=1024" medium="image">
			<media:title type="html">sqllog3</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/10/sqllog5.png?w=1024" medium="image">
			<media:title type="html">sqllog5</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/10/sqllog2.png?w=1024" medium="image">
			<media:title type="html">sqllog2</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/10/sqllog7.png?w=1024" medium="image">
			<media:title type="html">sqllog7</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/10/sqllog10.png?w=1024" medium="image">
			<media:title type="html">sqllog10</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/10/sqllog11.png?w=1024" medium="image">
			<media:title type="html">sqllog11</media:title>
		</media:content>
	</item>
		<item>
		<title>Optimising SQL Server for ad hoc workloads</title>
		<link>http://dotnetstories.wordpress.com/2012/08/10/optimising-sql-server-for-ad-hoc-workloads/</link>
		<comments>http://dotnetstories.wordpress.com/2012/08/10/optimising-sql-server-for-ad-hoc-workloads/#comments</comments>
		<pubDate>Thu, 09 Aug 2012 23:39:38 +0000</pubDate>
		<dc:creator>fofo</dc:creator>
				<category><![CDATA[Sql Server]]></category>
		<category><![CDATA[SQL Server 2008]]></category>
		<category><![CDATA[SQL Server 2012]]></category>
		<category><![CDATA[ad hoc workloads]]></category>
		<category><![CDATA[execution plan]]></category>
		<category><![CDATA[Plan Cache]]></category>

		<guid isPermaLink="false">http://dotnetstories.wordpress.com/?p=2195</guid>
		<description><![CDATA[In this post I will be demonstrating with hands-on examples how to optimise your SQL Server for "ad hoc workloads". I will be using the view LookIntoPlanCache that I created in this post .Make sure to read this post if you are not familiar with terms like Query Optimiser,execution plans and Plan Cache.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=2195&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>In this post I will be demonstrating with hands-on examples how to optimise your SQL Server for &#8220;<strong>ad hoc workloads</strong>&#8220;. I will be using the view <strong>LookIntoPlanCache </strong>that I created in this <a href="http://dotnetstories.wordpress.com/2012/08/09/looking-into-the-contents-of-plan-cache-in-sql-server/" target="_blank">post</a> .Make sure to read this post if you are not familiar with terms like <strong>Query Optimiser</strong>,<strong>execution plans</strong> and <strong>Plan Cache.</strong></p>
<p>I will be using <strong>SQL Server 2012 Developer</strong> edition in this post but feel free to use SQL Server 2008/R2 versions.I will be using the <strong>AdventureWorks2012</strong> database.You can download it from the <strong>Codeplex</strong> website.</p>
<p>There are times when the application code (high level code) includes ad-hoc queries/statements and there are no stored procedures in the code when it should.There are many cases when developers do not even use <strong>parameters.</strong> By that I mean that the developer code is written in a way that does not make use of execution plan reuse. Some people will say &#8220;Hey man, I do not care I have enough memory in my SQL Server box&#8221;.Well, SQL Server just loves memory and it is the one hardware resource that we must keep an eye on.</p>
<p>It is never a good argument to claim that there is &#8220;enough hard disk or memory in my system&#8221; so I do not care about fixing things&#8230;</p>
<p>Imagine that you have a reused plan in the plan cache and because of ad-hoc queries/workloads that plan is pushed out of the plan cache.That does not help the performance of the system.When you have many plans in the cache that are not reused at all that can lead to <strong>procedure cache bloat.</strong>In this case you have many entries (execution plans) in the plan cache that are used only one and if this happens far too often <strong>good plans (heavily reused plans) </strong>are forced out of the plan cache.</p>
<p>There is a setting that can be configured in SQL Server to approach such issues when we have lots of adhoc queries.It is called <strong>Optimize for Ad Hoc Workloads </strong>and you can enable it by using the system stored procedure <strong>sp_configure. </strong>By default this setting is <strong>off.</strong>When enabling this feature you must keep in mind that it will affect newly created plans and not the ones that are already placed in the plan cache.</p>
<p>Let me move on with my hands-on examples.</p>
<p>Connect to an instance of SQL Server. Type (copy paste) the code below.Do not execute anything yet.</p>
<pre class="brush: sql; title: ; notranslate">

USE AdventureWorks2012;
GO

IF OBJECT_ID ( 'Person.uspGetPersons', 'P' ) IS NOT NULL
DROP PROCEDURE Person.uspGetPersons;
GO

CREATE PROCEDURE Person.uspGetPersons
@LastName nvarchar(50),
@FirstName nvarchar(50)
AS

SET NOCOUNT ON;
SELECT FirstName, LastName, Title
FROM Person.Person
WHERE FirstName = @FirstName AND LastName = @LastName;
GO

DBCC FREEPROCCACHE

SELECT * FROM dbo.LookIntoPlanCache

EXEC Person.uspGetPersons N'Miller', N'Dylan'
GO
EXEC Person.uspGetPersons N'Duffy', N'Terry';
GO
EXEC person.uspgetpersons N'Brown', N'Kevin'
GO

SELECT * FROM dbo.LookIntoPlanCache

</pre>
<p>I execute line <strong>1-2 </strong>to make <strong>AdventureWorks2012</strong> the current database. Then I will create a simple stored procedure in lines <strong>8-17.</strong>In lines <strong>4-6 </strong>I just check to see if the stored procedure already exists on the system. Then in line <strong>19</strong> I clear the plan cache and use the <strong>LookIntoPlanCache </strong>view to see the contents of the plan cache.There will be no contents at this stage.</p>
<p>Then I execute the lines <strong>23-28 </strong>.There are 3 calls to the stored procedure.Please note that I have typed the name of the procedure with 3 different ways.</p>
<p>Finally I execute the <strong>LookIntoPlanCache</strong> view to check the contents of plan cache once more.Have a look at the picture below to see the results I got.</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/08/ad-hoc-new.jpg"><img class="aligncenter size-large wp-image-2205" title="ad-hoc-new" src="http://dotnetstories.files.wordpress.com/2012/08/ad-hoc-new.jpg?w=1024&#038;h=550" alt="" width="1024" height="550" /></a></p>
<p>The plan for the stored procedure is compiled, cached and reused.Having typed the stored procedure&#8217;s name in a different way does not matter .</p>
<p>In a new query window type (copy paste) the following t-sql code.Do not execute anything yet.</p>
<pre class="brush: sql; title: ; notranslate">

USE AdventureWorks2012
GO

DBCC FREEPROCCACHE

SELECT  *
FROM    [AdventureWorks2012].[Sales].[Currency]
GO
SELECT  *
FROM    [AdventureWorks2012].[Sales].[Currency];
GO

SELECT  *
FROM    [dbo].[LookIntoPlanCache]
GO

DBCC FREEPROCCACHE

SELECT  *
FROM    [AdventureWorks2012].[Sales].[Currency]
SELECT  *
FROM    [AdventureWorks2012].[Sales].[Currency];

SELECT  *
FROM    [dbo].[LookIntoPlanCache]
GO

</pre>
<p>Execute line <strong>4.</strong>Once more I clear the contents of plan cache.Then I execute 2 simple <strong>SELECT</strong> statements.Please note that I in the second <strong>SELECT</strong> statement I have added a <strong>&#8216;;&#8217;.</strong></p>
<p>Ηave a look at the picture below to see the results returned with the execution of the statements.</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/08/ad-hoc-1.jpg"><img class="aligncenter size-large wp-image-2196" title="AD-HOC-1" src="http://dotnetstories.files.wordpress.com/2012/08/ad-hoc-1.jpg?w=1024&#038;h=550" alt="" width="1024" height="550" /></a></p>
<p>You can see that I get 2 execution plans for an identical query.The first thing to note here is that we should try and write T-SQL ad hoc queries in the same way. The second thing is that ad-hoc statements get different treatment compared to stored procedures.</p>
<p>Then in line <strong>17 </strong>I clear the cache once more.Then I execute (line <strong>19-22</strong>) the two <strong>SELECT</strong> statements in the same batch.Then I look into the contents of the plan cache using the <strong>LookIntoPlanCache</strong> view.</p>
<p>Have  a look in the picture below to see what my results are.</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/08/ad-hoc-2.jpg"><img class="aligncenter size-large wp-image-2197" title="AD-HOC-2" src="http://dotnetstories.files.wordpress.com/2012/08/ad-hoc-2.jpg?w=1024&#038;h=550" alt="" width="1024" height="550" /></a></p>
<p>It is one entry this time in the plan cache but the plan size is twice the size.</p>
<p>Let&#8217;s see now how we can optimise SQL Server for ad-hoc workloads</p>
<p>In a new query window type (copy &#8211; paste the following).Do not execute anything yet.</p>
<pre class="brush: sql; title: ; notranslate">

USE AdventureWorks2012
GO

DBCC FREEPROCCACHE

SELECT  *
FROM    [AdventureWorks2012].[Production].[ProductModel]
GO

SELECT  *
FROM    [AdventureWorks2012].[Purchasing].[ShipMethod]
GO

SELECT  *
FROM    [AdventureWorks2012].[Sales].[Currency]
GO

SELECT  *
FROM    [dbo].[LookIntoPlanCache]
GO

sp_configure 'show advanced options', 1;
GO
RECONFIGURE
GO
sp_configure 'optimize for ad hoc workloads', 1;
GO
RECONFIGURE

SELECT  *
FROM    [AdventureWorks2012].[Production].[ProductModel]
GO

SELECT  *
FROM    [AdventureWorks2012].[Purchasing].[ShipMethod]
GO

SELECT  *
FROM    [AdventureWorks2012].[Sales].[Currency]
GO

SELECT  *
FROM    [dbo].[LookIntoPlanCache]
GO

</pre>
<p>I execute line <strong>1-2</strong> to make AdventureWorks2012 the current database.Then I clear the cache once more and execute three <strong>SELECT</strong> statements (lines <strong>6-16</strong>) (ad-hoc queries).</p>
<p>Then in lines <strong>18-20 </strong>I look into the plan cache once more.</p>
<p>Have a look at the picture below to see the results I have got.</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/08/opt1.jpg"><img class="aligncenter size-large wp-image-2211" title="opt1" src="http://dotnetstories.files.wordpress.com/2012/08/opt1.jpg?w=1024&#038;h=550" alt="" width="1024" height="550" /></a></p>
<p>Those results were the ones I expected. If most of your workload or even 25-40% percent of your workload is <strong>ad-hoc</strong> queries you should optimise SQL Server differently.</p>
<p>Execute lines <strong>23-29. </strong>That will enable SQL Server to optimise execution plans for ad-hoc queries.Then execute the same 3 <strong>SELECT</strong> statements (lines <strong>31-41</strong>) as before.Finally execute the view (lines <strong>43-45</strong>) to look into the contents of plan cache once more.</p>
<p>Have a look at the picture below to see the results I have got.</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/08/opt2.jpg"><img class="aligncenter size-large wp-image-2212" title="opt2" src="http://dotnetstories.files.wordpress.com/2012/08/opt2.jpg?w=1024&#038;h=550" alt="" width="1024" height="550" /></a></p>
<p>Instead of a compiled plan I have a <strong>Compiled Plan Stub</strong>.The size that each of these stubs is <strong>272</strong> bytes. That size  is significantly less (150 times) than the size that the original compiled plans occupied. What will happen if I run again the 3 <strong>SELECT</strong> statements in lines <strong>31-41?</strong> When I execute these lines again along with the view that looks the contents of the plan cache, I get the following results</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/08/opt3.jpg"><img class="aligncenter size-large wp-image-2213" title="opt3" src="http://dotnetstories.files.wordpress.com/2012/08/opt3.jpg?w=1024&#038;h=550" alt="" width="1024" height="550" /></a></p>
<p>As you can see now there are no more <strong>Compiled Plan Stubs </strong>for the 3 <strong>SELECT</strong> statements but compiled plans that take &#8220;full&#8221; size in the plan cache.So, when the same query is executed for a <strong>second</strong> time the full execution plan is generated.</p>
<p>To recap, make sure you write your code using stored procedures and parameterised queries. If you do not have access to the high level code and that code has been written by an &#8220;ad-hoc query&#8221; <em>enthusiast</em> make sure you optimise SQL Server accordingly,thus to optimise SQL Server for <strong>ad-hoc</strong> workloads.</p>
<p>Hope it helps!!!!</p>
<br />Filed under: <a href='http://dotnetstories.wordpress.com/category/sql-server/'>Sql Server</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2008/'>SQL Server 2008</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2012/'>SQL Server 2012</a> Tagged: <a href='http://dotnetstories.wordpress.com/tag/ad-hoc-workloads/'>ad hoc workloads</a>, <a href='http://dotnetstories.wordpress.com/tag/execution-plan/'>execution plan</a>, <a href='http://dotnetstories.wordpress.com/tag/plan-cache/'>Plan Cache</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dotnetstories.wordpress.com/2195/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dotnetstories.wordpress.com/2195/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=2195&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dotnetstories.wordpress.com/2012/08/10/optimising-sql-server-for-ad-hoc-workloads/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/ecbfd3584f3b2c0c3f528bacdfc15e0a?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96" medium="image">
			<media:title type="html">fofo</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/08/ad-hoc-new.jpg?w=1024" medium="image">
			<media:title type="html">ad-hoc-new</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/08/ad-hoc-1.jpg?w=1024" medium="image">
			<media:title type="html">AD-HOC-1</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/08/ad-hoc-2.jpg?w=1024" medium="image">
			<media:title type="html">AD-HOC-2</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/08/opt1.jpg?w=1024" medium="image">
			<media:title type="html">opt1</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/08/opt2.jpg?w=1024" medium="image">
			<media:title type="html">opt2</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/08/opt3.jpg?w=1024" medium="image">
			<media:title type="html">opt3</media:title>
		</media:content>
	</item>
		<item>
		<title>Looking into the contents of Plan Cache in SQL Server</title>
		<link>http://dotnetstories.wordpress.com/2012/08/09/looking-into-the-contents-of-plan-cache-in-sql-server/</link>
		<comments>http://dotnetstories.wordpress.com/2012/08/09/looking-into-the-contents-of-plan-cache-in-sql-server/#comments</comments>
		<pubDate>Wed, 08 Aug 2012 23:37:54 +0000</pubDate>
		<dc:creator>fofo</dc:creator>
				<category><![CDATA[Sql Server]]></category>
		<category><![CDATA[Sql Server 2005]]></category>
		<category><![CDATA[SQL Server 2008]]></category>
		<category><![CDATA[SQL Server 2012]]></category>
		<category><![CDATA[DMV]]></category>
		<category><![CDATA[execution plan]]></category>
		<category><![CDATA[Plan Cache]]></category>

		<guid isPermaLink="false">http://dotnetstories.wordpress.com/?p=2182</guid>
		<description><![CDATA[In this post I will be demonstrating with hands-on examples how to look into the contents of Plan Cache.  I will be explaining what Plan Cache is and the process of query optimization in detail. Then I will use some DMVs and DMFs to create a custom view that will return the contents of the plan cache.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=2182&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>In this post I will be demonstrating with hands-on examples how to look into the contents of <strong>Plan Cache</strong>.  I will be explaining what the <strong>Plan Cache (Procedure Cache)</strong> is, the process of query optimization and the generation of execution plans in detail. Then I will use some <strong>DMVs</strong> to create a custom view that will return the contents of the plan cache.</p>
<p>I will be using <strong>SQL Server 2012 Developer</strong> edition in this post but feel free to use SQL Server 2005,2008 versions.I will be using the <strong>AdventureWorks2012</strong> database.You can download it from the <strong>Codeplex</strong> website.</p>
<p>Let me talk a bit about query optimisation and T-SQL. T-SQL is <span style="text-decoration:underline;">declarative</span> by nature. When we write T-SQL statements in a query window in SSMS and execute them,we just say to SQL Server &#8220;I want these results back&#8221;. We do not provide any details on how the results will be returned.If there was nothing else between our T-SQL code and the <strong>SQL Server Database</strong> engine, we simply would not get any results back.Luckily for us there is a very important component, the <strong>Query Optimizer </strong>that generates an <strong>imperative</strong> plan. By saying imperative I mean <strong>detailed. </strong>This plan that is called <strong>execution plan</strong> is what is actually executed by the relational engine.The query optimiser will not look for a perfect plan.It is a cost-based optimiser that must find an efficient plan.The optimiser when deciding upon the execution plan will take in to consideration the type of operations,statistics (must always be up to date),indexes,hardware resources (number of CPUs ,available memory),SQL Server edition,number of active concurrent connections and query hints.</p>
<p>The following steps  take place upon query receival from SQL Server:</p>
<ul>
<li>Parser</li>
<li>Algebriser</li>
<li>Optimiser</li>
<li>Execution plan generation – caching</li>
<li>Query execution = execution plan execution</li>
</ul>
<p>The <strong>Parser</strong> checks the an incoming query validating it for correct syntax.The query is terminated if a syntax error is detected.On validating a query the parser generates the <strong>parse tree</strong>.</p>
<p>The A<strong>lgebriser</strong> takes the parse tree and resolves all the names of the different objects and data types.The output of all these verifications and resolutions is a binary set of data called a <strong>query processor tree</strong>.</p>
<p>Based on the query tree the <strong>Optimiser</strong> finds the optimal solution to execute the query.</p>
<p>Plans are then cached and reused so you do not have to create a new plan every time a query is executed.That will improve the performance significantly since execution plans are costly in both time and resources.So, the first time a query is executed the plan is created,compiled and placed in the procedure-plan cache. When there are subsequent requests for that query,the query plan will be used.</p>
<p>Connect to an instance of SQL Server. Type (copy paste) the code below and execute it. Make sure you include the execution plan ( <strong>CTRL+M</strong> )</p>
<pre class="brush: sql; title: ; notranslate">

USE [AdventureWorks2012]

GO

SELECT  Sales.SalesOrderDetail.OrderQty ,
Sales.SalesOrderDetail.SpecialOfferID ,
Sales.SalesOrderDetail.UnitPrice ,
Sales.SalesOrderDetail.UnitPriceDiscount ,
Sales.SalesOrderHeader.Status ,
Sales.SalesOrderHeader.AccountNumber
FROM    Sales.SalesOrderDetail
INNER JOIN Sales.SalesOrderHeader ON Sales.SalesOrderDetail.SalesOrderID = Sales.SalesOrderHeader.SalesOrderID
ORDER BY Sales.SalesOrderDetail.UnitPrice DESC

</pre>
<p>Ηave a look at the picture below to see the results returned with the execution of the statement above along with the execution plan.</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/08/execplan1.jpg"><img class="aligncenter size-large wp-image-2184" title="execplan1" src="http://dotnetstories.files.wordpress.com/2012/08/execplan1.jpg?w=1024&#038;h=550" alt="" width="1024" height="550" /></a></p>
<p>I will not be looking into the various components of this execution plan and what they mean. You can hover over each of these components and get information like <strong>IO</strong> cost and <strong>CPU</strong> cost.</p>
<p>Most SQL Server developers and administrators I know, strongly believe that DMVs are the best feature that has been added to SQL Server since 2005.I feel exactly the same way.Please have a look at this <a href="http://dotnetstories.wordpress.com/2012/07/19/looking-into-new-and-enhanced-dmvs-and-dmfs/" target="_blank">post</a> of mine that I talk about new <strong>DMVs</strong> and <strong>DMFs</strong> in <strong>SQL Server 2008 SP1</strong> and <strong>SQL Server 2012</strong>.</p>
<p>Wouldn&#8217;t it be nice to be able to look into the contents of the cache?We can do that by using various <strong>DMVs</strong>.</p>
<p>In a new query window type the following.Do not execute anything yet.</p>
<pre class="brush: sql; title: ; notranslate">

USE AdventureWorks2012
GO
--DO NOT USE THIS COMMAND IN A PRODUCTION SYSTEM

DBCC FREEPROCCACHE

SELECT * FROM sys.dm_exec_cached_plans

SELECT * FROM Person.Person

SELECT * FROM sys.dm_exec_cached_plans

select * from sys.dm_exec_query_plan(0x060005003A0E56049009B9FF0100000001000000000000000000000000000000000000000000000000000000);

select * from sys.dm_exec_sql_text(0x060005003A0E56049009B9FF0100000001000000000000000000000000000000000000000000000000000000)

</pre>
<p>Execute line <strong>1-5</strong> first. I clear the contents of plan cache with the <strong>DBCC FREEPROCCACHE</strong> command.</p>
<p>Then (in line <strong>7</strong>) I use the <strong>sys.dm_exec_cached_plans </strong>dynamic view that returns a row for each query plan that is cached by SQL Server.</p>
<p>Please have a look in this <a href="http://msdn.microsoft.com/en-us/library/ms187404.aspx" target="_blank">link</a> for more information about the <strong>sys.dm_exec_cached_plans view.</strong></p>
<p>At this time I do not expect to see anything in the plan cache.Then in line<strong> 9</strong> I execute a simple SELECT statement..</p>
<p>An execution plan is generated. I run again the <strong>sys.dm_exec_cached_plans </strong>(line<strong> 11</strong>) view and I get back a row for this cached plan. I find out the relevant handle.</p>
<p>In my case is &#8220;0x060005003A0E56049029B9FF0100000001000000000000000000000000000000000000000000000000000000&#8243;</p>
<p>In your case it will be different so make sure you use yours instead.</p>
<p>Now I want to find the actual execution plan.We can use the <strong>sys.dm_exec_query_plan </strong>dynamic view.This view returns the plan in XML format for the batch specified by the <strong>plan handle</strong>.</p>
<p>Please have a look at this <a href="http://msdn.microsoft.com/en-us/library/ms189747.aspx" target="_blank">link</a> for more information about the <strong>sys.dm_exec_query_plan </strong>view.I need to provide the handle as an input parameter.</p>
<p>I execute line <strong>13 </strong>and I get the query plan in XML format for the sql query &#8220;Select * from Person.Person&#8221;</p>
<p>Then I want to find the SQL statement that created that plan.I will use the <strong>sys.dm_exec_sql_text </strong>dynamic view.</p>
<p>Please have a look at this <a href="http://msdn.microsoft.com/en-us/library/ms181929.aspx" target="_blank">link</a> for more information about the <strong>sys.dm_exec_sql_text</strong> dynamic view.I need to provide the handle as an input parameter.</p>
<p>I execute the statement in line <strong>15.</strong>The results I get is something like this</p>
<p><strong>dbid    objectid    number    encrypted    text</strong><br />
5              NULL          NULL                0              SELECT * FROM Person.Person</p>
<p>So I managed to get the handle of the SQL query and then get the execution plan for this query and finally the text for the query itself.</p>
<p>Well that seems like a lot of work and I will demonstrate how to combine all these views into a custom-helper view.</p>
<p>In the same query window type (copy paste) and execute the following.</p>
<pre class="brush: sql; title: ; notranslate">

CREATE FUNCTION ExecPlanAndSqlText2 (@planhandle varbinary (MAX))
RETURNS TABLE
AS
RETURN SELECT EST.text,ECP.objtype,ECP.cacheobjtype,ECP.usecounts,ECP.size_in_bytes,EQP.query_plan FROM sys.dm_exec_sql_text(@planhandle) AS EST CROSS JOIN
sys.dm_exec_query_plan(@planhandle) AS EQP
JOIN sys.dm_exec_cached_plans AS ECP
ON ECP.plan_handle = @planhandle;
GO

SELECT * FROM ExecPlanAndSqlText(0x060005003A0E56049029B9FF0100000001000000000000000000000000000000000000000000000000000000);

</pre>
<p>I create a helper function first using all the dynamic management views and their relevant columns and then I pass as an input parameter the handle.</p>
<p>When I run the last SELECT statement I get the following results.</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/08/execplan2.jpg"><img class="aligncenter size-large wp-image-2189" title="execplan2" src="http://dotnetstories.files.wordpress.com/2012/08/execplan2.jpg?w=1024&#038;h=550" alt="" width="1024" height="550" /></a></p>
<p>As you can see now I have all the information I need in one function.</p>
<p>In the same query window type (copy and paste) the code below.Execute the code</p>
<pre class="brush: sql; title: ; notranslate">

CREATE VIEW LookIntoPlanCache
AS
SELECT  EPAST.*
FROM    sys.dm_exec_cached_plans AS ECP
CROSS APPLY ExecPlanAndSqlText(ECP.plan_handle) AS EPAST

SELECT * FROM LookIntoPlanCache

</pre>
<p>I create a view (<strong>LookIntoPlanCache</strong>) that collects all the plan handles and passes them to our user defined function. Finally I execute a simple SELECT to see the contents of the view.</p>
<p>When I do that  I get the following results.</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/08/execplan3.jpg"><img class="aligncenter size-large wp-image-2190" title="execplan3" src="http://dotnetstories.files.wordpress.com/2012/08/execplan3.jpg?w=1024&#038;h=550" alt="" width="1024" height="550" /></a></p>
<p>Finally I have the SQL query text,the execution plan and the times this plan was used in one view.</p>
<p>Make sure you use this view to look into the contents of your <strong>Plan Cache</strong>.It works for every system.</p>
<p>Hope it helps!!!!</p>
<br />Filed under: <a href='http://dotnetstories.wordpress.com/category/sql-server/'>Sql Server</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2005/'>Sql Server 2005</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2008/'>SQL Server 2008</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2012/'>SQL Server 2012</a> Tagged: <a href='http://dotnetstories.wordpress.com/tag/dmv/'>DMV</a>, <a href='http://dotnetstories.wordpress.com/tag/execution-plan/'>execution plan</a>, <a href='http://dotnetstories.wordpress.com/tag/plan-cache/'>Plan Cache</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dotnetstories.wordpress.com/2182/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dotnetstories.wordpress.com/2182/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=2182&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dotnetstories.wordpress.com/2012/08/09/looking-into-the-contents-of-plan-cache-in-sql-server/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/ecbfd3584f3b2c0c3f528bacdfc15e0a?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96" medium="image">
			<media:title type="html">fofo</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/08/execplan1.jpg?w=1024" medium="image">
			<media:title type="html">execplan1</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/08/execplan2.jpg?w=1024" medium="image">
			<media:title type="html">execplan2</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/08/execplan3.jpg?w=1024" medium="image">
			<media:title type="html">execplan3</media:title>
		</media:content>
	</item>
		<item>
		<title>Using SQL Server Best Practices Analyzer in SQL Server</title>
		<link>http://dotnetstories.wordpress.com/2012/08/07/using-sql-server-best-practices-analyzer-in-sql-server/</link>
		<comments>http://dotnetstories.wordpress.com/2012/08/07/using-sql-server-best-practices-analyzer-in-sql-server/#comments</comments>
		<pubDate>Tue, 07 Aug 2012 17:22:48 +0000</pubDate>
		<dc:creator>fofo</dc:creator>
				<category><![CDATA[Sql Server]]></category>
		<category><![CDATA[Sql Server 2005]]></category>
		<category><![CDATA[SQL Server 2008]]></category>
		<category><![CDATA[SQL Server 2012]]></category>
		<category><![CDATA[SQL Server 2012 Best Practices Analyzer]]></category>

		<guid isPermaLink="false">http://dotnetstories.wordpress.com/?p=2167</guid>
		<description><![CDATA[In this rather short post, I would like to talk about a very handy and easy to use tool (SQL Server 2012 Best Practices Analyzer-SBPA) that provides a lot of information about all the SQL Server 2012 components installed on the system and checks if the configurations are setup according to the recommended best practices.This tool is available for previous versions of SQL Server and SQL Server 2012<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=2167&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>In this rather short post, I would like to talk about a very handy and easy to use tool (<strong>SQL Server 2012 Best Practices Analyzer-SBPA</strong>) that provides a lot of information about all the SQL Server 2012 components installed on the system and checks if the configurations are setup according to the recommended best practices.This tool is available for previous versions of SQL Server and SQL Server 2012.You can download the SQL Server 2012 version of the tool from this <a href="http://www.microsoft.com/en-us/download/details.aspx?id=29302" target="_blank">link</a>.</p>
<p>The supported operating systems are <strong>Windows 7</strong>, <strong>Windows Server 2008</strong>, <strong>Windows Server 2008 R2</strong>.</p>
<p>There is a chance that the SBPA will fail during installation. You probably will have to <a href="http://www.microsoft.com/en-us/download/details.aspx?id=16475" target="_blank">download</a> and install <strong>Microsoft Baseline Configuration Analyzer V2.0</strong> first.</p>
<p>After I install the SBPA tool, I launch it (<strong>Admin Priveleges</strong>).</p>
<p>Have a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/08/sbap1.jpg"><img class="aligncenter size-large wp-image-2170" title="sbap1" src="http://dotnetstories.files.wordpress.com/2012/08/sbap1.jpg?w=1024&#038;h=576" alt="" width="1024" height="576" /></a></p>
<p>Then I click <strong>Start Scan.</strong>A new window appears.In this window I can specify the instance name of the SQL Server I want to scan and then choose what SQL Server components I want also to analyse. In my case I specified the <strong>SQL Server Engine &amp; Setup</strong> of my SQL Server Developer 2012 version.</p>
<p>Have a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/08/sbap2.jpg"><img class="aligncenter size-large wp-image-2171" title="sbap2" src="http://dotnetstories.files.wordpress.com/2012/08/sbap2.jpg?w=1024&#038;h=550" alt="" width="1024" height="550" /></a></p>
<p>Then I click <strong>Start Scan </strong>once more.Then the scanning begins.Upon completion of the scanning by the <strong>SBPA</strong> <strong>2.0,</strong> a report is generated and is categorized into<strong> Errors</strong>, <strong>Warnings</strong> and <strong>Compliants. </strong>Have a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/08/sbap3.jpg"><img class="aligncenter size-large wp-image-2172" title="sbap3" src="http://dotnetstories.files.wordpress.com/2012/08/sbap3.jpg?w=1024&#038;h=550" alt="" width="1024" height="550" /></a></p>
<p>You will be able to see different errors that exist on the SQL Server instance in relation with the rules configured in the <strong>Best Practices Analyzer.</strong>We can also click the detailed explanation for each error. In my case the error I decided to look into <strong>tempdb-wrong configuration</strong> error.There is a also a resolution for the error.</p>
<p>Have a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/08/sbap4.jpg"><img class="aligncenter size-large wp-image-2173" title="sbap4" src="http://dotnetstories.files.wordpress.com/2012/08/sbap4.jpg?w=1024&#038;h=550" alt="" width="1024" height="550" /></a></p>
<p>You can expand the <strong>Warning</strong> section of the report.In my case one of the warnings is that the<strong> AdventureWorks2012 </strong>database is configured with the<strong> Simple Recovery Model.</strong>I will have to decide depending on my disaster/recovery requirements if I need to change that setting or not.</p>
<p>Have a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/08/sbap5.jpg"><img class="aligncenter size-large wp-image-2174" title="sbap5" src="http://dotnetstories.files.wordpress.com/2012/08/sbap5.jpg?w=1024&#038;h=550" alt="" width="1024" height="550" /></a></p>
<p>To recap, this tool validates an SQL Server instance against a fixed set of built-in rules that can help us identify common installation and configuration issues.</p>
<p>Hope it helps!!!</p>
<br />Filed under: <a href='http://dotnetstories.wordpress.com/category/sql-server/'>Sql Server</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2005/'>Sql Server 2005</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2008/'>SQL Server 2008</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2012/'>SQL Server 2012</a> Tagged: <a href='http://dotnetstories.wordpress.com/tag/sql-server-2012-best-practices-analyzer/'>SQL Server 2012 Best Practices Analyzer</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dotnetstories.wordpress.com/2167/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dotnetstories.wordpress.com/2167/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=2167&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dotnetstories.wordpress.com/2012/08/07/using-sql-server-best-practices-analyzer-in-sql-server/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/ecbfd3584f3b2c0c3f528bacdfc15e0a?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96" medium="image">
			<media:title type="html">fofo</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/08/sbap1.jpg?w=1024" medium="image">
			<media:title type="html">sbap1</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/08/sbap2.jpg?w=1024" medium="image">
			<media:title type="html">sbap2</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/08/sbap3.jpg?w=1024" medium="image">
			<media:title type="html">sbap3</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/08/sbap4.jpg?w=1024" medium="image">
			<media:title type="html">sbap4</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/08/sbap5.jpg?w=1024" medium="image">
			<media:title type="html">sbap5</media:title>
		</media:content>
	</item>
		<item>
		<title>Looking into logical query processing in SQL Server</title>
		<link>http://dotnetstories.wordpress.com/2012/08/07/looking-into-logical-query-processing-in-sql-server/</link>
		<comments>http://dotnetstories.wordpress.com/2012/08/07/looking-into-logical-query-processing-in-sql-server/#comments</comments>
		<pubDate>Tue, 07 Aug 2012 12:40:02 +0000</pubDate>
		<dc:creator>fofo</dc:creator>
				<category><![CDATA[Sql Server]]></category>
		<category><![CDATA[Sql Server 2005]]></category>
		<category><![CDATA[SQL Server 2008]]></category>
		<category><![CDATA[SQL Server 2012]]></category>
		<category><![CDATA[logical query execution]]></category>

		<guid isPermaLink="false">http://dotnetstories.wordpress.com/?p=2161</guid>
		<description><![CDATA[A few weeks back, I received an email from a good colleague of mine that was really puzzled with the execution of a rather simple query.Finally he gave up and draw the conclusion that there was some sort of a bug in SQL Server.I tried this query myself and it did not work.The query was [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=2161&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>A few weeks back, I received an email from a good colleague of mine that was really puzzled with the execution of a rather simple query.Finally he gave up and draw the conclusion that there was some sort of a bug in SQL Server.I tried this query myself and it did not work.The query was pretty simple and I could not see why it was not working,in the first place.The answer to this &#8220;strange&#8221; behavior of  SQL Server is not strange at all. We write SQL queries using the T-SQL syntax.The way we form a SQL query is not the same way SQL Server logically (internally) executes the query.</p>
<p>Let&#8217;s see that with a hands-on example.</p>
<p>I am using the <strong>AdventureWorks2012</strong> database.Make sure you visit the <strong>CodePlex</strong> website and download it.Then attach it (restore it) to your local SQL Server instance.</p>
<p>I will be using SQL Server 2012 Developer Edition but the Express edition will suffice .</p>
<p>I connect to the SQL Server instance.</p>
<p>Type (copy and paste the code below) the code below in a new query window in SSMS. Make sure you review it. At first glance there is nothing wrong.Try to execute the query.</p>
<pre class="brush: sql; title: ; notranslate">

USE AdventureWorks2012
GO

SELECT  [JobTitle] ,
[BirthDate] ,
[MaritalStatus] ,
[Gender] ,
[HireDate] ,
[VacationHours] ,
[SickLeaveHours] ,
( [VacationHours] + [SickLeaveHours] ) AS [TotalLeaveHours]
FROM    [AdventureWorks2012].[HumanResources].[Employee]
WHERE   [TotalLeaveHours] &gt;=70
</pre>
<p>You will receive an error.</p>
<p><em><strong>Invalid column name &#8216;TotalLeaveHours&#8217;.</strong></em></p>
<p>SQL Server executes internally the SQL query with a different logic. It does not follow the order of the statements as they are depicted in the SQL Query.The order of execution is the following</p>
<ul>
<li>FROM</li>
<li>ON</li>
<li>JOIN</li>
<li>WHERE</li>
<li>GROUP BY</li>
<li>HAVING</li>
<li>SELECT</li>
<li>DISTINCT</li>
<li>ORDER BY</li>
<li>TOP</li>
</ul>
<p>You can see now why my query failed. <strong>SELECT</strong> is evaluated after the <strong>WHERE</strong> clause.</p>
<p>One way to visualize what I am explaining, please have  a look (download) <a href="http://tsql.solidq.com/books/insidetsql2008/Logical%20Query%20Processing%20Poster.pdf" target="_blank">this</a> poster.</p>
<p>Let&#8217;s rewrite the previous statement.</p>
<p>Execute the code below</p>
<pre class="brush: sql; title: ; notranslate">

USE AdventureWorks2012
GO

SELECT  [JobTitle] ,
[BirthDate] ,
[MaritalStatus] ,
[Gender] ,
[HireDate] ,
[VacationHours] ,
[SickLeaveHours] ,
( [VacationHours] + [SickLeaveHours] ) AS [TotalLeaveHours]
FROM    [AdventureWorks2012].[HumanResources].[Employee]
ORDER BY [TotalLeaveHours] DESC

</pre>
<p>You can see now why my query succeeds. <strong>ORDER BY</strong> is evaluated after the <strong>SELECT</strong> clause.</p>
<p>Another excellent article on the subject is this <a href="http://autoexec.gr/blogs/antonch/archive/2009/08/26/sql-query.aspxhttp://" target="_blank">one</a> (greek audience only :P).</p>
<p>I am big fan of <strong>LINQ to SQL</strong> and <strong>Entity Framework</strong>. I am sure you have written at least one query using the LINQ syntax.Have a look at the ( snippet )code below</p>
<p><strong>var companies = from comp in ctx.Companies</strong><br />
<strong>                                   where comp.CompanyName.StartsWith(&#8220;Ad&#8221;)</strong><br />
<strong>                                   orderby comp.CompanyName</strong><br />
<strong>                                   select new { comp.CompanyID, comp.CompanyName };</strong></p>
<p>I am sure you can see the similarities between LINQ query syntax and how SQL Server processes a query internally. So if you think that LINQ syntax is &#8220;strange&#8221; you know now why we write the LINQ queries the way we do.<br />
Hope it helps!!!</p>
<br />Filed under: <a href='http://dotnetstories.wordpress.com/category/sql-server/'>Sql Server</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2005/'>Sql Server 2005</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2008/'>SQL Server 2008</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2012/'>SQL Server 2012</a> Tagged: <a href='http://dotnetstories.wordpress.com/tag/logical-query-execution/'>logical query execution</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dotnetstories.wordpress.com/2161/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dotnetstories.wordpress.com/2161/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=2161&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dotnetstories.wordpress.com/2012/08/07/looking-into-logical-query-processing-in-sql-server/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/ecbfd3584f3b2c0c3f528bacdfc15e0a?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96" medium="image">
			<media:title type="html">fofo</media:title>
		</media:content>
	</item>
		<item>
		<title>Using Query Paging in SQL Server 2012</title>
		<link>http://dotnetstories.wordpress.com/2012/08/07/using-query-paging-in-sql-server-2012/</link>
		<comments>http://dotnetstories.wordpress.com/2012/08/07/using-query-paging-in-sql-server-2012/#comments</comments>
		<pubDate>Tue, 07 Aug 2012 00:08:50 +0000</pubDate>
		<dc:creator>fofo</dc:creator>
				<category><![CDATA[Sql Server]]></category>
		<category><![CDATA[SQL Server 2012]]></category>
		<category><![CDATA[FETCH]]></category>
		<category><![CDATA[OFFSET]]></category>
		<category><![CDATA[Query Paging]]></category>

		<guid isPermaLink="false">http://dotnetstories.wordpress.com/?p=2155</guid>
		<description><![CDATA[In this post I will be looking into a new feature in SQL Server 2012. Actually this is a T-SQL enhancement and it is called Query Paging.Often in our applications we need to page/select a number of rows (or a range of rows if you prefer) by specifying a row count offset in the results. <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=2155&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>In this post I will be looking into a new feature in SQL Server 2012. Actually this is a T-SQL enhancement and it is called <strong>Query Paging</strong>.Often in our applications we need to page/select a number of rows (or a range of rows if you prefer) by specifying a row count offset in the results. I will be introducing <strong>OFFSET</strong> which specifies the number of rows to skip  and <strong>FETCH</strong> which specifies the number of rows after the <strong>OFFSET</strong> to return.We will use <strong>OFFSET/FETCH NEXT</strong> in combination with the <strong>ORDER BY</strong> clause.</p>
<p>I am using the <strong>AdventureWorks2012</strong> database.Make sure you visit the <strong>CodePlex</strong> website and download it.Then attach it (restore it) to your local SQL Server instance.</p>
<p>I will be using SQL Server 2012 Developer Edition but the Express edition will suffice .<br />
I connect to the SQL Server instance.</p>
<p>Type (copy and paste but make sure you review it) the code below.Execute the code below</p>
<pre class="brush: sql; title: ; notranslate">

USE [AdventureWorks2012]

GO

SELECT  [Title] ,
[FirstName] ,
[MiddleName] ,
[LastName] ,
[Suffix]
FROM    [AdventureWorks2012].[Person].[Person]
ORDER BY LastName ,
FirstName
OFFSET 0 ROWS
FETCH NEXT 10 ROWS ONLY;

</pre>
<p>In this first example I simply instruct SQL Server to skip <strong>0</strong> rows and then fetch me the next <strong>10</strong> rows.</p>
<p>In the same (or in a new query window ) type (copy and paste but make sure you review it) the code below.Execute the code below</p>
<pre class="brush: sql; title: ; notranslate">

SELECT  [Title] ,
[FirstName] ,
[MiddleName] ,
[LastName] ,
[Suffix]
FROM    [AdventureWorks2012].[Person].[Person]
ORDER BY LastName ,
FirstName
OFFSET 500 ROWS
FETCH NEXT 100 ROWS ONLY;

</pre>
<p>In this second example I simply instruct SQL Server to skip <strong>500</strong> rows and then fetch me the next <strong>100</strong> rows.</p>
<p>I will rewrite the code below to incorporate <strong>variables</strong>. In this case I simply instruct SQL Server to skip <strong>1000</strong> rows and then fetch me the next <strong>1000</strong> rows.</p>
<p>In the same query window type (copy-paste) but do not execute the code below.</p>
<pre class="brush: sql; title: ; notranslate">

DECLARE @offset INT = 1000;
DECLARE @rows_tofetch INT = 1000;

SELECT  [Title] ,
[FirstName] ,
[MiddleName] ,
[LastName] ,
[Suffix]
FROM    [AdventureWorks2012].[Person].[Person]
ORDER BY LastName ,
FirstName
OFFSET @offset ROWS
FETCH NEXT
@rows_tofetch ROWS ONLY;

</pre>
<p>In order to get the next page (skip <strong>1000</strong> rows and return <strong>1000</strong> rows after that) we need to rewrite the code above.</p>
<p>Review the code below and then copy and paste it in a new query window.</p>
<pre class="brush: sql; title: ; notranslate">

DECLARE @offset INT = 1000;
DECLARE @rows_tofetch INT = 1000;

SET @offset = 2 * @offset + @rows_tofetch

SELECT  [Title] ,
[FirstName] ,
[MiddleName] ,
[LastName] ,
[Suffix]
FROM    [AdventureWorks2012].[Person].[Person]
ORDER BY LastName ,
FirstName
OFFSET @offset ROWS
FETCH NEXT
@rows_tofetch ROWS ONLY;
</pre>
<p>Execute the code above.</p>
<p>Hope it helps!!!</p>
<br />Filed under: <a href='http://dotnetstories.wordpress.com/category/sql-server/'>Sql Server</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2012/'>SQL Server 2012</a> Tagged: <a href='http://dotnetstories.wordpress.com/tag/fetch/'>FETCH</a>, <a href='http://dotnetstories.wordpress.com/tag/offset/'>OFFSET</a>, <a href='http://dotnetstories.wordpress.com/tag/query-paging/'>Query Paging</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dotnetstories.wordpress.com/2155/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dotnetstories.wordpress.com/2155/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=2155&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dotnetstories.wordpress.com/2012/08/07/using-query-paging-in-sql-server-2012/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/ecbfd3584f3b2c0c3f528bacdfc15e0a?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96" medium="image">
			<media:title type="html">fofo</media:title>
		</media:content>
	</item>
		<item>
		<title>Looking into Stored Procedure recompilation in SQL Server</title>
		<link>http://dotnetstories.wordpress.com/2012/08/04/looking-into-stored-procedure-recompilation-in-sql-server/</link>
		<comments>http://dotnetstories.wordpress.com/2012/08/04/looking-into-stored-procedure-recompilation-in-sql-server/#comments</comments>
		<pubDate>Fri, 03 Aug 2012 21:19:46 +0000</pubDate>
		<dc:creator>fofo</dc:creator>
				<category><![CDATA[Sql Server]]></category>
		<category><![CDATA[Sql Server 2005]]></category>
		<category><![CDATA[SQL Server 2008]]></category>
		<category><![CDATA[SQL Server 2012]]></category>
		<category><![CDATA[recompilation]]></category>

		<guid isPermaLink="false">http://dotnetstories.wordpress.com/?p=2141</guid>
		<description><![CDATA[In this post I will be looking into recompilation in SQL Server. More specifically I will show you why it happens,when it happens and the tools to identify recompilation.

I will be using SQL Server 2012 Developer edition in this post but feel free to use SQL Server 2005,2008 versions.I will also use SQL Server Profiler.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=2141&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>In this post I will be looking into recompilation and stored procedures in SQL Server. More specifically I will show you why it happens,when it happens and the tools to identify recompilation of stored procedures.There are many reasons why a stored procedure will recompile. Some are more obvious than others.</p>
<p>If you use the <strong>sp_recompile </strong>system stored procedure before the actual stored procedure the <strong>SQL Server Optimiser</strong> will drop the existing plan from the procedure cache forcing a new plan to be created the next time that the procedure will run.You can also add the <strong>WITH RECOMPILE</strong> directly to the stored procedure declaration right before the <strong>AS</strong> keyword. This tells SQL Server to throw out the query plan on every execution of the stored procedure.</p>
<p>In this post I will be looking into how schema changes in objects that are referenced by the stored procedure, can cause recompilation.</p>
<p>I will be using <strong>SQL Server 2012 Developer</strong> edition in this post but feel free to use SQL Server 2005,2008 versions.I will also use SQL Server Profiler.</p>
<p>We all know that SQL Server optimises the execution of a sql query using execution plans.The execution plan is what is compiled and executed by the relational engine and then placed for reuse in the plan cache.T-SQL is declarative by nature.That means that it does not provide any details on how the results will be returned.On the other hand SQL Server execution plans provide the actual (imperative-detailed) way for the relational engine to fetch the results.One way of using the plan cache in an efficient way is exactly writing you T-SQL commands in the context of a stored procedure.</p>
<p>Let me demonstrate recompilation by creating a small example. I am using the <strong>AdventureWorks2012</strong> database.</p>
<p>I connect to the SQL Server instance.</p>
<p>Type (copy and paste but make sure you review it) the code below.Make sure you do not execute anything yet.</p>
<pre class="brush: sql; title: ; notranslate">

USE [AdventureWorks2012]

GO

IF ( SELECT OBJECT_ID('dbo.GetProductInfoByListPrice')
) IS NOT NULL

DROP PROCEDURE dbo.GetProductInfoByListPrice

GO

CREATE PROCEDURE GetProductInfoByListPrice
@LowerVal MONEY,
@UpperVal MONEY
AS
BEGIN

SELECT  Name,
ProductNumber,
Color,Size,StandardCost
FROM    Production.Product
WHERE   [ListPrice] &gt; @LowerVal
AND [ListPrice] &lt; @UpperVal

END

GO

DBCC FREEPROCCACHE

SELECT cpl.objtype AS PlanType,
OBJECT_NAME(stl.objectid,stl.dbid) AS ObjectName,
cpl.refcounts AS ReferenceCounts,
cpl.usecounts AS UseCounts,
stl.text AS SQLBatch,
qpl.query_plan AS QueryPlan
FROM sys.dm_exec_cached_plans AS cpl
CROSS APPLY sys.dm_exec_query_plan(cpl.plan_handle) AS qpl
CROSS APPLY sys.dm_exec_sql_text(cpl.plan_handle) AS stl;
GO

</pre>
<p>Execute lines <strong>1-3</strong> to bring <strong>AdventureWorks2012 </strong>to scope. Then in lines <strong>5-10</strong> I check to see if the stored procedure already exists. Then I create a stored procedure (lines <strong>12-23</strong>) that returns a few columns from the <strong>Production.Product</strong> table based on two input parameters based on the <strong>ListPrice </strong>column values.In line <strong>29</strong> I use the DBCC command to clear the plan cache.Please do not execute this command in production systems.</p>
<p>In lines <strong>31-40</strong> I use 1 DMV and 2 DMFs to get information from the procedure cache.I get information such as the Plan Type,Use counts(how many times the plan was used),the actual query and its query plan.When you execute the code you will see that there are no plans in the plan cache.</p>
<p>Now we need to launch <strong>SQL Server Profiler</strong>. Go to the menu <strong>Tools</strong> -&gt; <strong>SQL Server Profiler</strong>.The SQL Server Profiler will launch and I will connect using <strong>Windows Authentication</strong>.I will create a new trace based on the <strong>Blank template</strong>.In the events tab I will use the <strong>SP:Recompile</strong> and <strong>SQL:StmtRecompile</strong>.The <strong>SQL:StmtRecompile</strong> event fires every time a statement recompilation occurs during execution.The <strong>SP:Recompile</strong> event exists but only for backwards compatibility in <strong>SQL Server 2005</strong>.Make sure you hit the <strong>Run</strong> button.</p>
<p>Have a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/08/sql-profiler.jpg"><img class="aligncenter size-large wp-image-2144" title="sql-profiler" src="http://dotnetstories.files.wordpress.com/2012/08/sql-profiler.jpg?w=1024&#038;h=571" alt="" width="1024" height="571" /></a></p>
<p>In the same query window type (copy-paste) but do not execute the code below.</p>
<pre class="brush: sql; title: ; notranslate">

EXEC GetProductInfoByListPrice 7,155

EXEC GetProductInfoByListPrice 24,3456

SELECT cpl.objtype AS PlanType,
OBJECT_NAME(stl.objectid,stl.dbid) AS ObjectName,
cpl.refcounts AS ReferenceCounts,
cpl.usecounts AS UseCounts,
stl.text AS SQLBatch,
qpl.query_plan AS QueryPlan
FROM sys.dm_exec_cached_plans AS cpl
CROSS APPLY sys.dm_exec_query_plan(cpl.plan_handle) AS qpl
CROSS APPLY sys.dm_exec_sql_text(cpl.plan_handle) AS stl;
GO

</pre>
<p>Execute lines <strong>1-3</strong>.We execute the stored procedure with two different values. Then have a look again in the <strong>procedure cache </strong>(lines <strong>5-14</strong>)<strong>.</strong></p>
<p>Ηave a look at the picture below to see the results I have got in my case.The execution plan was created, placed in the cache and reused.There is no activity in the <strong>SQL Server Profiler.</strong></p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/08/result1.jpg"><img class="aligncenter size-large wp-image-2147" title="result1" src="http://dotnetstories.files.wordpress.com/2012/08/result1.jpg?w=1024&#038;h=550" alt="" width="1024" height="550" /></a></p>
<p>In the same query window type the following</p>
<pre class="brush: sql; title: ; notranslate">

CREATE INDEX IX_Test ON  Production.Product(ListPrice)

EXEC GetProductInfoByListPrice 55,5555

</pre>
<p>I am creating a new index on the <strong>Production.Product</strong> table and then I execute the stored procedure again. <strong>SQL Server Optimiser</strong> detects the presence of the new index and does not use the current execution plan in the procedure cache.It does recompile the existing plan because it is more beneficial to spend more CPU cycles to create a better execution plan.</p>
<p>Have a look in the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/08/sql-profiler-1.jpg"><img class="aligncenter size-large wp-image-2149" title="sql-profiler-1" src="http://dotnetstories.files.wordpress.com/2012/08/sql-profiler-1.jpg?w=1024&#038;h=571" alt="" width="1024" height="571" /></a></p>
<p>To recap we saw that the <strong>SQL Server Optimiser</strong> will drop a plan from the procedure cache when it decides that are changes in the underlying objects that are referenced from the stored procedure that result in a more optimal plan.</p>
<p>Bear in mind that if you have a high recompilation ratio this is one factor that will slow your whole application.You must identify the reasons behind that and eliminate them.It costs both in terms of time and resources for SQL Server to create a new plan.</p>
<p>Hope it helps!!!</p>
<br />Filed under: <a href='http://dotnetstories.wordpress.com/category/sql-server/'>Sql Server</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2005/'>Sql Server 2005</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2008/'>SQL Server 2008</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2012/'>SQL Server 2012</a> Tagged: <a href='http://dotnetstories.wordpress.com/tag/recompilation/'>recompilation</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dotnetstories.wordpress.com/2141/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dotnetstories.wordpress.com/2141/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=2141&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dotnetstories.wordpress.com/2012/08/04/looking-into-stored-procedure-recompilation-in-sql-server/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/ecbfd3584f3b2c0c3f528bacdfc15e0a?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96" medium="image">
			<media:title type="html">fofo</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/08/sql-profiler.jpg?w=1024" medium="image">
			<media:title type="html">sql-profiler</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/08/result1.jpg?w=1024" medium="image">
			<media:title type="html">result1</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/08/sql-profiler-1.jpg?w=1024" medium="image">
			<media:title type="html">sql-profiler-1</media:title>
		</media:content>
	</item>
		<item>
		<title>Using THROW statement in SQL Server 2012</title>
		<link>http://dotnetstories.wordpress.com/2012/07/20/using-throw-statement-in-sql-server-2012/</link>
		<comments>http://dotnetstories.wordpress.com/2012/07/20/using-throw-statement-in-sql-server-2012/#comments</comments>
		<pubDate>Fri, 20 Jul 2012 19:38:07 +0000</pubDate>
		<dc:creator>fofo</dc:creator>
				<category><![CDATA[general .net]]></category>
		<category><![CDATA[Sql Server]]></category>
		<category><![CDATA[SQL Server 2012]]></category>

		<guid isPermaLink="false">http://dotnetstories.wordpress.com/?p=2133</guid>
		<description><![CDATA[In this post I would like to demonstrate with a hands-on example how to use the new THROW statement available to us for error handling in SQL Server 2012.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=2133&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>In this post I would like to demonstrate with a hands-on example how to use the new <strong>THROW</strong> statement available to us for error handling in SQL Server 2012.</p>
<p>Well, most of you know that we have <strong>TRY-CATCH</strong> statements for exception handling since SQL Server 2005.I have blogged about error handling and exceptions in <a href="http://dotnetstories.wordpress.com/2011/11/12/looking-into-exceptions-in-sql-server/" target="_blank">this</a> post.</p>
<p>I am sure that most of you are familiar with <strong>TRY-CATCH</strong> statement but before I go on with the actual example I will explain it. Inside the <strong>TRY</strong> block all the instructions that might cause an exception are included. When an exception occurs from one of the statements that are inside in the <strong>TRY</strong> block, the control is handed to the <strong>CATCH</strong> block, where the exception can be handled (by printing a message to the user, log the error e.t.c)</p>
<p>In SQL Server 2012 we have a new statement, <strong>THROW</strong> which can be used as an alternative of <strong>RAISERROR</strong> but most importantly we can re-throw the original error that occurred back to the client, so it can be handled at the application level.</p>
<p>You can use any edition of SQL Server of 2012 you want for this example. SQL Server Express edition will do just fine.Download the Express edition, <a href="http://www.microsoft.com/en-us/download/details.aspx?id=29062" target="_blank">here</a>.</p>
<p>Connect to the instance of the SQL Server.</p>
<p>Before I move on to demonstrate the use of <strong>THROW </strong>statement, there are some things we must note</p>
<ul>
<li>The line (statement) before the THROW statement must end with semicolon <strong>(;)</strong></li>
<li>All exceptions raised by THROW statement will have a severity of <strong>16</strong></li>
<li>THROW statement doesn&#8217;t require an error number but if you choose to use one it should be greater than <strong>50.000</strong></li>
</ul>
<p>The syntax is pretty basic:</p>
<p><strong>THROW (error_number,message,state)</strong></p>
<p>You must define these parameters only if you use <strong>THROW</strong> outside the <strong>CATCH</strong> block.</p>
<p>1) <strong>THROW outside a catch block</strong></p>
<p>In a new query window type and execute the code below</p>
<pre class="brush: sql; title: ; notranslate">

THROW 12767, 'I am a new statement in SQL Server 2012', 1;
THROW 52767, 'I am a new statement in SQL Server 2012', 1;

</pre>
<p>The first statement will fail since the error number is less than 50.000. The second statement will succeed.</p>
<p>2) <strong>THROW inside a catch block</strong></p>
<p>In a new query window type  the code below</p>
<pre class="brush: sql; title: ; notranslate">
USE tempdb;
go

CREATE TABLE Persons
(
P_Id int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
PRIMARY KEY (P_Id)
)

INSERT INTO persons VALUES (1,'Michael','Owen','122 liverpool street','liverpool')
INSERT INTO persons VALUES (2,'Steven','Gerrard','123 liverpool street','liverpool')

SELECT * FROM persons

CREATE TABLE Orders
(
O_Id int NOT NULL PRIMARY KEY,
OrderNo int NOT NULL,
P_Id int FOREIGN KEY REFERENCES Persons(P_Id)
)

BEGIN TRY
BEGIN TRANSACTION
INSERT INTO orders VALUES (2,12345,2)
INSERT INTO orders VALUES (4,12345,3)
COMMIT TRANSACTION
END TRY
BEGIN CATCH
RAISERROR ('we have an error',17,21);
IF @@TRANCOUNT &gt; 0
ROLLBACK TRANSACTION
END CATCH

</pre>
<p>I create a table(Persons) in <strong>tempdb </strong>and then I insert some values in it.Then I create a second table that is related to the first one with a foreign key constraint.</p>
<p>Then I use an insert statement for the second table (Orders -That I know will violate the foreign key constraint) inside a <strong>TRY-CATCH</strong> block and use <strong>RAISERROR </strong>function to raise a custom error and then rollback the transaction.</p>
<p>Execute the code and note the results and messages.</p>
<p>Now let&#8217;s re-write the INSERT statement above using the <strong>THROW</strong> statement.</p>
<p>In a new query window type and execute the code below</p>
<pre class="brush: sql; title: ; notranslate">
USE tempdb;
go

BEGIN TRY
BEGIN TRANSACTION
INSERT INTO orders VALUES (2,12345,2)
INSERT INTO orders VALUES (4,12345,3)
COMMIT TRANSACTION
END TRY
BEGIN CATCH
PRINT 'There was an error when inserting the records';
THROW;
END CATCH

</pre>
<p>In my case the results are</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/07/error.jpg"><img class="aligncenter size-large wp-image-2136" title="error" src="http://dotnetstories.files.wordpress.com/2012/07/error.jpg?w=1024&#038;h=550" alt="" width="1024" height="550" /></a></p>
<p>The actual error that caused the exception in the first place, appears (is re-thrown) again.</p>
<p>Please note that there is not (yet) <strong>FINALLY</strong> statement.</p>
<p>Hope it helps!!!</p>
<br />Filed under: <a href='http://dotnetstories.wordpress.com/category/general-net/'>general .net</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server/'>Sql Server</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2012/'>SQL Server 2012</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dotnetstories.wordpress.com/2133/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dotnetstories.wordpress.com/2133/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=2133&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dotnetstories.wordpress.com/2012/07/20/using-throw-statement-in-sql-server-2012/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/ecbfd3584f3b2c0c3f528bacdfc15e0a?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96" medium="image">
			<media:title type="html">fofo</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/07/error.jpg?w=1024" medium="image">
			<media:title type="html">error</media:title>
		</media:content>
	</item>
		<item>
		<title>Looking into new and enhanced DMVs and DMFs in SQL Server 2012</title>
		<link>http://dotnetstories.wordpress.com/2012/07/19/looking-into-new-and-enhanced-dmvs-and-dmfs/</link>
		<comments>http://dotnetstories.wordpress.com/2012/07/19/looking-into-new-and-enhanced-dmvs-and-dmfs/#comments</comments>
		<pubDate>Thu, 19 Jul 2012 18:46:07 +0000</pubDate>
		<dc:creator>fofo</dc:creator>
				<category><![CDATA[Sql Server]]></category>
		<category><![CDATA[SQL Server 2012]]></category>
		<category><![CDATA[DMF]]></category>
		<category><![CDATA[DMO]]></category>
		<category><![CDATA[DMV]]></category>

		<guid isPermaLink="false">http://dotnetstories.wordpress.com/?p=2098</guid>
		<description><![CDATA[In this post I would like to demonstrate with a few examples the new DMOs (Dynamic Management Objects - DMVs &#38; DMFs) that are available to us with the RTM version of SQL Server 2012.Some of them were present in SQL Server 2008 R2 SP1  but I do not know how many people have noticed their existence.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=2098&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>In this post I would like to demonstrate with a few examples how to use the new <strong>DMOs</strong> (Dynamic Management Objects &#8211; <strong>DMVs</strong> &amp; <strong>DMFs</strong>) that are available to us with the RTM version of SQL Server 2012.Some of them were present in <strong>SQL Server 2008 R2 SP1 </strong> but I do not know how many people have noticed their existence.</p>
<p>DMO&#8217;s were first introduced in <strong>SQL Server 2005</strong> and were immediately very popular with developers and administrators.They give us a great insight on what is going on inside  SQL Server.It would be very difficult otherwise to get  all this information so easily. They have great information on sessions,plan cache,memory,threads,database connections,waits,latches e.t.c.We can get all this information without placing a heavy burden on the server.</p>
<p>All of them start with <strong>dm_ </strong> in the name.They are all stored in the <strong>sys</strong> schema.</p>
<p>You can use any edition of SQL Server of 2012 you want for this example. SQL Server Express edition will do just fine.Download the Express edition, <a href="http://www.microsoft.com/en-us/download/details.aspx?id=29062" target="_blank">here</a>.</p>
<p>Connect to the instance of the SQL Server.</p>
<p>The first DMF I will be looking into is <strong>sys.dm_os_volume_stats</strong>. This function returns several important information about the physical disk drive the specified database and its respective files are stored in SQL Server 2012.It returns information such as: <strong>Available free space</strong> on the volume,<strong>mount point</strong> at which the volume is rooted,whether the volume supports <strong>sparse files</strong> or not,whether the volume is currently marked as <strong>read only</strong> or not,whether the volume is currently <strong>compressed</strong> or not, e.t.c</p>
<p>As input parameters we need to provide the <strong>Database Id</strong> and the <strong>File Id values.<br />
</strong></p>
<p>In an new query window type and execute the following code.Note the results.</p>
<pre class="brush: sql; title: ; notranslate">

USE AdventureWorks2012;
GO

SELECT name,total_bytes,available_bytes,volume_mount_point,file_system_type,is_compressed
FROM sys.database_files df
CROSS APPLY sys.dm_os_volume_stats(db_id(),df.file_id)

</pre>
<p>The next DMV is <strong>sys.dm_os_windows_info.</strong>It information about <strong>Windows Operating System</strong>.Type and execute the code below. Note the results.</p>
<pre class="brush: sql; title: ; notranslate">

USE MASTER;
GO
SELECT * FROM sys.dm_os_windows_info;

</pre>
<p>The next one I want to talk about is (<strong>sys.dm_server_services</strong>) and returns the list of SQL Server&#8217;s windows services.Type and execute the code below.Note the results.</p>
<pre class="brush: sql; title: ; notranslate">

USE MASTER;
GO
SELECT * FROM sys.dm_server_services

</pre>
<p>The next one I want to talk about is (<strong>sys.dm_server_memory_dumps</strong>) and returns  information about each memory dump file generated by SQL Server.</p>
<p>Type and execute the code below.Note the results.</p>
<pre class="brush: sql; title: ; notranslate">

USE MASTER;
GO
SELECT * FROM sys.dm_server_memory_dumps;

</pre>
<p>The next one I want to talk about (<strong>sys.dm_server_registry</strong>) and returns configuration and installation information stored in the registry for the current SQL Server instance.</p>
<p>Type and execute the code below.Note the results.</p>
<pre class="brush: sql; title: ; notranslate">

USE MASTER;
GO
SELECT * FROM sys.dm_server_registry;

</pre>
<p>The next one I want to talk about  is  (<strong>sys.dm_exec_query_stats</strong>). This is not a new one but it has changed in this version of SQL Server. 4 new columns have been added.</p>
<ul>
<li>    <strong>total_rows</strong> –  This column returns the total number of rows returned by the query</li>
</ul>
<ul>
<li>    <strong>last_rows</strong> –    This column returns the total number of rows returned by last execution of the query</li>
</ul>
<ul>
<li>    <strong>min_rows</strong> –   This column returns the minimum number of rows query returned over the number of times that the plan has executed since it was last compiled</li>
</ul>
<ul>
<li>    <strong>max_rows</strong> –  This column returns the maximum number of rows query returned over the number of times that the plan has executed since it was last compiled</li>
</ul>
<p>I will be using AdventureWorks2012 for this example. I run the first query below. I note that it returns 655 rows.</p>
<p>Then in the second query, I am returning old and new columns from the s<strong>ys.dm_exec_query_stats</strong> for the query where the total number of rows is <strong>655</strong>.</p>
<p>Type and execute the code below.</p>
<pre class="brush: sql; title: ; notranslate">

USE AdventureWorks2012;
GO

SELECT        Sales.SalesOrderHeader.OrderDate, Sales.SalesOrderHeader.DueDate, Sales.SalesOrderHeader.ShipDate, Sales.SalesOrderHeader.SubTotal,
Sales.SalesOrderDetail.UnitPrice, Sales.SalesOrderDetail.OrderQty
FROM          Sales.SalesOrderHeader INNER JOIN
Sales.SalesOrderDetail ON Sales.SalesOrderHeader.SalesOrderID = Sales.SalesOrderDetail.SalesOrderID
WHERE Sales.SalesOrderHeader.ShipDate &gt; '2008-07-29'
ORDER by UnitPrice

SELECT execution_count,total_logical_reads,total_logical_writes,total_physical_reads,total_worker_time,total_rows,last_rows,min_rows,max_rows
FROM sys.dm_exec_query_stats
WHERE total_rows = 655

</pre>
<p>There are many great posts on the web about <strong>DMVs</strong> but one post I really like is Glen Berry&#8217;s , <a href="http://sqlserverperformance.wordpress.com/2010/05/02/recap-of-april-2010-dmv-a-day-series/" target="_blank">A DMV per day series</a>.</p>
<p>Hope it helps!!!!</p>
<br />Filed under: <a href='http://dotnetstories.wordpress.com/category/sql-server/'>Sql Server</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2012/'>SQL Server 2012</a> Tagged: <a href='http://dotnetstories.wordpress.com/tag/dmf/'>DMF</a>, <a href='http://dotnetstories.wordpress.com/tag/dmo/'>DMO</a>, <a href='http://dotnetstories.wordpress.com/tag/dmv/'>DMV</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dotnetstories.wordpress.com/2098/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dotnetstories.wordpress.com/2098/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=2098&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dotnetstories.wordpress.com/2012/07/19/looking-into-new-and-enhanced-dmvs-and-dmfs/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/ecbfd3584f3b2c0c3f528bacdfc15e0a?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96" medium="image">
			<media:title type="html">fofo</media:title>
		</media:content>
	</item>
		<item>
		<title>Using Page Restore in SQL Server 2012 using SSMS</title>
		<link>http://dotnetstories.wordpress.com/2012/07/19/using-page-restore-in-sql-server-2012-using-ssms/</link>
		<comments>http://dotnetstories.wordpress.com/2012/07/19/using-page-restore-in-sql-server-2012-using-ssms/#comments</comments>
		<pubDate>Wed, 18 Jul 2012 22:30:06 +0000</pubDate>
		<dc:creator>fofo</dc:creator>
				<category><![CDATA[Sql Server]]></category>
		<category><![CDATA[SQL Server 2012]]></category>
		<category><![CDATA[Page restore]]></category>

		<guid isPermaLink="false">http://dotnetstories.wordpress.com/?p=2101</guid>
		<description><![CDATA[In this post I would like to provide you with a hands-on example on how to perform a Page Restore using the SSMS in SQL Server 2012.We could perform page restores in SQL Server 2005/2008 (with T-SQL ) but in SQL Server 2012 we have SSMS UI support for the Page Restore operation.The Page Restore operation is intended for repairing isolated damaged pages.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=2101&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>In this post I would like to provide you with a hands-on example on how to perform a <strong>Page Restore</strong> using the SSMS in SQL Server 2012.We could perform page restores in SQL Server 2005/2008 (with T-SQL ) but in SQL Server 2012 we have <strong>SSMS UI</strong> support for the <strong>Page Restore</strong> operation.The <strong>Page Restore</strong> operation is intended for repairing isolated damaged pages.</p>
<p>In this post I will also show you how to corrupt a database by interfering (corrupting) with some database pages.</p>
<p>The<strong> Restore Pages feature</strong> let us restore damaged pages without restoring the entire database.These pages are marked as suspect.</p>
<p>Restore pages is only possible in the full and bulk-logged recovery model.We can only restore <span style="text-decoration:underline;">database pages</span>.<strong>Page Restore </strong>must start with a full, file, or filegroup backup and there should be no break in the chain of log backups.</p>
<p>You can use any edition of SQL Server of 2012 you want for this example. SQL Server Express edition will do just fine.Download the Express edition, <a href="http://www.microsoft.com/en-us/download/details.aspx?id=29062" target="_blank">here</a> .</p>
<p>In my case I will use Developer edition of 2012 for these example.</p>
<p>I connect to the SQL Server instance.</p>
<p>Type (copy &#8211; paste) the code below in a new query window</p>
<pre class="brush: sql; title: ; notranslate">

USE MASTER;
GO

CREATE DATABASE FOOTBALL;

USE FOOTBALL;
GO

CREATE TABLE Footballer
(
FootballerID int identity(1,1),
Footballer_No varchar(20),
Footballer_lastname nvarchar(50),
Footballer_firstname nvarchar(50),
Footballer_middlename nvarchar(50)
)

CREATE UNIQUE CLUSTERED INDEX cx_Foo_IX ON Footballer (FootballerID)

declare @CTR INT
declare @ctrstr varchar(7)
select @ctr=0
WHILE @CTR&lt;60000
BEGIN
select @ctr=@ctr+1
select @ctrstr=Str(@ctr)
insert footballer(Footballer_No ,Footballer_lastname,Footballer_firstname, Footballer_middlename) values('F_No'+@ctrstr,'Footlname'+@ctrstr,'Footfname'+@ctrstr,'Footmname'+@ctrstr)
END

SELECT * FROM footballer

BACKUP DATABASE [FOOTBALL] TO  DISK = N'C:\Program Files\Microsoft SQL Server\MSSQL11.SQL2012\MSSQL\Backup\FOOTBALL.bak'
WITH NOFORMAT, NOINIT,  NAME = N'FOOTBALL-Full Database Backup', SKIP, NOREWIND, NOUNLOAD,  STATS = 10
GO

</pre>
<p>I am creating a database that I will corrupt in the process.Then I create a table and clustered index on that table.Then I populate the table with 60.000 records.</p>
<p>Then I take full database backup of the database.This backup contains un-damaged pages.</p>
<p>I would like to corrupt a page that is part of the clustered index.I will use the <strong>DBCC IND</strong> command.<strong>DBCC IND</strong> command provides the list of pages used by the table or index.</p>
<pre class="brush: sql; title: ; notranslate">

DBCC IND ('FOOTBALL', 'footballer',1)

</pre>
<p>Have a look at the picture below to see the results and the page I selected (1267).</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/07/pageid.jpg"><img class="aligncenter size-large wp-image-2103" title="pageID" src="http://dotnetstories.files.wordpress.com/2012/07/pageid.jpg?w=1024&#038;h=550" alt="" width="1024" height="550" /></a></p>
<p>Before I move on with the corruption of that particular page I must take the database offline.</p>
<p>Type and execute the code below</p>
<pre class="brush: sql; title: ; notranslate">

USE master;
ALTER DATABASE FOOTBALL SET OFFLINE;

</pre>
<p>Now I need to calculate the starting offset of the page I am about to corrupt.</p>
<pre class="brush: sql; title: ; notranslate">

SELECT 1267*8192 AS [StartingOffset]

</pre>
<p>In my case is <strong>10379264</strong></p>
<p>Now we need to open the .mdf file in a hexadecimal editor. You can download this free Hex editor <a href="http://mh-nexus.de/en/hxd/" target="_blank">here </a></p>
<p>It is very easy to install and run the editor. Make sure you launch this application with elevated privileges.Then navigate to the location of the .mdf file and open it.</p>
<p>Inside this file we need to locate our page. I press  <strong>GoTo</strong> (or Ctrl + G), then paste in the offset value (10379264) by selecting <strong>decimal</strong>.Then I press <strong>OK </strong> and I am at the beginning of this page.</p>
<p>Have a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/07/football.jpg"><img class="aligncenter size-large wp-image-2104" title="football" src="http://dotnetstories.files.wordpress.com/2012/07/football.jpg?w=1024&#038;h=550" alt="" width="1024" height="550" /></a></p>
<p>Now I am about to corrupt by simply changing the values and corrupting the page.</p>
<p>Have a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/07/corrupted-page.jpg"><img class="aligncenter size-large wp-image-2105" title="corrupted-page" src="http://dotnetstories.files.wordpress.com/2012/07/corrupted-page.jpg?w=1024&#038;h=550" alt="" width="1024" height="550" /></a></p>
<p>Now we need to bring the database online.</p>
<p>Type and execute the code below</p>
<pre class="brush: sql; title: ; notranslate">

USE master;
ALTER DATABASE FOOTBALL SET ONLINE;

</pre>
<p>Then we can run a simple query</p>
<p>Type and execute the code below</p>
<pre class="brush: sql; title: ; notranslate">

USE FOOTBALL;
SELECT FootballerID FROM Footballer

</pre>
<p>We will get an error. Have a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/07/errorio.jpg"><img class="aligncenter size-large wp-image-2106" title="errorIO" src="http://dotnetstories.files.wordpress.com/2012/07/errorio.jpg?w=1024&#038;h=550" alt="" width="1024" height="550" /></a></p>
<p>Now let&#8217;s run the DBCC CHECKDB command.</p>
<pre class="brush: sql; title: ; notranslate">

DBCC CHECKDB ('FOOTBALL')

</pre>
<p>Have a look at the picture below to see the results I have got.</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/07/errorio-2.jpg"><img class="aligncenter size-large wp-image-2107" title="errorIO-2" src="http://dotnetstories.files.wordpress.com/2012/07/errorio-2.jpg?w=1024&#038;h=550" alt="" width="1024" height="550" /></a></p>
<p>You can query the <strong>suspect_pages</strong> table of  the <strong>msdb</strong> database.</p>
<pre class="brush: sql; title: ; notranslate">

SELECT * FROM msdb..suspect_pages

</pre>
<p>In my case this is the output I get when I run the statement above.</p>
<p>database_id    file_id     page_id              event_type       error_count            last_update_date<br />
&#8212;&#8212;&#8212;&#8211; &#8212;&#8212;&#8212;&#8211; &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211; &#8212;&#8212;&#8212;&#8211; &#8212;&#8212;&#8212;&#8211; &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
6                            1           1267                            1                             3                  2012-07-18 03:17:57.333</p>
<p>You can also have a look at the SQL Server Logs.You will identify the corruption immediately.</p>
<p>Have a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/07/sqlserverlogerror1.jpg"><img class="aligncenter size-large wp-image-2112" title="SQLSERVERLOGERROR" src="http://dotnetstories.files.wordpress.com/2012/07/sqlserverlogerror1.jpg?w=1024&#038;h=574" alt="" width="1024" height="574" /></a></p>
<p>In order to restore the page, I select the database, I right-click on it (from the <strong>Object Explorer</strong> in the SSMS) then select <strong>Tasks-&gt;Restore-&gt;Page .</strong></p>
<p>Have a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/07/restore-pages-ssms.jpg"><img class="aligncenter size-large wp-image-2110" title="restore-pages-SSMS" src="http://dotnetstories.files.wordpress.com/2012/07/restore-pages-ssms.jpg?w=1024&#038;h=583" alt="" width="1024" height="583" /></a><br />
Then the <strong>Restore Page</strong> screen appears.You can see that the database is selected, the FileID and PageID of the damaged page.You can run DBCC CHECKDB command.You can also see the<strong> Tail-Log</strong> backup and all the other backups.<br />
Have a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/07/restore-corrupted-page.jpg"><img class="aligncenter size-full wp-image-2114" title="restore-corrupted-page" src="http://dotnetstories.files.wordpress.com/2012/07/restore-corrupted-page.jpg?w=460&#038;h=438" alt="" width="460" height="438" /></a></p>
<p>Then I press <strong>OK</strong>.Then I execute the statements below and there are no errors.</p>
<pre class="brush: sql; title: ; notranslate">

USE FOOTBALL;
SELECT FootballerID FROM Footballer
DBCC CHECKDB ('FOOTBALL');

</pre>
<p>To recap, we showed (do not do this in a production database) how to corrupt a page in a database and also how to perform <strong>Page Restore</strong> using SSMS UI in SQL Server 2012.</p>
<p>Hope it helps!!!</p>
<br />Filed under: <a href='http://dotnetstories.wordpress.com/category/sql-server/'>Sql Server</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2012/'>SQL Server 2012</a> Tagged: <a href='http://dotnetstories.wordpress.com/tag/page-restore/'>Page restore</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dotnetstories.wordpress.com/2101/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dotnetstories.wordpress.com/2101/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=2101&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dotnetstories.wordpress.com/2012/07/19/using-page-restore-in-sql-server-2012-using-ssms/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/ecbfd3584f3b2c0c3f528bacdfc15e0a?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96" medium="image">
			<media:title type="html">fofo</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/07/pageid.jpg?w=1024" medium="image">
			<media:title type="html">pageID</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/07/football.jpg?w=1024" medium="image">
			<media:title type="html">football</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/07/corrupted-page.jpg?w=1024" medium="image">
			<media:title type="html">corrupted-page</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/07/errorio.jpg?w=1024" medium="image">
			<media:title type="html">errorIO</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/07/errorio-2.jpg?w=1024" medium="image">
			<media:title type="html">errorIO-2</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/07/sqlserverlogerror1.jpg?w=1024" medium="image">
			<media:title type="html">SQLSERVERLOGERROR</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/07/restore-pages-ssms.jpg?w=1024" medium="image">
			<media:title type="html">restore-pages-SSMS</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/07/restore-corrupted-page.jpg" medium="image">
			<media:title type="html">restore-corrupted-page</media:title>
		</media:content>
	</item>
		<item>
		<title>Stored procedure compilation and caching</title>
		<link>http://dotnetstories.wordpress.com/2012/07/17/stored-procedure-compilation-and-caching/</link>
		<comments>http://dotnetstories.wordpress.com/2012/07/17/stored-procedure-compilation-and-caching/#comments</comments>
		<pubDate>Tue, 17 Jul 2012 02:10:11 +0000</pubDate>
		<dc:creator>fofo</dc:creator>
				<category><![CDATA[general .net]]></category>
		<category><![CDATA[Sql Server]]></category>
		<category><![CDATA[Sql Server 2005]]></category>
		<category><![CDATA[SQL Server 2008]]></category>
		<category><![CDATA[SQL Server 2012]]></category>
		<category><![CDATA[execution plan]]></category>
		<category><![CDATA[procedure cache]]></category>
		<category><![CDATA[stored procedure]]></category>

		<guid isPermaLink="false">http://dotnetstories.wordpress.com/?p=2084</guid>
		<description><![CDATA[In this post I will explain when the actual caching (the stored procedure is placed in the procedure cache ).

Many people think that the execution plan of a stored procedure is cached in the procedure cache when the stored procedure is created. That is a mistake.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=2084&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>In this post I will explain when the actual caching (the stored procedure is placed in the procedure cache ) takes place.</p>
<p>Many people think that the execution plan of a stored procedure is cached in the procedure cache when the stored procedure is created. That is a mistake.</p>
<p>You can use any edition of SQL Server of 2012/2008/2005 you want for this example. SQL Server Express edition will do just fine.Download the Express edition, <a href="http://www.microsoft.com/en-us/download/details.aspx?id=29062" target="_blank">here</a> .</p>
<p>In my case I will use Developer edition of 2012 for these example.</p>
<p>I connect to the SQL Server instance.</p>
<p>Type (copy and paste but make sure you review it) and execute the code below</p>
<pre class="brush: sql; title: ; notranslate">

USE AdventureWorks2012
GO

DBCC FREEPROCCACHE
GO

CREATE PROCEDURE PeopleinSeattle
AS
SELECT [AddressID]
,[AddressLine1]
,[AddressLine2]
,[City]
,[StateProvinceID]
,[PostalCode]
FROM [AdventureWorks2012].[Person].[Address]
WHERE [City] = 'Seattle'

SELECT cpl.objtype AS PlanType,
OBJECT_NAME(stl.objectid,stl.dbid) AS ObjectName,
cpl.refcounts AS ReferenceCounts,
cpl.usecounts AS UseCounts,
stl.text AS SQLBatch,
qpl.query_plan AS QueryPlan
FROM sys.dm_exec_cached_plans AS cpl
CROSS APPLY sys.dm_exec_query_plan(cpl.plan_handle) AS qpl
CROSS APPLY sys.dm_exec_sql_text(cpl.plan_handle) AS stl;
GO

</pre>
<p>Let me explain what I do with the code above. I clear the procedure cache in line <strong>4</strong>.Do not do that in a production environment.</p>
<p>Then in lines <strong>8-17</strong> I create a stored procedure.In lines <strong>19-28</strong> I use 1 DMV and 2 DMFs to get information from the procedure cache.</p>
<p>I get information such as the Plan Type,Use counts(how many times the plan was used),the actual query and its query plan.</p>
<p>When I execute lines <strong>8-17</strong> I get the result you see in the picture below.</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/07/plan-cache.jpg"><img class="aligncenter size-large wp-image-2090" title="plan-cache" src="http://dotnetstories.files.wordpress.com/2012/07/plan-cache.jpg?w=1024&#038;h=550" alt="" width="1024" height="550" /></a></p>
<p>It is clear that there is no stored procedure in the cache.Let&#8217;s execute the stored procedure and run the code with the DMO (DMV+DMFs) again</p>
<pre class="brush: sql; title: ; notranslate">

exec PeopleinSeattle

SELECT cpl.objtype AS PlanType,
OBJECT_NAME(stl.objectid,stl.dbid) AS ObjectName,
cpl.refcounts AS ReferenceCounts,
cpl.usecounts AS UseCounts,
stl.text AS SQLBatch,
qpl.query_plan AS QueryPlan
FROM sys.dm_exec_cached_plans AS cpl
CROSS APPLY sys.dm_exec_query_plan(cpl.plan_handle) AS qpl
CROSS APPLY sys.dm_exec_sql_text(cpl.plan_handle) AS stl;
GO
</pre>
<p>The result follows in the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/07/plan-cach1.jpg"><img class="aligncenter size-large wp-image-2091" title="plan-cach1" src="http://dotnetstories.files.wordpress.com/2012/07/plan-cach1.jpg?w=1024&#038;h=550" alt="" width="1024" height="550" /></a></p>
<p>Only when the stored procedure is executed the Query optimiser generates a plan, compiles it and places it in the procedure cache.No plan is generated when the stored procedure is created.</p>
<p>Execute the code below</p>
<pre class="brush: sql; title: ; notranslate">

exec PeopleinSeattle
GO 40

SELECT cpl.objtype AS PlanType,
OBJECT_NAME(stl.objectid,stl.dbid) AS ObjectName,
cpl.refcounts AS ReferenceCounts,
cpl.usecounts AS UseCounts,
stl.text AS SQLBatch,
qpl.query_plan AS QueryPlan
FROM sys.dm_exec_cached_plans AS cpl
CROSS APPLY sys.dm_exec_query_plan(cpl.plan_handle) AS qpl
CROSS APPLY sys.dm_exec_sql_text(cpl.plan_handle) AS stl;
GO

</pre>
<p>We execute the stored procedure 40 times and we check again to see the contents of the procedure cache.</p>
<p>Have a look at the picture below to see the results</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/07/plan-cache2.jpg"><img class="aligncenter size-large wp-image-2092" title="plan-cache2" src="http://dotnetstories.files.wordpress.com/2012/07/plan-cache2.jpg?w=1024&#038;h=550" alt="" width="1024" height="550" /></a></p>
<p>Now we can see that the compiled plan that was compiled and placed in the procedure cache for this particular stored procedure will be used every time this procedure is executed.That is great performance boost.</p>
<p>That is one of the main advantages of stored procedures.</p>
<p>Hope it helps!!!</p>
<br />Filed under: <a href='http://dotnetstories.wordpress.com/category/general-net/'>general .net</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server/'>Sql Server</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2005/'>Sql Server 2005</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2008/'>SQL Server 2008</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2012/'>SQL Server 2012</a> Tagged: <a href='http://dotnetstories.wordpress.com/tag/execution-plan/'>execution plan</a>, <a href='http://dotnetstories.wordpress.com/tag/procedure-cache/'>procedure cache</a>, <a href='http://dotnetstories.wordpress.com/tag/stored-procedure/'>stored procedure</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dotnetstories.wordpress.com/2084/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dotnetstories.wordpress.com/2084/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=2084&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dotnetstories.wordpress.com/2012/07/17/stored-procedure-compilation-and-caching/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/ecbfd3584f3b2c0c3f528bacdfc15e0a?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96" medium="image">
			<media:title type="html">fofo</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/07/plan-cache.jpg?w=1024" medium="image">
			<media:title type="html">plan-cache</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/07/plan-cach1.jpg?w=1024" medium="image">
			<media:title type="html">plan-cach1</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/07/plan-cache2.jpg?w=1024" medium="image">
			<media:title type="html">plan-cache2</media:title>
		</media:content>
	</item>
		<item>
		<title>Using T-SQL Functions in the WHERE clause of query</title>
		<link>http://dotnetstories.wordpress.com/2012/07/16/using-t-sql-functions-in-the-where-clause-of-query/</link>
		<comments>http://dotnetstories.wordpress.com/2012/07/16/using-t-sql-functions-in-the-where-clause-of-query/#comments</comments>
		<pubDate>Mon, 16 Jul 2012 18:45:27 +0000</pubDate>
		<dc:creator>fofo</dc:creator>
				<category><![CDATA[Sql Server]]></category>
		<category><![CDATA[Sql Server 2005]]></category>
		<category><![CDATA[SQL Server 2008]]></category>
		<category><![CDATA[SQL Server 2012]]></category>

		<guid isPermaLink="false">http://dotnetstories.wordpress.com/?p=2076</guid>
		<description><![CDATA[In this post Ι would like to demonstrate with a hands-on example the negative performance implications when using built-in or user defined functions in a WHERE statement of a SQL query.

Obviously we can use the functions in the SELECT part of the query. In that case the performance impact is not affected as much.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=2076&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>In this post Ι would like to demonstrate with a hands-on example the negative performance implications when using built-in or user defined functions in a <strong>WHERE</strong> statement of a SQL query.</p>
<p>Obviously we can use the functions in the SELECT part of the query. In that case the performance impact is not that great.</p>
<p>You can use any edition of SQL Server of 2012 you want for this example. SQL Server Express edition will do just fine.Download the Express edition, <a href="http://www.microsoft.com/en-us/download/details.aspx?id=29062" target="_blank">here </a>.</p>
<p>In my case I will use Developer edition of 2012 for these examples.</p>
<p>I connect to the SQL Server instance.</p>
<p>I need to create a sample database (<strong>SAMPLEDEMO</strong>) and a table (<strong>SALES</strong>) in it.I create a non-clustered index on the <strong>OrderDate</strong> column.</p>
<p>Type (copy and paste but make sure you review it) and execute the code below</p>
<pre class="brush: sql; title: ; notranslate">

USE [master]
GO

CREATE DATABASE [SAMPLEDEMO]

GO

USE [SAMPLEDEMO]
GO

CREATE TABLE [dbo].[SaleS](
[SalesOrderID] [int] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
[OrderDate] [datetime] NOT NULL,
[DueDate] [datetime] NOT NULL,
[ShipDate] [datetime] NULL,
[Status] [tinyint] NOT NULL,
[SubTotal] [money] NOT NULL,
[TaxAmt] [money] NOT NULL,
[Freight] [money] NOT NULL,
[Comment] [nvarchar](128) NULL,
[rowguid] [uniqueidentifier] ROWGUIDCOL  NOT NULL,
[ModifiedDate] [datetime] NOT NULL,
CONSTRAINT [PK_SalesOrderHeader_SalesOrderID] PRIMARY KEY CLUSTERED
(
[SalesOrderID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

&amp;nbsp;

CREATE NONCLUSTERED INDEX [IX_Sales_OrderDate] ON [dbo].[SaleS]
(
[OrderDate] ASC
)

GO

</pre>
<p>Then I will populate the table with 10.000 rows of data using a third party tool (Data Generator from RedGate).</p>
<p>Now we will write 2 queries (we were asked  to produce a report from our e.g sales manager) that will bring back only the <strong>SalesOrderID</strong> and <strong>OrderDate</strong> values  for the orders that were placed on <strong>July 2008</strong>.</p>
<p>Type and execute the code below.Make sure that you have clicked on the &#8220;<strong>Include Actual Execution Plan</strong>&#8221; button.We need the metrics from the execution plan.</p>
<pre class="brush: sql; title: ; notranslate">

USE [SAMPLEDEMO]
GO

SELECT [SalesOrderID],[OrderDate]
FROM [SAMPLEDEMO].[dbo].[SaleS]
WHERE year(orderdate)= '2008' and MONTH(OrderDate) = '7'

SELECT [SalesOrderID],[OrderDate]
FROM [SAMPLEDEMO].[dbo].[SaleS]
WHERE orderdate between '2008/07/01' and  '2008/08/01'

</pre>
<p>Have a look at the picture below to see my findings</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/07/execplan.jpg"><img class="aligncenter size-large wp-image-2079" title="execplan" src="http://dotnetstories.files.wordpress.com/2012/07/execplan.jpg?w=1024&#038;h=550" alt="" width="1024" height="550" /></a></p>
<p>In both cases I get back <strong>102</strong> rows of data. In the first sql query using the built-in functions the cost is 91% relative to the batch and I see that I have an <strong>Index scan</strong>. By rewriting the query (omitting the built-in functions) I see that the cost is 9% relative to the batch and the optimiser uses an <strong>Index Seek</strong> which is a far better option.The non-clustered index we created is used in the second case because the optimiser can see now that the values in the <strong>OrderDate</strong> column are organised according to this index.This is a &#8220;range&#8221; query and because there is an index that is available on this column it is used efficiently to retrieve the requested data.</p>
<p>Hope it helps!!!!</p>
<br />Filed under: <a href='http://dotnetstories.wordpress.com/category/sql-server/'>Sql Server</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2005/'>Sql Server 2005</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2008/'>SQL Server 2008</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2012/'>SQL Server 2012</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dotnetstories.wordpress.com/2076/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dotnetstories.wordpress.com/2076/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=2076&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dotnetstories.wordpress.com/2012/07/16/using-t-sql-functions-in-the-where-clause-of-query/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/ecbfd3584f3b2c0c3f528bacdfc15e0a?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96" medium="image">
			<media:title type="html">fofo</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/07/execplan.jpg?w=1024" medium="image">
			<media:title type="html">execplan</media:title>
		</media:content>
	</item>
		<item>
		<title>Using SQL Server 2012 Performance Dashboard Reports</title>
		<link>http://dotnetstories.wordpress.com/2012/07/16/using-sql-server-2012-performance-dashboard-reports/</link>
		<comments>http://dotnetstories.wordpress.com/2012/07/16/using-sql-server-2012-performance-dashboard-reports/#comments</comments>
		<pubDate>Mon, 16 Jul 2012 16:04:27 +0000</pubDate>
		<dc:creator>fofo</dc:creator>
				<category><![CDATA[Sql Server]]></category>
		<category><![CDATA[SQL Server 2012]]></category>
		<category><![CDATA[SQL Server 2012 Performance Dashboard Reports]]></category>

		<guid isPermaLink="false">http://dotnetstories.wordpress.com/?p=2062</guid>
		<description><![CDATA[In this post I would like to talk about various reports ( SQL Server 2012 Performance Dashboard Reports ) that are available to us (through Microsoft Download) and their purpose is to give us an overview of the overall performance of an SQL Server instance.Basically they can help us identify where bottlenecks (CPU,IO) can be found<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=2062&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>In this post I would like to talk about various reports (<strong> SQL Server 2012 Performance Dashboard Reports </strong>) that are available to us (through Microsoft Download) and their purpose is to give us an overview of the overall performance of a SQL Server instance.Basically they can help us identify where bottlenecks (CPU,IO) can be found.These reports identify where blocking occurs (waits-latches).You do not have to install <strong>SQL Server Reporting Services</strong> in order to use these reports.They retrieve all the data they present us through DMVs.</p>
<p>You can download these reports <a href="http://www.microsoft.com/en-us/download/details.aspx?id=29063" target="_blank">here</a>. The installation process is straight forward and you just need to follow the steps of the installer.</p>
<p>Once these reports (these are simply Reporting Services report files) have been installed we can use them through SSMS (Custom Reports).</p>
<p>There are <strong>Standard Server</strong> reports available to us.Let&#8217;s have a look at what standard reports are available to us at the server level.We have reports on queries performance (filtered by IO,CPU), transactions that cause blocking,locking,memory consumption on the server e.t.c.</p>
<p>Please have a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/07/server-dashboard1.jpg"><img class="aligncenter size-large wp-image-2064" title="server dashboard" src="http://dotnetstories.files.wordpress.com/2012/07/server-dashboard1.jpg?w=1024&#038;h=577" alt="" width="1024" height="577" /></a></p>
<p>I have chosen the first report (Server Dashboard) &#8211; This report provides overview data about the SQL Server instance and its configurations.</p>
<p>Have a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/07/server-dashboard-1.jpg"><img class="aligncenter size-large wp-image-2065" title="server dashboard-1" src="http://dotnetstories.files.wordpress.com/2012/07/server-dashboard-1.jpg?w=1024&#038;h=577" alt="" width="1024" height="577" /></a></p>
<p>There are Standard reports at the database level as well.We can have <strong>Disk Usage</strong> reports,<strong>backup and restore</strong> <strong>events</strong> reports and much more.</p>
<p>Have a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/07/database-standard-server-reports.jpg"><img class="aligncenter size-large wp-image-2068" title="database standard server reports" src="http://dotnetstories.files.wordpress.com/2012/07/database-standard-server-reports.jpg?w=1024&#038;h=579" alt="" width="1024" height="579" /></a></p>
<p>For this example I have chosen the <strong>Index Usage Statistics</strong> report.</p>
<p>Have a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/07/index-usage.jpg"><img class="aligncenter size-large wp-image-2067" title="index-usage" src="http://dotnetstories.files.wordpress.com/2012/07/index-usage.jpg?w=1024&#038;h=550" alt="" width="1024" height="550" /></a></p>
<p>Let me show you how to use <strong>SQL Server 2012 Performance Dashboard Reports</strong> after you install them. You just right- click on the server in the Object Explorer, then choose <strong>Reports</strong> from the context menu and then <strong>Custom Reports</strong>.</p>
<p>Have a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/07/report-perf-1.jpg"><img class="aligncenter size-large wp-image-2069" title="report-perf-1" src="http://dotnetstories.files.wordpress.com/2012/07/report-perf-1.jpg?w=1024&#038;h=575" alt="" width="1024" height="575" /></a></p>
<p>Then in the <strong>Open File Dialog</strong> box choose <strong>performance_dashboard_main</strong> report file (you must know the folder where all these reports are kept- e.g &#8220;C:\Program Files (x86)\Microsoft SQL Server\110\Tools\Performance Dashboard&#8221; folder)  and click <strong>Open</strong> to display the report.</p>
<p>Please have a look at the picture below that shows the output of this particular report.</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/07/report-perf-2.jpg"><img class="aligncenter size-large wp-image-2070" title="report-perf-2" src="http://dotnetstories.files.wordpress.com/2012/07/report-perf-2.jpg?w=1024&#038;h=550" alt="" width="1024" height="550" /></a></p>
<p>Have a look at the rest of the <strong>Custom Reports</strong> (Waits,IO Statistics) by selecting the links from the main report.</p>
<p>Hope it helps!!!</p>
<br />Filed under: <a href='http://dotnetstories.wordpress.com/category/sql-server/'>Sql Server</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2012/'>SQL Server 2012</a> Tagged: <a href='http://dotnetstories.wordpress.com/tag/sql-server-2012-performance-dashboard-reports/'>SQL Server 2012 Performance Dashboard Reports</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dotnetstories.wordpress.com/2062/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dotnetstories.wordpress.com/2062/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=2062&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dotnetstories.wordpress.com/2012/07/16/using-sql-server-2012-performance-dashboard-reports/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/ecbfd3584f3b2c0c3f528bacdfc15e0a?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96" medium="image">
			<media:title type="html">fofo</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/07/server-dashboard1.jpg?w=1024" medium="image">
			<media:title type="html">server dashboard</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/07/server-dashboard-1.jpg?w=1024" medium="image">
			<media:title type="html">server dashboard-1</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/07/database-standard-server-reports.jpg?w=1024" medium="image">
			<media:title type="html">database standard server reports</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/07/index-usage.jpg?w=1024" medium="image">
			<media:title type="html">index-usage</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/07/report-perf-1.jpg?w=1024" medium="image">
			<media:title type="html">report-perf-1</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/07/report-perf-2.jpg?w=1024" medium="image">
			<media:title type="html">report-perf-2</media:title>
		</media:content>
	</item>
		<item>
		<title>Using SQL Space Map</title>
		<link>http://dotnetstories.wordpress.com/2012/07/16/using-sql-space-map/</link>
		<comments>http://dotnetstories.wordpress.com/2012/07/16/using-sql-space-map/#comments</comments>
		<pubDate>Mon, 16 Jul 2012 14:19:19 +0000</pubDate>
		<dc:creator>fofo</dc:creator>
				<category><![CDATA[Sql Server]]></category>
		<category><![CDATA[SQL Server 2012]]></category>
		<category><![CDATA[SQL Space Map]]></category>

		<guid isPermaLink="false">http://dotnetstories.wordpress.com/?p=2056</guid>
		<description><![CDATA[In this rather short post , I would like to talk about a very handy and easy to use tool that makes it very easy to identify space utilisation for a particular database.

This is a codeplex project and you can download it here . This tool helps to identify large objects,objects with a high index/data ration and tables which may be growing more quickly than expected.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=2056&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>In this rather short post , I would like to talk about a very handy and easy to use tool (SQL Space Map) that makes it very easy to identify space utilisation for a particular database in our SQL Server instance.</p>
<p>This is a codeplex project and you can download it <a href="http://sqlspacemap.codeplex.com/" target="_blank">here</a> . This tool helps to identify large objects,objects with a high index/data ratio and tables which may be growing more quickly than expected.</p>
<p>Installation of this tool is really straight forward. When you run it for the first time, you need to specify the server instance you are connecting to, the database and whether you are connecting using SQL Server or Windows authentication.</p>
<p>I launched the <strong>SQL Space Map</strong> application , I have connected to my local instance of<strong> SQL Server 2012 Developer</strong> edition through Windows authentication to the <strong>AdventureWorks2012</strong> database. When I do that I can see a visual representation of the database objects in my database and their respective size. Make sure you hover over the various tables and indexes.</p>
<p>Please have a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/07/sql-space-map.jpg"><img class="aligncenter size-large wp-image-2058" title="sql-space-map" src="http://dotnetstories.files.wordpress.com/2012/07/sql-space-map.jpg?w=1024&#038;h=581" alt="" width="1024" height="581" /></a></p>
<p>We have also the<strong> Standard Reports</strong> that are available out of the box for SQL Server 2012 and provide similar information.But I prefer the visual representation that <strong>SQL Space Map</strong> tool provides.</p>
<p>Have a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/07/sql-server-reports.jpg"><img class="aligncenter size-large wp-image-2057" title="sql-server-reports" src="http://dotnetstories.files.wordpress.com/2012/07/sql-server-reports.jpg?w=1024&#038;h=574" alt="" width="1024" height="574" /></a></p>
<p>Hope it helps!!!</p>
<br />Filed under: <a href='http://dotnetstories.wordpress.com/category/sql-server/'>Sql Server</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2012/'>SQL Server 2012</a> Tagged: <a href='http://dotnetstories.wordpress.com/tag/sql-space-map/'>SQL Space Map</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dotnetstories.wordpress.com/2056/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dotnetstories.wordpress.com/2056/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=2056&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dotnetstories.wordpress.com/2012/07/16/using-sql-space-map/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/ecbfd3584f3b2c0c3f528bacdfc15e0a?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96" medium="image">
			<media:title type="html">fofo</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/07/sql-space-map.jpg?w=1024" medium="image">
			<media:title type="html">sql-space-map</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/07/sql-server-reports.jpg?w=1024" medium="image">
			<media:title type="html">sql-server-reports</media:title>
		</media:content>
	</item>
		<item>
		<title>Looking into Concat string function in SQL Server 2012</title>
		<link>http://dotnetstories.wordpress.com/2012/07/16/looking-into-concat-string-function-in-sql-server-2012/</link>
		<comments>http://dotnetstories.wordpress.com/2012/07/16/looking-into-concat-string-function-in-sql-server-2012/#comments</comments>
		<pubDate>Sun, 15 Jul 2012 23:59:44 +0000</pubDate>
		<dc:creator>fofo</dc:creator>
				<category><![CDATA[Sql Server]]></category>
		<category><![CDATA[SQL Server 2012]]></category>
		<category><![CDATA[Concat function]]></category>

		<guid isPermaLink="false">http://dotnetstories.wordpress.com/?p=2054</guid>
		<description><![CDATA[I will be writing a new series of posts about the new T-SQL functions that are available to us in SQL Server 2012.

In this rather sort post I will demonstrate with  hands-on examples how to use the new string function,CONCAT().<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=2054&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>I will be writing a new series of posts about the new T-SQL functions that are available to us in SQL Server 2012.</p>
<p>In this rather sort post I will demonstrate with  hands-on examples how to use the new string function,<strong>CONCAT()</strong>.</p>
<p>You can have a look in another blog <a href="http://dotnetstories.wordpress.com/2011/09/30/using-built-in-functions-in-sql-server/" target="_blank">post</a> of mine related to built-in SQL functions.You can have a look into the new logical functions introduced in SQL Server 2012, <a href="http://dotnetstories.wordpress.com/2012/07/15/lookin-into-iif-and-choose-sql-server-2012-logical-functions/" target="_blank">here</a> .</p>
<p>You can have a look in another blog <a href="http://dotnetstories.wordpress.com/2012/07/15/looking-into-the-new-conversion-functions-in-sql-server-2012/">post</a> of mine explaining the new conversion functions in SQL Server 2012.</p>
<p>You can use any edition of SQL Server of 2012 you want. SQL Server Express edition will do just fine.Download the Express edition, <a href="http://www.microsoft.com/en-us/download/details.aspx?id=29062" target="_blank">here</a> .</p>
<p>In my case I will use Developer edition of 2012 for these examples.</p>
<p>This function (<strong>CONCAT()</strong>) allows multiple string values to be joined (concatenated) together.This function implicitly converts the data values to a string and NULL values are converted to an empty string.</p>
<p>Type and execute the code below</p>
<pre class="brush: sql; title: ; notranslate">

SELECT CONCAT ('Liverpool', ' ' , 'is', ' ', 'the', ' ', 'best', ' football', ' ', 'team', ' ' ,'in', ' ' ,'the', ' ' ,'world')

SELECT Concat('Nikos',Null,' Kantzelis', ' Mr.')

SELECT CONCAT ('Usain', ' ' , 'Bolt', ' ' , 'came', ' ', 1, 'st', ' ' ,'in', ' ' , 'the', ' ' , 'Olympics')

</pre>
<p>All these strings in the first statement are now concatenated into one.</p>
<p>The same happens for the second statement but please note that the <strong>NULL</strong> value is converted to an empty string.</p>
<p>In the third statement the strings are concatenated.Please note  that <strong>CONCAT()</strong> function in this case will convert the value of <strong>1</strong> to its string equivalent.</p>
<p>Let&#8217;s try this new function in a table</p>
<p>Type and execute the code below</p>
<pre class="brush: sql; title: ; notranslate">

USE tempdb;
GO

CREATE TABLE [dbo].[Employee](
[ID] [int] IDENTITY(1,1) NOT NULL,
[FirstName] [nvarchar](30) NOT NULL,
[LastName] [nvarchar](30) NOT NULL,
[Country] [nvarchar] (30) NOT NULL,
[City] [nvarchar] (30) NOT NULL,
[PostalCode] [nvarchar] (30) NOT NULL,
[HireDate] [date] NOT NULL,
CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED ([ID] ASC)
) ON [PRIMARY]

INSERT INTO Employee (FirstName, LastName,Country,City,PostalCode,HireDate)
VALUES
('Robbie', 'Fowler','USA', 'New York', '434343','1/10/2004'),
('Steven', 'Gerrard','Ireland','Dublin','75676','10/3/2000'),
('Michael', 'Owen', 'Britain','London','87677','2/4/2002')

SELECT CONCAT(FirstName, ' ' ,LastName) AS FULLNAME FROM Employee

</pre>
<p>I create a table and I insert some values in it. Then in the last statement I concatenate the first and the last name for all the rows in the table.</p>
<p>Hope it helps!!!</p>
<br />Filed under: <a href='http://dotnetstories.wordpress.com/category/sql-server/'>Sql Server</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2012/'>SQL Server 2012</a> Tagged: <a href='http://dotnetstories.wordpress.com/tag/concat-function/'>Concat function</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dotnetstories.wordpress.com/2054/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dotnetstories.wordpress.com/2054/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=2054&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dotnetstories.wordpress.com/2012/07/16/looking-into-concat-string-function-in-sql-server-2012/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/ecbfd3584f3b2c0c3f528bacdfc15e0a?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96" medium="image">
			<media:title type="html">fofo</media:title>
		</media:content>
	</item>
		<item>
		<title>Looking into the new conversion functions in SQL Server 2012</title>
		<link>http://dotnetstories.wordpress.com/2012/07/15/looking-into-the-new-conversion-functions-in-sql-server-2012/</link>
		<comments>http://dotnetstories.wordpress.com/2012/07/15/looking-into-the-new-conversion-functions-in-sql-server-2012/#comments</comments>
		<pubDate>Sun, 15 Jul 2012 00:56:22 +0000</pubDate>
		<dc:creator>fofo</dc:creator>
				<category><![CDATA[Sql Server]]></category>
		<category><![CDATA[SQL Server 2012]]></category>
		<category><![CDATA[PARSE]]></category>
		<category><![CDATA[TRY_CONVERT]]></category>
		<category><![CDATA[TRY_PARSE]]></category>

		<guid isPermaLink="false">http://dotnetstories.wordpress.com/?p=2045</guid>
		<description><![CDATA[I will be writing a new series of posts about the new T-SQL functions that are available to us in SQL Server 2012.

In this rather sort post I will demonstrate with a hands-on examples how to use 3 new conversion functions in SQL Server 2012, PARSE and TRY_CONVERT,TRY_PARSE.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=2045&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>I will be writing a new series of posts about the new T-SQL functions that are available to us in SQL Server 2012.</p>
<p>In this rather sort post I will demonstrate with a hands-on examples how to use 3 new conversion functions in SQL Server 2012, <strong>PARSE</strong>, <strong>TRY_CONVERT </strong>and<strong> TRY_PARSE</strong>.</p>
<p>You can have a look in another blog <a href="http://dotnetstories.wordpress.com/2011/09/30/using-built-in-functions-in-sql-server/" target="_blank">post</a> of mine related to built-in SQL functions.You can have a look into the new logical functions introduced in SQL Server 2012, <a href="http://dotnetstories.wordpress.com/2012/07/15/lookin-into-iif-and-choose-sql-server-2012-logical-functions/" target="_blank">here</a> .</p>
<p>You can use any edition of SQL Server of 2012 you want. SQL Server Express edition will do just fine.Download the Express edition, <a href="http://www.microsoft.com/en-us/download/details.aspx?id=29062" target="_blank">here</a> .</p>
<p>In my case I will use Developer edition of 2012 for these examples.</p>
<p>I will look into the <strong>PARSE()</strong> function first.We can convert a string value to numeric or datetime format with the <strong>PARSE</strong> function.If that is not possible an error will be returned.We must say that this function relies on <strong>CLR</strong> to do the conversions.If there is no <strong>CLR</strong> an error will be returned.</p>
<p>This function takes a string value as the first argument and as a second argument there is the <strong>data type</strong> that we want to convert the value in the first argument.The third argument (optional) is the <strong>culture</strong> in which the string value will be formatted.</p>
<p>Type and execute the code below</p>
<pre class="brush: sql; title: ; notranslate">

SELECT PARSE ('THURSDAY, 30 JANUARY 2014' AS datetime2 USING 'en-us') AS NEWDAY
SELECT PARSE('€345,98' AS money USING 'fr-FR') AS Newmoney
SELECT PARSE('nick' AS datetime2 USING 'en-GB') AS Res

</pre>
<p>The first two statements will succeed but the third one will fail.</p>
<p>Let&#8217;s try this new function in a table</p>
<p>Type and execute the code below</p>
<pre class="brush: sql; title: ; notranslate">

USE tempdb;
GO

CREATE TABLE [dbo].[Employee](
[ID] [int] IDENTITY(1,1) NOT NULL,
[FirstName] [nvarchar](30) NOT NULL,
[LastName] [nvarchar](30) NOT NULL,
[Country] [nvarchar] (30) NOT NULL,
[City] [nvarchar] (30) NOT NULL,
[PostalCode] [nvarchar] (30) NOT NULL,
[HireDate] [date] NOT NULL,
CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED ([ID] ASC)
) ON [PRIMARY]

INSERT INTO Employee (FirstName, LastName,Country,City,PostalCode,HireDate)
VALUES
('Robbie', 'Fowler','USA', 'New York', '434343','1/10/2004'),
('Steven', 'Gerrard','Ireland','Dublin','75676','10/3/2000'),
('Michael', 'Owen', 'Britain','London','87677','2/4/2002')

SELECT FirstName,LastName,PARSE(PostalCode as int) newpostalcode FROM Employee

</pre>
<p>I create a table and I insert some values in it. Then in the last statement I parse the postal code values from <strong>string</strong> to <strong>int</strong>.</p>
<p>The next function I will be looking into is <strong>TRY_PARSE.</strong>It is very similar with the <strong>PARSE</strong> function. We can convert a string value to numeric or datetime format with the <strong>TRY_PARSE</strong> function.If that is not possible an NULL value will be returned.We must say that this function relies on CLR to do the conversions.If there is no CLR an error will be returned.</p>
<p>This function takes a string value as the first argument and as a second argument there is the data type that we want to convert the value in the first argument.The third argument (optional) is the culture in which the string value will be formatted.</p>
<p>Type and execute the code below</p>
<pre class="brush: sql; title: ; notranslate">

SELECT TRY_PARSE ('THURSDAY, 30 JANUARY 2014' AS datetime2 USING 'en-us') AS NEWDAY
SELECT TRY_PARSE('€345,98' AS money USING 'fr-FR') AS Newmoney
SELECT TRY_PARSE('nick' AS datetime2 USING 'en-GB') AS Res

</pre>
<p>The first two statements will succeed but the third one will fail and we will receive a NULL value as a result.</p>
<p>Type and execute the code below</p>
<pre class="brush: sql; title: ; notranslate">

USE tempdb;
GO
SELECT FirstName,LastName,TRY_PARSE(PostalCode as int) newpostalcode FROM Employee
SELECT FirstName,LastName,TRY_PARSE(Country as int) newcountry FROM Employee

</pre>
<p>The first  statement will succeed but the second one will fail and we will receive NULL values as a result.</p>
<p>The final function I will be explaining is the <strong>TRY_CONVERT</strong> function.</p>
<p><strong>TRY_CONVERT</strong> takes a value that is passed to it and tries to convert it to the specified data type.</p>
<p>If the cast succeeds, <strong>TRY_CONVERT</strong> returns the value as the specified data type.On the other hand if an error occurs, <strong>NULL</strong> is returned.if you request a conversion that is explicitly not permitted, then <strong>TRY_CONVERT</strong> will fail with an error.The first argument is the data type into which to cast the expression (value to be cast).</p>
<p>Type and execute the code below</p>
<pre class="brush: sql; title: ; notranslate">

SELECT TRY_CONVERT(datetime2, '12/12/2010') AS Result;
SELECT TRY_CONVERT(datetime2, '32/12/2010') AS Result;
SELECT TRY_CONVERT(int, 'nick' ) AS Res

</pre>
<p>The first statement will succeed but the second and third statement will fail and we will get as a result of that a NULL value.</p>
<p>Type and execute the code below</p>
<pre class="brush: sql; title: ; notranslate">

USE tempdb;
GO
SELECT FirstName,LastName,TRY_CONVERT(int,PostalCode) newpostalcode FROM Employee
SELECT FirstName,LastName,TRY_CONVERT(INT, Country) newpostalcode FROM Employee

</pre>
<p>The first  statement will succeed but the second one will fail and we will receive NULL values as a result.</p>
<p>Type and execute the code below</p>
<pre class="brush: sql; title: ; notranslate">

SELECT TRY_CONVERT(xml, 6677) AS Res;
GO

</pre>
<p>This statement will return an error.To recap we saw 3 new conversion functions introduced in SQL Server 2012.</p>
<p>Hope it helps!!!</p>
<br />Filed under: <a href='http://dotnetstories.wordpress.com/category/sql-server/'>Sql Server</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2012/'>SQL Server 2012</a> Tagged: <a href='http://dotnetstories.wordpress.com/tag/parse/'>PARSE</a>, <a href='http://dotnetstories.wordpress.com/tag/try_convert/'>TRY_CONVERT</a>, <a href='http://dotnetstories.wordpress.com/tag/try_parse/'>TRY_PARSE</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dotnetstories.wordpress.com/2045/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dotnetstories.wordpress.com/2045/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=2045&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dotnetstories.wordpress.com/2012/07/15/looking-into-the-new-conversion-functions-in-sql-server-2012/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/ecbfd3584f3b2c0c3f528bacdfc15e0a?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96" medium="image">
			<media:title type="html">fofo</media:title>
		</media:content>
	</item>
		<item>
		<title>Looking into IIF and CHOOSE SQL Server 2012 logical functions</title>
		<link>http://dotnetstories.wordpress.com/2012/07/15/lookin-into-iif-and-choose-sql-server-2012-logical-functions/</link>
		<comments>http://dotnetstories.wordpress.com/2012/07/15/lookin-into-iif-and-choose-sql-server-2012-logical-functions/#comments</comments>
		<pubDate>Sat, 14 Jul 2012 21:09:33 +0000</pubDate>
		<dc:creator>fofo</dc:creator>
				<category><![CDATA[Sql Server]]></category>
		<category><![CDATA[SQL Server 2012]]></category>
		<category><![CDATA[CHOOSE]]></category>
		<category><![CDATA[IIF]]></category>

		<guid isPermaLink="false">http://dotnetstories.wordpress.com/?p=2034</guid>
		<description><![CDATA[I will be writing a new series of posts about the new T-SQL functions that are available to us in

In this rater sort post I will demonstrate with a hands-on examples how to use 2 new logical functions in SQL Server 2012, CHOOSE and IIF.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=2034&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>I will be writing a new series of posts about the new T-SQL functions that are available to us in SQL Server 2012.</p>
<p>In this rather sort post I will demonstrate with a hands-on examples how to use 2 new logical functions in SQL Server 2012, <strong>CHOOSE</strong> and <strong>IIF</strong>.</p>
<p>You can have a look in another blog <a href="http://dotnetstories.wordpress.com/2011/09/30/using-built-in-functions-in-sql-server/" target="_blank">post</a> of mine related to built-in SQL functions.</p>
<p>You can use any edition of SQL Server of 2012 you want. SQL Server Express edition will do just fine.Download the Express edition, <a href="http://www.microsoft.com/en-us/download/details.aspx?id=29062" target="_blank">here</a></p>
<p>In my case I will use Developer edition of 2012 for these examples.</p>
<p>I will start with the <strong>CHOOSE</strong> function.This function returns an item from a specified set of  items for the specified index.You can think of it as an array kind of thing.</p>
<p>Launch SQL Server 2012 and in a new query window type and execute the code below</p>
<pre class="brush: sql; title: ; notranslate">

SELECT CHOOSE ( 3, 'Chelsea', 'Man Utd', 'Liverpool', 'Arsenal' ) AS 'The Best team';

</pre>
<p>But what happens if the index is not an integer? In that case the value is converted to an integer value.</p>
<p>Execute the code below</p>
<pre class="brush: sql; title: ; notranslate">

SELECT CHOOSE ( 3.3, 'Chelsea', 'Man Utd', 'Liverpool', 'Arsenal' ) AS 'The Best team';

</pre>
<p>You can use a variable to define the index value.</p>
<p>Execute the code below</p>
<pre class="brush: sql; title: ; notranslate">

declare @index as int = 3

SELECT CHOOSE ( @index, 'Chelsea', 'Man Utd', 'Liverpool', 'Arsenal' ) AS 'The Best team';

</pre>
<p>In this last example I will be using a table from the <strong>AdventureWorks2012</strong> OLTP database.</p>
<p>You can download it from <a href="http://msftdbprodsamples.codeplex.com/releases/view/55330" target="_blank">codeplex</a>.</p>
<p>I will be using CHOOSE to concatenate values from the <strong>Person.Person</strong> table.Obviously this is not the best way to do this but I just want to demonstrate the <strong>CHOOSE</strong> function</p>
<p>Type and execute the code below</p>
<pre class="brush: sql; title: ; notranslate">

USE AdventureWorks2012
GO

SELECT CHOOSE(1,Title,FirstName,MiddleName,LastName) + ' ' + CHOOSE(2,Title,FirstName,MiddleName,LastName) + ' ' + CHOOSE(3,Title,FirstName,MiddleName,LastName) + ' .' + CHOOSE(4,Title,FirstName,MiddleName,LastName) FROM Person.Person;

</pre>
<p>Let&#8217;s move on to the <strong>IIF</strong> function.The <strong>IIF</strong> function returns one of two values depending if the first expression evaluates to TRUE or FALSE.If the first expression evaluates to TRUE the first value is displayed,if not the second value is displayed.</p>
<p>Type and execute the code below</p>
<pre class="brush: sql; title: ; notranslate">

SELECT IIF(DATEPART(WEEKDAY,GETDATE())=3 ,'Today is Tuesday','It is not Tuesday') as 'Day of the Week' ;
</pre>
<p>Please note that the start of the week is Sunday with index 1. So today as I am writing this post (Saturday night) the above script will return &#8220;It is not Tuesday&#8221; and that is because the first argument of the expression returned &#8220;FALSE&#8221;.</p>
<p>Ιn this last example I will use the Product table of the AdventureWorks database.In this example we want to produce a small report where every product that starts with &#8216;Touring&#8217; should return back &#8216;<strong>Out Of Stock</strong>&#8216;.</p>
<p>Type and execute the following code</p>
<pre class="brush: sql; title: ; notranslate">

SELECT [ProductID]
,[Name]
,[ProductNumber]
,[MakeFlag]
,[ListPrice]
,[Size]
,IIF((LEFT(Name,7)= 'Touring'), 'Out Of stock','In stock') as StockAvailability
FROM [AdventureWorks2012].[Production].[Product]
</pre>
<p>Please have a look at the results!!!</p>
<p>Hope it helps!!!!</p>
<br />Filed under: <a href='http://dotnetstories.wordpress.com/category/sql-server/'>Sql Server</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2012/'>SQL Server 2012</a> Tagged: <a href='http://dotnetstories.wordpress.com/tag/choose/'>CHOOSE</a>, <a href='http://dotnetstories.wordpress.com/tag/iif/'>IIF</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dotnetstories.wordpress.com/2034/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dotnetstories.wordpress.com/2034/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=2034&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dotnetstories.wordpress.com/2012/07/15/lookin-into-iif-and-choose-sql-server-2012-logical-functions/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/ecbfd3584f3b2c0c3f528bacdfc15e0a?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96" medium="image">
			<media:title type="html">fofo</media:title>
		</media:content>
	</item>
		<item>
		<title>Installing a named instance of SQL Server 2012</title>
		<link>http://dotnetstories.wordpress.com/2012/07/14/%ce%b9nstalling-a-named-instance-of-sql-server-2012/</link>
		<comments>http://dotnetstories.wordpress.com/2012/07/14/%ce%b9nstalling-a-named-instance-of-sql-server-2012/#comments</comments>
		<pubDate>Sat, 14 Jul 2012 13:44:23 +0000</pubDate>
		<dc:creator>fofo</dc:creator>
				<category><![CDATA[Sql Server]]></category>
		<category><![CDATA[SQL Server 2012]]></category>
		<category><![CDATA[sql server 2012 installation]]></category>

		<guid isPermaLink="false">http://dotnetstories.wordpress.com/?p=2010</guid>
		<description><![CDATA[In this post I will demonstrate how to install SQL Server 2012 in your machine.

I had no intention of writing such a post but I installed SQL Server 2012 Developer edition a few weeks back in my Windows 7 Ultimate SP1 laptop and it worked just fine.Then I thought it would be a good idea to write a post like that since a lot of people install the latest version of SQL Server.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=2010&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>In this post I will demonstrate how to install SQL Server 2012 in your machine.</p>
<p>I had no intention of writing such a post but I installed <strong>SQL Server 2012 Developer edition</strong> a few weeks back in my <strong>Windows 7 Ultimate SP1</strong> laptop and it worked just fine.Then I thought it would be a good idea to write a post like that since a lot of people install the latest version of SQL Server.</p>
<p>Developer edition of SQL Server 2012 can be installed in a client operating system.</p>
<p>Enterprise edition can only be installed in server operating systems.You can download the Enterprise edition from this <a href="http://www.microsoft.com/sqlserver/en/us/get-sql-server/try-it.aspx" target="_blank">link</a>.</p>
<p>If you are a MSDN subscriber (or you have purchased a valid license) you can still follow the steps below.</p>
<p>Before you download the Enterprise edition of SQL Server 2012 make sure you have read the <a href="http://msdn.microsoft.com/en-us/library/ms143506.aspx" target="_blank">Hardware and Software requirements</a></p>
<p>Being a MCT  I have a TechNet subscription and I have downloaded the Developer edition from my account.</p>
<p>Before I show you how to install the product I would like to talk a bit about the various editions of SQL Server 2012.</p>
<p>There are 3 main editions of SQL Server 2012</p>
<ul>
<li><a href="http://www.microsoft.com/sqlserver/en/us/editions/2012-editions/enterprise.aspx" target="_blank">Enterprise</a> &#8211; Suitable for mission critical applications and data warehouses.All the features are included in this edition</li>
<li><a href="http://www.microsoft.com/sqlserver/en/us/editions/2012-editions/business-intelligence.aspx" target="_blank">Business Intelligence</a> &#8211; This is a brand new edition.It provides things like Rapid Data Discovery with <strong>Power View</strong> and a new feature called <strong>Data Quality Services</strong>.</li>
<li><a href="http://www.microsoft.com/sqlserver/en/us/editions/2012-editions/standard.aspx" target="_blank">Standard</a> &#8211; This is the basic edition and provides database,reporting and analytics capabilities.</li>
</ul>
<p>If you want to check what features are supported by the various editions of SQL Server 2012 have a look at <a href="http://msdn.microsoft.com/en-us/library/cc645993.aspx" target="_blank">this</a> msdn article.</p>
<p>There are also some special edition</p>
<ul>
<li><a href="http://www.microsoft.com/sqlserver/en/us/editions/2012-editions/web.aspx" target="_blank">Web</a> &#8211; This is basically the Standard edition.It is targeted towards serving data from websites,hosted websites.ISPs should definitely look to purchase licenses for this edition</li>
<li>Developer &#8211; This has all the features of the Enterprise edition but it is only for demo purposes and development environment</li>
<li><a href="http://www.microsoft.com/sqlserver/en/us/editions/2012-editions/express.aspx" target="_blank">Express</a> &#8211; This is a free edition and has only database engine features .There are some limitations of course.Each database has a maximum database size of <strong>10 GB</strong> and the maximum memory utilised is <strong>1 GB</strong>.</li>
</ul>
<p>Obviously at some point the licensing issue will come up.Have a look <a href="http://www.microsoft.com/sqlserver/en/us/get-sql-server/how-to-buy.aspx" target="_blank">here</a> if you are interested.</p>
<p>Another important thing to note is that SQL Server 2012 is available in both <strong>32</strong> and <strong>64</strong> bit versions.</p>
<p>The SQL Server 2012 installation process is pretty similar with the one of SQL Server 2008.One notable difference is that <strong>BUILTIN\administrators</strong> and <strong>Local System (NT AUTHORITY\SYSTEM)</strong> are not automatically provisioned in the <strong>sysadmin</strong> fixed server role.</p>
<p>Let&#8217;s install SQL Server 2012, now.</p>
<p><span style="text-decoration:underline;"><strong>Installation Steps</strong></span></p>
<p>1) I run the SQL Server installer by starting SETUP.EXE</p>
<p>2) The <strong>SQL Server Installation Center</strong> will now display</p>
<p>3) Then I click on <strong>Installation</strong></p>
<p>4) Then I click on <strong>New SQL Server stand-alone installation or add features to an existing installation</strong></p>
<p>Have a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/07/sql-server-installation-1.jpg"><img class="aligncenter size-full wp-image-2011" title="sql server installation-1" src="http://dotnetstories.files.wordpress.com/2012/07/sql-server-installation-1.jpg?w=460&#038;h=439" alt="" width="460" height="439" /></a><br />
5)  The <strong>Setup Support Rules</strong> dialog displays.If there are any failures they must be corrected.In my case there none.Then I click OK</p>
<p>Have a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/07/sql-server-installation-2.jpg"><img class="aligncenter size-full wp-image-2012" title="sql server installation-2" src="http://dotnetstories.files.wordpress.com/2012/07/sql-server-installation-2.jpg?w=460&#038;h=345" alt="" width="460" height="345" /></a></p>
<p>6) The next screen appears, the <strong>Product Updates</strong> screen. Then I click <strong>Next</strong> since there are no updates.</p>
<p>Have a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/07/sql-server-installation-3.jpg"><img class="aligncenter size-full wp-image-2014" title="sql server installation-3" src="http://dotnetstories.files.wordpress.com/2012/07/sql-server-installation-3.jpg?w=460&#038;h=345" alt="" width="460" height="345" /></a></p>
<p>7) On the <strong>Setup Support Rules</strong> screen I have a look in the detailed report and I make a note of the warning.Problems that might occur in this stage must be corrected.</p>
<p>Have a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/07/sql-server-installation-4.jpg"><img class="aligncenter size-full wp-image-2015" title="sql server installation-4" src="http://dotnetstories.files.wordpress.com/2012/07/sql-server-installation-4.jpg?w=460&#038;h=345" alt="" width="460" height="345" /></a><br />
8) Then I click <strong>Next</strong>.On the <strong>Installation Type</strong>, Ι select <strong>Perform a new installation of SQL Server 2012</strong>. Notice that there is already a default instance of SQL Server installed.The default instance is <strong>SQL Server 2008 R2 Developer Edition</strong>. Τhere is also an instance of <strong>SQL Server 2008 R2 Express Edition</strong> and another instance of <strong>SQL Server 2012 Developer</strong> edition.You can have all these instances installed in one machine without any problem.</p>
<p>Have a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/07/sql-server-installation-5.jpg"><img class="aligncenter size-full wp-image-2019" title="sql server installation-5" src="http://dotnetstories.files.wordpress.com/2012/07/sql-server-installation-5.jpg?w=460&#038;h=304" alt="" width="460" height="304" /></a></p>
<p>9) Then I click <strong>Next</strong>.On the <strong>Product Key</strong> screen, in my case there is already the product key.In your case you will have to type it in or select <strong>Specify a Free Edition</strong> (this will install SQL Server 2012 180 day evaluation copy)</p>
<p>Have a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/07/sql-server-installation-6.jpg"><img class="aligncenter size-full wp-image-2020" title="sql server installation-6" src="http://dotnetstories.files.wordpress.com/2012/07/sql-server-installation-6.jpg?w=460&#038;h=304" alt="" width="460" height="304" /></a></p>
<p>10) Then I click <strong>Next</strong>. On the <strong>License Terms Page</strong>, Ι click <strong>I accept the license terms</strong>.</p>
<p>Have a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/07/sql-server-installation-7.jpg"><img class="aligncenter size-full wp-image-2021" title="sql server installation-7" src="http://dotnetstories.files.wordpress.com/2012/07/sql-server-installation-7.jpg?w=460&#038;h=304" alt="" width="460" height="304" /></a><br />
11) Τhen I Click <strong>Next</strong>.  On the <strong>Setup Role</strong> screen, click <strong>SQL Server Feature Installation</strong> and then I click <strong>Next</strong>. Have a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/07/sql-server-installation-8.jpg"><img class="aligncenter size-full wp-image-2023" title="sql server installation-8" src="http://dotnetstories.files.wordpress.com/2012/07/sql-server-installation-8.jpg?w=460&#038;h=304" alt="" width="460" height="304" /></a></p>
<p>12) On the <strong>Feature Selection</strong> screen, I review the features I would like to install. When installing the Enterprise edition of SQL Server you see that there are many components that you could install.If you do not need to install <strong>SQL Server Analysis Services</strong>, <strong>SQL Server Integration Services</strong>,<strong>SQL Reporting Services</strong> then do not.Try (if this is an option for you) not to install <strong>SQL Server Reporting Services</strong> on the same server as the database engine. IIS and web services bring a lot of vulnerabilities to SQL Server.I have selected<br />
· Database Engine Services<br />
· SQL Server Replication<br />
· Data Quality Services</p>
<p>Have a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/07/sql-server-installation-9.jpg"><img class="aligncenter size-full wp-image-2024" title="sql server installation-9" src="http://dotnetstories.files.wordpress.com/2012/07/sql-server-installation-9.jpg?w=460&#038;h=304" alt="" width="460" height="304" /></a></p>
<p>13) Then I click the <strong>Next</strong> button.On the <strong>Installation Rules</strong> screen, click <strong>Next</strong>.</p>
<p>14) In the <strong>Instance Configuration </strong>screen I specify a named instance  &#8220;SQL2012Main&#8221; as the name.</p>
<p>Have a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/07/sql-server-installation-10.jpg"><img class="aligncenter size-full wp-image-2025" title="sql server installation-10" src="http://dotnetstories.files.wordpress.com/2012/07/sql-server-installation-10.jpg?w=460&#038;h=304" alt="" width="460" height="304" /></a></p>
<p>15) Then I click <strong>Next</strong>. On the <strong>Disk Space Requirements</strong> screen, click <strong>Next</strong>.</p>
<p>16) In the <strong>Server Configuration</strong> screen Ι will configure the service accounts for the SQL Server instance. I can choose the security account the service will run and whether the service will start automatically or not.I will leave the defaults but you can choose whatever suits your needs.You can also set the collation for the server instance. In my case is <strong>Greek_CI-AS</strong> . You can change the collation of the server after the installation and you can set the collation at the database level.</p>
<p>Ηave a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/07/sql-server-installation-11.jpg"><img class="aligncenter size-full wp-image-2026" title="sql server installation-11" src="http://dotnetstories.files.wordpress.com/2012/07/sql-server-installation-11.jpg?w=460&#038;h=304" alt="" width="460" height="304" /></a></p>
<p>17) Then I click <strong>Next</strong>. In the <strong>Database Engine Configuration</strong> screen, the authentication mode is set to <strong>Windows Authentication</strong>. Then I need to specify <strong>SQL Server Administrators Accounts</strong> and I click the <strong>Add Current User</strong> button.You can also have a look at the <strong>Data Directories</strong> tab.As we know it is a good practice to separate data files from log files but I will leave the default Data Directories.Have a look at the <strong>FILESTREAM</strong> tab.The Filestream option can be enabled at the point of installation, but can also be configured post installation. FILESTREAM enables SQL Server-based applications to store unstructured data, such as documents and images on the file system.</p>
<p>Have a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/07/sql-server-installation-12.jpg"><img class="aligncenter size-full wp-image-2027" title="sql server installation-12" src="http://dotnetstories.files.wordpress.com/2012/07/sql-server-installation-12.jpg?w=460&#038;h=304" alt="" width="460" height="304" /></a></p>
<p>18) Then I click <strong>Next</strong>.In the <strong>Error Reporting</strong> screen, click <strong>Next</strong>.</p>
<p>19) In the <strong>Installation Configuration Rules</strong> screen, I click <strong>Next</strong>.In the <strong>Ready to Install</strong> screen, I did review the Installation Summary page.</p>
<p>Also I reviewed the configuration file path, which indicates the <strong>configuration.ini</strong> file used.</p>
<p>Have a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/07/sql-server-installation-13.jpg"><img class="aligncenter size-full wp-image-2028" title="sql server installation-13" src="http://dotnetstories.files.wordpress.com/2012/07/sql-server-installation-13.jpg?w=460&#038;h=304" alt="" width="460" height="304" /></a></p>
<p>20) Then I click the <strong>Install</strong> button.Then installer will now install the SQL Server binaries.When the installation completes the <strong>Complete</strong> screen will appear.</p>
<p>Then I need to close the installer window and finally close the <strong>SQL Server Installation Center</strong>.</p>
<p>Installing SQL Server is a very straight-forward, smooth process and you will have no problem completing it.</p>
<p>Hope it helps!!!!</p>
<br />Filed under: <a href='http://dotnetstories.wordpress.com/category/sql-server/'>Sql Server</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2012/'>SQL Server 2012</a> Tagged: <a href='http://dotnetstories.wordpress.com/tag/sql-server-2012-installation/'>sql server 2012 installation</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dotnetstories.wordpress.com/2010/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dotnetstories.wordpress.com/2010/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=2010&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dotnetstories.wordpress.com/2012/07/14/%ce%b9nstalling-a-named-instance-of-sql-server-2012/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/ecbfd3584f3b2c0c3f528bacdfc15e0a?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96" medium="image">
			<media:title type="html">fofo</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/07/sql-server-installation-1.jpg" medium="image">
			<media:title type="html">sql server installation-1</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/07/sql-server-installation-2.jpg" medium="image">
			<media:title type="html">sql server installation-2</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/07/sql-server-installation-3.jpg" medium="image">
			<media:title type="html">sql server installation-3</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/07/sql-server-installation-4.jpg" medium="image">
			<media:title type="html">sql server installation-4</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/07/sql-server-installation-5.jpg" medium="image">
			<media:title type="html">sql server installation-5</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/07/sql-server-installation-6.jpg" medium="image">
			<media:title type="html">sql server installation-6</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/07/sql-server-installation-7.jpg" medium="image">
			<media:title type="html">sql server installation-7</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/07/sql-server-installation-8.jpg" medium="image">
			<media:title type="html">sql server installation-8</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/07/sql-server-installation-9.jpg" medium="image">
			<media:title type="html">sql server installation-9</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/07/sql-server-installation-10.jpg" medium="image">
			<media:title type="html">sql server installation-10</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/07/sql-server-installation-11.jpg" medium="image">
			<media:title type="html">sql server installation-11</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/07/sql-server-installation-12.jpg" medium="image">
			<media:title type="html">sql server installation-12</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/07/sql-server-installation-13.jpg" medium="image">
			<media:title type="html">sql server installation-13</media:title>
		</media:content>
	</item>
		<item>
		<title>User defined server roles in SQL Server 2012</title>
		<link>http://dotnetstories.wordpress.com/2012/07/07/user-defined-server-roles-in-sql-server-2012/</link>
		<comments>http://dotnetstories.wordpress.com/2012/07/07/user-defined-server-roles-in-sql-server-2012/#comments</comments>
		<pubDate>Fri, 06 Jul 2012 23:23:12 +0000</pubDate>
		<dc:creator>fofo</dc:creator>
				<category><![CDATA[Sql Server]]></category>
		<category><![CDATA[SQL Server 2012]]></category>
		<category><![CDATA[user defined server roles]]></category>

		<guid isPermaLink="false">http://dotnetstories.wordpress.com/?p=2002</guid>
		<description><![CDATA[In this post I would like to talk about a new security enhancement that was introduced in SQL Server 2012,user defined server roles.As we all know we had (and still have in SQL Server 2012) user defined database roles for database level permissions.In the latest version of SQL Server we can have custom - user defined server roles for server level permissions<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=2002&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>In this post I would like to talk about a new security enhancement that was introduced in SQL Server 2012,<strong>user defined server roles</strong>.As we all know we had (and still have in SQL Server 2012) user defined database roles for database level permissions.In the latest version of SQL Server we can have custom &#8211; user defined server roles for server level permissions.Basically for a server-level principal we can grant server-level permissions with a mix of GRANT and DENY permissions.</p>
<p>Now we are ready to go on with our hands-on example.I will be using the <a href="http://www.microsoft.com/sqlserver/en/us/editions/2012-editions/express.aspx">SQL Server 2012 Express Edition</a></p>
<p>If you have an instance of SQL Server 2012 running in your machine,simply connect to it.</p>
<p>In a new query window type</p>
<pre class="brush: sql; title: ; notranslate">

use master;
GO

CREATE SERVER ROLE MyCustomServerRole;

GRANT CONTROL SERVER TO MyCustomServerRole;

DENY ALTER ANY LOGIN TO MyCustomServerRole;
DENY ALTER ANY SERVER AUDIT TO MyCustomServerRole;
DENY ALTER ANY SERVER ROLE TO MyCustomServerRole;
DENY CREATE SERVER ROLE TO MyCustomServerRole;
DENY UNSAFE ASSEMBLY TO MyCustomServerRole;

CREATE LOGIN Mary WITH PASSWORD = '!@#$%^&amp;';

EXECUTE AS LOGIN ='Mary';

CREATE LOGIN Jane WITH PASSWORD = '!@#$%^&amp;abc';

SELECT * FROM sys.dm_exec_cached_plans;

CREATE SERVER ROLE JaneRole;

REVERT;

ALTER SERVER ROLE MyCustomServerRole ADD MEMBER Mary;

EXECUTE AS LOGIN ='Mary';

CREATE LOGIN Jane WITH PASSWORD = '!@#$%^&amp;abc';

SELECT * FROM sys.dm_exec_cached_plans;

CREATE SERVER ROLE JaneRole;

</pre>
<p>Let me explain what I am doing with the T-SQL code above.</p>
<ul>
<li>In lines <strong>4-6</strong>, I create a custom server role (<strong>MyCustomServerRole</strong>) and then grant it <strong>sysadmin</strong> permissions.</li>
<li>In lines <strong>8-12,</strong> I take some of these permissions away.</li>
<li>In line <strong>14</strong> I create a SQL Server login,<strong>Mary</strong>.</li>
<li>In line <strong>15</strong> I impersonate the login <strong>Mary</strong> and I execute lines 18-22 as <strong>Mary</strong>.</li>
<li>All the statements in lines <strong>18-22</strong> fail since Mary has no server level permissions. That was expected.</li>
<li>Then in line <strong>24</strong> I revert to being a <strong>sysadmin</strong></li>
<li>In line <strong>26</strong> I add <strong>Mary</strong> to the <strong>MyCustomServerRole</strong> and login again as <strong>Mary</strong></li>
<li>The statement in line <strong>30</strong> will fail (there is no such permission for that login) &#8211; Have a look at the DENY permission statements above</li>
<li>The statement in line <strong>32</strong> will succeed</li>
<li>The statement in line <strong>34</strong> will fail (there is no such permission for that login) &#8211; Have a look at the DENY permission statements above</li>
</ul>
<p>Now le’ts see how to create a windows login. We need to create a windows user first. <strong>Right-click</strong> on Computer icon on the desktop and then select <strong>Manage</strong>, expand <strong>Local Users and Groups</strong> and then create <strong>New User</strong>.I create a windows user named “nikolaosk”. Put any password you want. Have a look at the picture below.</p>
<p><a href="http://dotnetstories.files.wordpress.com/2011/10/windowsuser.jpg"><img class="aligncenter size-full wp-image-1492" title="windowsuser" src="http://dotnetstories.files.wordpress.com/2011/10/windowsuser.jpg?w=460&#038;h=328" alt="" width="460" height="328" /></a><br />
I am creating a new login from that windows user. Then I add the login to the user defined server role.</p>
<pre class="brush: sql; title: ; notranslate">

use master;
GO
CREATE LOGIN [FOFO-PC\nikolaosk] FROM WINDOWS;

ALTER SERVER ROLE MyCustomServerRole ADD MEMBER [FOFO-PC\nikolaosk];

</pre>
<p>Now I will launch another instance of SSMS and log in as &#8220;nikolaosk&#8221;</p>
<p>In a command prompt type <strong>runas  /user:nikolaosk “C:\Program Files (x86)\Microsoft SQL Server\110\Tools\Binn\ManagementStudio\Ssms.exe”</strong></p>
<p>Then type the password.</p>
<p>In the new SSMS application (running as nikolaos), in a new query  window type again</p>
<pre class="brush: sql; title: ; notranslate">

use master;
GO
CREATE LOGIN Jane WITH PASSWORD = '!@#$%^&amp;abc';

SELECT * FROM sys.dm_exec_cached_plans;

CREATE SERVER ROLE JaneRole;

SELECT * FROM sys.fn_my_permissions(NULL,'server')

</pre>
<p>The first statement will fail since there are no permissions for that operation for the <strong>MyCustomServerRole</strong> user defined role that login <strong>nikolaosk</strong> is member of.</p>
<p>The second statement will succeed but the third statement will fail since there are no permissions for that operation for the <strong>MyCustomServerRole</strong> user defined role that login<strong> nikolaosk</strong> is member of.If you want to see what kind of server level permissions login <strong>nikolaosk</strong> has, execute the last statement.</p>
<p>Close the second SSMS application. Now go back to the first SSMS application.</p>
<p>In a new query window type and execute the following statements.</p>
<pre class="brush: sql; title: ; notranslate">

use master;
GO

DROP LOGIN Mary;
DROP LOGIN [FOFO-PC\nikolaosk];

</pre>
<p>Disconnect from the server instance and then close SSMS.</p>
<p>Hope it helps!!!</p>
<br />Filed under: <a href='http://dotnetstories.wordpress.com/category/sql-server/'>Sql Server</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2012/'>SQL Server 2012</a> Tagged: <a href='http://dotnetstories.wordpress.com/tag/user-defined-server-roles/'>user defined server roles</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dotnetstories.wordpress.com/2002/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dotnetstories.wordpress.com/2002/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=2002&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dotnetstories.wordpress.com/2012/07/07/user-defined-server-roles-in-sql-server-2012/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/ecbfd3584f3b2c0c3f528bacdfc15e0a?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96" medium="image">
			<media:title type="html">fofo</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2011/10/windowsuser.jpg" medium="image">
			<media:title type="html">windowsuser</media:title>
		</media:content>
	</item>
		<item>
		<title>Backing up and restoring the tail-log end of a transaction log</title>
		<link>http://dotnetstories.wordpress.com/2012/07/05/backing-up-and-restoring-the-tail-log-end-of-a-transaction-log/</link>
		<comments>http://dotnetstories.wordpress.com/2012/07/05/backing-up-and-restoring-the-tail-log-end-of-a-transaction-log/#comments</comments>
		<pubDate>Thu, 05 Jul 2012 10:15:43 +0000</pubDate>
		<dc:creator>fofo</dc:creator>
				<category><![CDATA[Sql Server]]></category>
		<category><![CDATA[Sql Server 2005]]></category>
		<category><![CDATA[SQL Server 2008]]></category>
		<category><![CDATA[bulk-logged recovery model]]></category>
		<category><![CDATA[Differential backup]]></category>
		<category><![CDATA[Full backup]]></category>
		<category><![CDATA[full recovery model]]></category>
		<category><![CDATA[simple recovery model]]></category>
		<category><![CDATA[Tail-Log backup]]></category>

		<guid isPermaLink="false">http://dotnetstories.wordpress.com/?p=1989</guid>
		<description><![CDATA[I know a lot of people that are confused about the whole backup and restore process of a database. There are many issues that confuse people but the one I have found the most confusing for people is the use of Tail-log backup.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=1989&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>In this post I would like to demonstrate with a hands-on example how to <strong>backup</strong> and <strong>restore</strong> the tail of the transaction log.In this post I will also demonstrate how to take <strong>full</strong> and <strong>differential</strong> backups and how to restore them. I will also talk about the various SQL Server backup types, recovery models and how these models affect our backup and restore strategy.</p>
<p>But why we take backups in the first place? <strong>Disaster-Recovery</strong> is the first thing that comes to mind.We also use backups in <strong>Log Shipping</strong>,<strong>Database Mirroring</strong>,<strong>Replication</strong>. We do backups when we want to transfer our database from the development to the production server.Finally we take backups when moving or upgrading a database.</p>
<p>One could argue that we take backups when we want to<strong> manage the size of the transaction log</strong>.The only operation that clears the transaction log of a database that is in <strong>FULL or BULK logged recovery model</strong> is a transaction log backup. Some people are performing a backup before performing a database repair or an upgrade of any kind.</p>
<p>Before we proceed I would like to explain the various recovery models. In the <strong>Simple</strong> <strong>Recovery Model</strong> every time I have a checkpoint operation or the .ldf file is full, the .ldf file is automatically truncated.In this model the <strong>transaction log is not backed up</strong>.You can not have point in time data recovery with this model.Data is recoverable only to the most recent backup.</p>
<p>The <strong>Simple</strong> <strong>Recovery Model</strong>  is only suitable for small databases and databases with few transactions.We could use this recovery model if we want to have a small transaction log and for databases that are read-only or in development stage.</p>
<p>The <strong>Full Recovery Model</strong> is the default recovery model.In this model the transaction log grows rapidly since all database transactions are logged in at a very detailed level.If I have a catastrophe in my database I can rollback to the time where the catastrophe occurred.This recovery model is suitable for databases with thousands of transactions per day and where no loss of data is tolerated.</p>
<p>The <strong>Bulk Recovery Model</strong> works almost identically with the <strong>Full Recovery Model</strong>.It logs everything in extents=8pages and all bulk tasks (bcp,bulk insert) are logged minimally.We can go back to the time of failure but it will take more time to get the database back on-line.This model would be very suitable for databases with large bulk tasks.The disadvantage of this model is that you cannot go back at a specific point in time.</p>
<p>Having looked into the recovery models,let&#8217;s have a look on the various <strong>SQL Server backup types</strong>.</p>
<p>Let&#8217;s see what happens when we take a <strong>full database backup</strong>.The first thing to say is that you need always a full database backup.This is the base for all subsequent backups. You cannot have a differential backup,transaction log backup if you do not have at least one full backup.With a full database backup you backup all your data.You can have a full backup with <strong>all the recovery models</strong> that you choose to have for your database.Every full backup I perform in a database contains in it all the data previously fully backed up.<br />
The backup operation is an online operation and the first thing it happens is to log that a backup was started. Then we write the data in the backup file.Then we do log any changes that occur as we backup.Finally the backing up of the data is finished and we log that fact.If you’re performing only full backups, you might lose some data in the event of a system crash—specifically, any changes made since the last full backup.</p>
<p>A <strong>differential backup</strong> is cumulative backup. It includes all data and structures that have changed since the last full backup, regardless of when that last full backup was made, or how many previous differential backups have been run.You can have a differential backup with all the recovery models.</p>
<p>The <strong>Transaction Log backup</strong> only works for full or bulk-logged recovery models.Each T-Log backup contains all the transactions that are in my database since the last T-Log backup.Always perform a full back up before you take a T-log backup.The time taken for a T-log backup is little and there is no performance penalty.An important thing to remember is that for every t-log back up I need the previous backup to have a restore.</p>
<p>I know a lot of people that are confused about the whole backup and restore process of a database. There are many issues that confuse people but the one I have found the most confusing for people is the use of <strong>Tail-log backup</strong>. Well, a tail-log backup is a transaction log backup that holds all the records that have not been backed up. So before we begin our restoration process,because we want to recover our SQL Server database to its latest point in time we must back up the tail of its transaction log.This will be the last back up.One thing that I want to immediately highlight is that you can not have a transaction log backup or tail-log backup unless the database is in either <strong>FULL or BULK-LOGGED recovery model</strong>.</p>
<p>How often you should back up a database depends on how long you have to restore it. In general, the more often you back up a database and the type of backup you take, the shorter the restore time.<br />
You can tailor backups and restores for each database. The kind of backup you decide to use will depend on the size of the database and the amount of transactional activity.</p>
<p>Having only full backups is not a good strategy.If we have only one full database backup taken on 12 a.m every day for a database and our system crashes on 5 p.m, then we lose 5 hours of data.</p>
<p>If you can’t tolerate any data loss on restore, use the <strong>full plus log backup</strong> strategy.With this strategy we have a full database backup taken every day and then numerous T-Log backups at short periods of time. This method guarantees against data loss and works well for databases that are updated frequently. Although using this strategy increases your database’s complexity and maintenance, the total amount of time necessary to back up the database <strong>will decrease</strong>.So one might think that this is the appropriate restore strategy.At some point we will have thousands of files.It will take forever to restore thousands T-Log files.In order to have a good restore strategy for our average highly transactional database we need to include differential backups in our strategy. We could have for example 1 full backup every week, 7 differential backups every night and 336 (a week has 168 hours and I take 2 T-Log backups per hour) T-Log backups.</p>
<p>If you want to see a good backup (restore) strategy, have a look at <a href="http://www.sqlmag.com/content/content/98371/Figure_04.jpg">this</a> picture from <strong>sqlmag.com</strong></p>
<p>Now we are ready to go on with our hands-on example.I will be using the <a href="http://www.microsoft.com/sqlserver/en/us/editions/2012-editions/express.aspx">SQL Server 2012 Express Edition</a></p>
<p>If you have an instance of SQL Server running in your machine,simply connect to it.</p>
<p>In an new query window type</p>
<pre class="brush: sql; title: ; notranslate">

USE master
GO

CREATE DATABASE sampledatabase
GO

ALTER DATABASE sampledatabase SET RECOVERY FULL

USE sampledatabase
GO

CREATE TABLE SampleData
(
id INT PRIMARY KEY ,
col1 tinyint NOT NULL ,
col2 BIT NOT NULL ,
col3 BIT NOT NULL ,
RegistrationDate DATETIME NOT NULL
)

GO

INSERT [dbo].[SampleData] ([id],[col1], [col2], [col3], [RegistrationDate])
VALUES (94495, 25, 1, 1, '02-03-2008')

INSERT [dbo].[SampleData] ([id],[col1], [col2], [col3], [RegistrationDate])
VALUES (94496, 26, 1, 1, '02-04-2008')

INSERT [dbo].[SampleData] ([id],[col1], [col2], [col3], [RegistrationDate])
VALUES (94497, 27, 1, 1, '02-05-2008')

BACKUP DATABASE sampledatabase TO DISK ='C:\sqldata\backups\sampledata_full.bak' WITH INIT,STATS;
GO

INSERT [dbo].[SampleData] ([id],[col1], [col2], [col3], [RegistrationDate])
VALUES (94498, 29, 1, 1, '02-03-2009')

INSERT [dbo].[SampleData] ([id],[col1], [col2], [col3], [RegistrationDate])
VALUES (94499, 28, 1, 1, '02-04-2009')

INSERT [dbo].[SampleData] ([id],[col1], [col2], [col3], [RegistrationDate])
VALUES (94500, 30, 1, 1, '02-05-2009')

SELECT * FROM sampledata

BACKUP DATABASE sampledatabase TO DISK = 'C:\sqldata\backups\sampledata_diff.bak' WITH DIFFERENTIAL
GO

INSERT [dbo].[SampleData] ([id],[col1], [col2], [col3], [RegistrationDate])
VALUES (94501, 29, 1, 1, '02-03-2010')

INSERT [dbo].[SampleData] ([id],[col1], [col2], [col3], [RegistrationDate])
VALUES (94502, 28, 1, 1, '02-04-2010')

INSERT [dbo].[SampleData] ([id],[col1], [col2], [col3], [RegistrationDate])
VALUES (94503, 30, 1, 1, '02-05-2010')

BACKUP LOG sampledatabase TO DISK ='C:\sqldata\backups\sampledata_Log1.bak' WITH INIT,STATS;
GO

INSERT [dbo].[SampleData] ([id],[col1], [col2], [col3], [RegistrationDate])
VALUES (94505, 29, 1, 1, '02-08-2010')

INSERT [dbo].[SampleData] ([id],[col1], [col2], [col3], [RegistrationDate])
VALUES (94506, 28, 1, 1, '02-09-2010')

INSERT [dbo].[SampleData] ([id],[col1], [col2], [col3], [RegistrationDate])
VALUES (94507, 30, 1, 1, '12-05-2010')

BACKUP LOG sampledatabase TO DISK ='C:\sqldata\backups\sampledata_Log2.bak' WITH INIT,STATS;
GO

INSERT [dbo].[SampleData] ([id],[col1], [col2], [col3], [RegistrationDate])
VALUES (94508, 29, 1, 1, '02-28-2011')

INSERT [dbo].[SampleData] ([id],[col1], [col2], [col3], [RegistrationDate])
VALUES (94510, 28, 1, 1, '02-27-2011')

INSERT [dbo].[SampleData] ([id],[col1], [col2], [col3], [RegistrationDate])
VALUES (94511, 30, 1, 1, '12-12-2011')

BACKUP LOG sampledatabase TO DISK ='C:\sqldata\backups\sampledata_Log3.bak' WITH INIT,STATS;
GO

INSERT [dbo].[SampleData] ([id],[col1], [col2], [col3], [RegistrationDate])
VALUES (100000, 30, 1, 1, '12-02-2012')

SELECT TOP 5*
FROM sampledata
ORDER BY id DESC

USE master
GO

ALTER DATABASE sampledatabase SET OFFLINE WITH NO_WAIT;

--simulate the disaster by deleting the .mdf file

ALTER DATABASE sampledatabase SET ONLINE

--this will result in a nasty error since there is no database file

BACKUP LOG sampledatabase TO DISK ='C:\sqldata\backups\sampledata_TailLog.bak' WITH INIT,NO_TRUNCATE;
GO

RESTORE DATABASE sampledatabase FROM DISK='C:\sqldata\backups\sampledata_full.bak' WITH NORECOVERY, REPLACE
RESTORE DATABASE sampledatabase FROM DISK='C:\sqldata\backups\sampledata_diff.bak' WITH NORECOVERY

RESTORE DATABASE sampledatabase FROM DISK='C:\sqldata\backups\sampledata_Log1.bak' WITH NORECOVERY

RESTORE DATABASE sampledatabase FROM DISK='C:\sqldata\backups\sampledata_Log2.bak' WITH NORECOVERY

RESTORE DATABASE sampledatabase FROM DISK='C:\sqldata\backups\sampledata_Log3.bak' WITH NORECOVERY

RESTORE DATABASE sampledatabase FROM DISK='C:\sqldata\backups\sampledata_TailLog.bak' WITH NORECOVERY

RESTORE DATABASE sampledatabase WITH RECOVERY

USE sampledatabase
GO

SELECT TOP 5*
FROM sampledata
ORDER BY id DESC

</pre>
<p>Let me explain what I am doing in the T-SQL script above.</p>
<p>1) In lines <strong>1-21</strong> I create a sample database,set the recovery model to FULL and create a sample table in it.</p>
<p>2) In lines <strong>23-30</strong> I insert 3 records in the table.</p>
<p>3) In lines <strong>32-33</strong> I take a full database backup.</p>
<p>4) In lines <strong>35-42</strong> I insert 3 more records in the table.</p>
<p>5) In lines <strong>46-47</strong> I take a differential backup.</p>
<p>6) In lines <strong>49-56</strong> I insert 3 more records in the table.</p>
<p>7) In lines <strong>58-59</strong> I take another transaction log backup.</p>
<p>8) In lines <strong>61-68</strong> I insert 3 more records in the table.</p>
<p>9) In lines <strong>70-71</strong> I take another transactional log backup.</p>
<p>10) In lines <strong>73-80</strong> I insert 3 more records in the table.</p>
<p>11) In lines <strong>82-83</strong> I take another transaction log backup.</p>
<p>12) In lines <strong>85-86</strong> I insert a new record in the table.</p>
<p>13) In lines <strong>88-90</strong> Ι perform a simple &#8220;Select&#8221; to see the newly inserted record.</p>
<p>14) In lines <strong>92-95</strong> I take the database offline.Then I go to the local path and delete the .mdf file.In my case the path is <strong>C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA</strong>. As you can understand I am simulating a disaster.</p>
<p>15) In line <strong>99</strong> I try to take the database online but I receive a nasty error as expected.</p>
<p>16) In lines <strong>103-104</strong> I take a tail-log backup.This is the first step in the restore process.</p>
<p>17) In lines <strong>106-117</strong> I restore with the correct sequence (full,differential,3 t-log backups,final tail-log backup). Everything works fine.</p>
<p>18) In lines <strong>122-124</strong>, I am looking for the last record (id=100.000) that was inserted after the last t-log backup but was picked up from the tail-log backup. So despite the catastrophe I had, my database is back online and there is no data loss.</p>
<p>Please note that not everyone has the permission to backup a database.This permission is limited by default to members of the <strong>sysadmin</strong> fixed server role,members of the <strong>db_owner</strong> and <strong>db_backupoperator</strong> fixed database role.</p>
<p>Hope it helps!!!</p>
<br />Filed under: <a href='http://dotnetstories.wordpress.com/category/sql-server/'>Sql Server</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2005/'>Sql Server 2005</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2008/'>SQL Server 2008</a> Tagged: <a href='http://dotnetstories.wordpress.com/tag/bulk-logged-recovery-model/'>bulk-logged recovery model</a>, <a href='http://dotnetstories.wordpress.com/tag/differential-backup/'>Differential backup</a>, <a href='http://dotnetstories.wordpress.com/tag/full-backup/'>Full backup</a>, <a href='http://dotnetstories.wordpress.com/tag/full-recovery-model/'>full recovery model</a>, <a href='http://dotnetstories.wordpress.com/tag/simple-recovery-model/'>simple recovery model</a>, <a href='http://dotnetstories.wordpress.com/tag/tail-log-backup/'>Tail-Log backup</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dotnetstories.wordpress.com/1989/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dotnetstories.wordpress.com/1989/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=1989&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dotnetstories.wordpress.com/2012/07/05/backing-up-and-restoring-the-tail-log-end-of-a-transaction-log/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/ecbfd3584f3b2c0c3f528bacdfc15e0a?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96" medium="image">
			<media:title type="html">fofo</media:title>
		</media:content>
	</item>
		<item>
		<title>Restoring a database from SQL Server Enterprise Edition to any other edition of SQL Server</title>
		<link>http://dotnetstories.wordpress.com/2012/06/20/restoring-a-database-from-sql-server-enterprise-edition-to-any-other-edition-of-sql-server/</link>
		<comments>http://dotnetstories.wordpress.com/2012/06/20/restoring-a-database-from-sql-server-enterprise-edition-to-any-other-edition-of-sql-server/#comments</comments>
		<pubDate>Wed, 20 Jun 2012 19:11:10 +0000</pubDate>
		<dc:creator>fofo</dc:creator>
				<category><![CDATA[Sql Server]]></category>
		<category><![CDATA[SQL Server 2008]]></category>

		<guid isPermaLink="false">http://dotnetstories.wordpress.com/?p=1983</guid>
		<description><![CDATA[In this post I would like to demonstrate with a hands-on example that is not always possible to restore  a database to any Edition of SQL Server.We assume of course that the version of SQL Server remains the same. In my machine I have various instances of SQL Server running.I have an instance of SQL Server 2008 R2 Developer Edition and an instance of SQL Server 2008 R2 Standard edition.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=1983&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>In this post I would like to demonstrate with a hands-on example that is not always possible to restore  a database to any Edition of SQL Server.We assume of course that the version of SQL Server remains the same. In my machine I have various instances of SQL Server running.I have an instance of <strong>SQL Server 2008 R2 Developer Edition</strong> and an instance of <strong>SQL Server 2008 R2 Standard edition</strong>.</p>
<ul>
<li>If I have a database running on a (SQL Server 2008 R2 Developer Edition-Enterprise Edition) that has no enterprise features and take a backup of that database, I can restore it to a<strong> SQL Server 2008 R2 Standard</strong> edition istance.</li>
<li>If I have a database that has enterprise features (data compression,transparent data encryption,partitioning,change data capture) and try to restore it to an <strong>SQL Server 2008 Standard Edition</strong> instance I will fail.</li>
</ul>
<p>Let&#8217;s move on to our actual demonstration.In this example I will create a database and then create a table. I will insert some rows into the table. I will then compress the rows in the table.Data compression is an <strong>Enterprise</strong> feature only.Finally I will take a full database backup.</p>
<p>I will connect to this instance of SQL Server 2008 R2 Developer edition and in a new query window I will type</p>
<pre class="brush: sql; title: ; notranslate">

USE master;
GO

IF DATABASEPROPERTYEX ('MyDB', 'Version') &gt; 0
DROP DATABASE MyDB;
GO

CREATE DATABASE MyDB
ON (NAME = MyDB_Data,
FILENAME = N'C:\SqlData\MyDB_data.mdf')
LOG ON (NAME = MyDB_Log,
FILENAME = N'C:\SqlData\MyDB_log.ldf');
GO

USE MyDB;
GO

CREATE TABLE MyCompressionTable (ID INT, phrase1 CHAR(100),phrase2 CHAR(100))
GO

INSERT INTO MyCompressionTable VALUES (1,
'SQL Server is the best RDBMS','I love T-SQL')

GO 15000

-- DATA_COMPRESSION = ROW
ALTER TABLE MyCompressionTable
REBUILD WITH (DATA_COMPRESSION = ROW);
GO

BACKUP DATABASE MyDB
TO DISK = N'C:\SqlData\MyDB.bak'
WITH INIT;
GO

</pre>
<p>I am going to connect to the <strong>SQL Server 2008 R2 Standard Edition</strong> running in my machine.I will connect to this instance and try to restore the backup I have just taken.</p>
<pre class="brush: sql; title: ; notranslate">

USE master;
GO

RESTORE DATABASE MyDB2
FROM DISK = N'C:\SqlData\MyDB.bak'
WITH MOVE N'MyDB_Data' TO N'C:\SqlData\MyDB_data2.mdf',
MOVE N'MyDB_Log' TO N'C:\SqlData\MyDB_log2.ldf',
REPLACE, STATS = 10;
GO

</pre>
<p>I will receive an error after executing the query above. You can see below (a part) the error message</p>
<h6><em>RESTORE could not start database &#8216;MyDB2&#8242;.</em></h6>
<h6><em>RESTORE DATABASE is terminating abnormally.</em></h6>
<h6><em>Database &#8216;MyDB2&#8242; cannot be started in this edition of SQL Server because part or all of object &#8216;compressed&#8217; is enabled with data compression or vardecimal storage format. Data compression and vardecimal storage format are only supported on SQL Server Enterprise Edition.</em></h6>
<h6><em>Database &#8216;MYyDB2&#8242; cannot be started because some of the database functionality is not available in the current edition of SQL Server.</em></h6>
<p>To recap,if you&#8217;re not using the persistent Enterprise-only features (Partitioning, Data Compression, TDE,Change Data Capture), an Enterprise edition database can even be restored event to SQL Server Express edition, if of course is small enough.</p>
<p>I think DBAs should be very careful.It happens quite often (because of the cost) to buy licenses of Standard SQL Server.A DBA can have this instance as part of his disaster recovery strategy.When the disaster happens and then tries to restore the backup to the  SQL Server Standard Edition instance, that will  fail if the database has enterprise features.</p>
<p>There is a dynamic management view in SQL Server 2008 that can help us identify if a database has Enterprise features. This is the <strong>sys.dm_db_persisted_sku_features </strong>dynamic management view<strong>.<br />
</strong></p>
<p>In any query window you can type the following query.</p>
<h5><em>SELECT * FROM sys.dm_db_persisted_sku_features;</em></h5>
<p>In my example the output was</p>
<p><strong>Compression   100</strong></p>
<p>Hope it helps!!!</p>
<br />Filed under: <a href='http://dotnetstories.wordpress.com/category/sql-server/'>Sql Server</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2008/'>SQL Server 2008</a>  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dotnetstories.wordpress.com/1983/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dotnetstories.wordpress.com/1983/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=1983&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dotnetstories.wordpress.com/2012/06/20/restoring-a-database-from-sql-server-enterprise-edition-to-any-other-edition-of-sql-server/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/ecbfd3584f3b2c0c3f528bacdfc15e0a?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96" medium="image">
			<media:title type="html">fofo</media:title>
		</media:content>
	</item>
		<item>
		<title>Restoring a SQL Server database to an older version of Sql Server</title>
		<link>http://dotnetstories.wordpress.com/2012/06/20/restoring-a-sql-server-database-to-an-older-version-of-sql-server/</link>
		<comments>http://dotnetstories.wordpress.com/2012/06/20/restoring-a-sql-server-database-to-an-older-version-of-sql-server/#comments</comments>
		<pubDate>Wed, 20 Jun 2012 01:04:54 +0000</pubDate>
		<dc:creator>fofo</dc:creator>
				<category><![CDATA[Sql Server]]></category>
		<category><![CDATA[SQL Server 2008]]></category>
		<category><![CDATA[backup]]></category>
		<category><![CDATA[restore]]></category>

		<guid isPermaLink="false">http://dotnetstories.wordpress.com/?p=1974</guid>
		<description><![CDATA[In this post I would like to talk about a common issue that confuses SQL Server DBAs and developers.RecentlyI had to deploy an ASP.Net web site from the development server to the live production server.The database server on the deployment server was SQL Server 2008 R2<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=1974&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>In this post I would like to talk about a common issue that confuses SQL Server DBAs and developers.Recently I had to deploy an ASP.Net web site from the development server to the live production server.The database server on the deployment server was <strong>SQL Server 2008 R2</strong>.The hosting provider could only provide us with a <strong>SQL Server 2008</strong> database server.Most of my colleagues thought there was no problem with that.I had a different opinion.Ι know that this is not possible.</p>
<p>You cannot take a backup of a <strong>SQL Server 2008R2</strong> database and try to restore it to a <strong>SQL Server 2008</strong> server.Similarly you cannot take a backup of  a <strong>SQL Server 2012</strong> database and restore it to a <strong>SQL Server 2008 R2</strong> server.</p>
<p>You probably think that this is some sort of a bug.It is not.Each database has a physical number.SQL Server can understand a minimum and a maximum database physical version number.</p>
<p>I will create a short hands-on example to demonstrate that.I have an SQL Server express 2008 edition instance running on my machine.I also have SQL Server 2008 R2 Developer edition.</p>
<p>I will connect to this instance of SQL Server 2008 Developer edition and in a new query window I will type</p>
<pre class="brush: sql; title: ; notranslate">

USE master;
GO
IF DATABASEPROPERTYEX ('MyDB', 'Version') &gt;; 0
DROP DATABASE MyDB;
GO

-- Create database
CREATE DATABASE MyDB
ON (NAME = MyDB_Data,
FILENAME = N'C:\SqlData\MyDB_data.mdf')
LOG ON (NAME = MyDB_Log,
FILENAME = N'C:\SqlData\MyDB_log.ldf');
GO

-- Take full database backup
BACKUP DATABASE MyDB
TO DISK = N'C:\SqlData\MyDB.bak'
WITH INIT;
GO

</pre>
<p>I have created a database and have taken a full database backup.</p>
<p>I am going to connect to the SQL Server 2008 Express edition running in my machine.I will connect to this instance and try to restore the backup.</p>
<pre class="brush: sql; title: ; notranslate">

USE master;
GO

RESTORE DATABASE MyDB2
FROM DISK = N'C:\SqlData\MyDB.bak'
WITH MOVE N'MyDB_Data' TO N'C:\SqlData\MyDB_data2.mdf',
MOVE N'MyDB_Log' TO N'C:\SqlData\MyDB_log2.ldf',
REPLACE, STATS = 10;
GO

</pre>
<p>I will receive an error after executing the query above</p>
<p>Have a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/06/sqlversionerr.jpg"><img class="aligncenter size-large wp-image-1976" title="sqlversionerr" src="http://dotnetstories.files.wordpress.com/2012/06/sqlversionerr.jpg?w=1024&#038;h=571" alt="" width="1024" height="571" /></a></p>
<p>The only way I could solve my problem was to use another functionality through SSMS-<strong>Generate Scripts</strong>.I selected the <strong>MyDB</strong> database and then right-clicked on <strong>Tasks&#8212;&gt;;Generate Scripts</strong>. The wizard pops up.I clicked <strong>Next</strong> on the first screen.In the next screen I selected &#8220;Script entire database and all database objects&#8221; .Then I clicked <strong>Next</strong>.I chose to save the script to a file and not to clipboard or query window. Then I clicked the <strong>Advanced</strong> button. I had to select a few options. Have a look at the picture below.</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/06/script.jpg"><img class="aligncenter size-large wp-image-1978" title="script" src="http://dotnetstories.files.wordpress.com/2012/06/script.jpg?w=1024&#038;h=578" alt="" width="1024" height="578" /></a></p>
<p>In your case you would probably need to select different options&#8230;Then I clicked <strong>OK</strong> and then <strong>Next</strong>.Then I clicked <strong>Next</strong> once more, the script was generated and then I closed the wizard by clicking <strong>Finish</strong>.Then I just had to run the new script in the SQL Server 2008 R2 developer edition.My database was finally&#8221; restored&#8221; in the production server.</p>
<p><strong></strong>Finally I want to mention that compatibility level has nothing to do with trying to restore a backup to an earlier version of SQL Server.If you set the compatibility level of a database to e.g compatibility level 90 or 100 that does not mean that an SQL Server 2012 database backup can be restored on a SQL Server 2008 database server.</p>
<p>Hope it helps!!!</p>
<br />Filed under: <a href='http://dotnetstories.wordpress.com/category/sql-server/'>Sql Server</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2008/'>SQL Server 2008</a> Tagged: <a href='http://dotnetstories.wordpress.com/tag/backup/'>backup</a>, <a href='http://dotnetstories.wordpress.com/tag/restore/'>restore</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dotnetstories.wordpress.com/1974/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dotnetstories.wordpress.com/1974/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=1974&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dotnetstories.wordpress.com/2012/06/20/restoring-a-sql-server-database-to-an-older-version-of-sql-server/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/ecbfd3584f3b2c0c3f528bacdfc15e0a?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96" medium="image">
			<media:title type="html">fofo</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/06/sqlversionerr.jpg?w=1024" medium="image">
			<media:title type="html">sqlversionerr</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/06/script.jpg?w=1024" medium="image">
			<media:title type="html">script</media:title>
		</media:content>
	</item>
		<item>
		<title>Policy Based Management in SQL Server 2008/R2</title>
		<link>http://dotnetstories.wordpress.com/2012/06/05/policy-based-management-in-sql-server-2008r2/</link>
		<comments>http://dotnetstories.wordpress.com/2012/06/05/policy-based-management-in-sql-server-2008r2/#comments</comments>
		<pubDate>Mon, 04 Jun 2012 21:59:43 +0000</pubDate>
		<dc:creator>fofo</dc:creator>
				<category><![CDATA[Sql Server]]></category>
		<category><![CDATA[SQL Server 2008]]></category>
		<category><![CDATA[conditions]]></category>
		<category><![CDATA[facets]]></category>
		<category><![CDATA[policies]]></category>
		<category><![CDATA[Policy Based Management]]></category>

		<guid isPermaLink="false">http://dotnetstories.wordpress.com/?p=1956</guid>
		<description><![CDATA[In this post I would like to about a new feature in SQL Server 2008, namely Policy-Based Management.With this new feature DBAs can enforce policies to an instance of SQL Server or many instances of SQL Server they are responsible for.

Most DBAs  have to administer many SQL Server instances.It is not efficient to configure and manage them separately.DBAs try to enforce standards (e.g naming standards) on the instances they manage but it is very difficult to enforce them.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=1956&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>In this post I would like to about a new feature in SQL Server 2008, namely <strong>Policy-Based Management</strong>.With this new feature DBAs can enforce policies to an instance of SQL Server or many instances of SQL Server they are responsible for.</p>
<p>Most DBAs  have to administer many SQL Server instances.It is not efficient to configure and manage them separately.DBAs try to enforce standards (e.g naming standards) on the instances they manage but it is very difficult to enforce them.</p>
<p>Policy-Based Management (<strong>PBM</strong>) is available in SQL Server 2008 Enterprise and Standard edition.</p>
<p>As it was mentioned earlier <strong>PBM</strong> allows you to define and enforce policies on SQL Server.I will show you with a hands-on example  how to achieve that.We will create a simple policy that will enforce a naming convention (All user defined stored procedures for all the databases in the server must begin with the prefix <strong>usp</strong> )</p>
<p>We need an instance of SQL Server running in our machine. I have SQL Server 2008 R2 Developer edition installed in my machine.</p>
<p>In case you do not have an instance of Enterprise edition or Developer edition  of SQL Server 2008 / R2, you can download it here . This is a trial version.</p>
<p>There are a number of terms that we need to define in order to begin to understand Policy-Based Management:</p>
<ol>
<li><strong>Target</strong> &#8211; an entity (a database, a table, an index) that is managed by <strong>PΒΜ</strong></li>
<li><strong>Facet</strong> &#8211; a predefined set of properties that can be managed</li>
<li><strong>Condition</strong> &#8211; a property expression that evaluates to True or False, e.g the state of a Facet</li>
<li><strong>Policy</strong> &#8211; a condition to be checked and/or enforced</li>
</ol>
<p>Policy-Based Management is configured in SQL Server Management Studio (SSMS).</p>
<p>1) Launch SQL Server Management Studio and log in to the local instance of the SQL Server.</p>
<p>2) Navigate to the <strong>Object Explorer</strong> and expand the <strong>Management</strong> node and the <strong>Policy Management node</strong>.Υou will see the <strong>Policies</strong>, <strong>Conditions</strong>, and <strong>Facets</strong> nodes.</p>
<p>3)Right-click on <strong>Policies</strong> node and then select <strong>New Policy</strong>.Give your policy a name.</p>
<p>Have a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/06/policy1.jpg"><img class="aligncenter size-large wp-image-1958" title="policy1" src="http://dotnetstories.files.wordpress.com/2012/06/policy1.jpg?w=1024&#038;h=583" alt="" width="1024" height="583" /></a></p>
<p>Then in the <strong>Check Condition</strong> dropdown click <strong>New Condition</strong>,fill in the name and the expression.Have a look in the picture below.</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/06/policy2.jpg"><img class="aligncenter size-large wp-image-1962" title="policy2" src="http://dotnetstories.files.wordpress.com/2012/06/policy2.jpg?w=1024&#038;h=576" alt="" width="1024" height="576" /></a><br />
<strong></strong></p>
<p>and then Click <strong>OK.</strong></p>
<p>4) In the evaluation mode we select &#8220;On change:prevent&#8221; .This means that if some user makes a change that will cause a policy to evaluate to false, then this action will be prevented.</p>
<p>Have a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/06/policy-31.jpg"><img class="aligncenter size-large wp-image-1964" title="policy-3" src="http://dotnetstories.files.wordpress.com/2012/06/policy-31.jpg?w=1024&#038;h=575" alt="" width="1024" height="575" /></a></p>
<p>Finally we click <strong>OK</strong> on the policy window.</p>
<p>5)Now we need to test the new policy.In an new query window type</p>
<pre class="brush: sql; title: ; notranslate">

CREATE PROCEDURE test_SP1
 AS

PRINT 'this will violate a policy'

</pre>
<p>When you try to execute the procedure above (for any database in the server) you will receive an error message from SQL Server since there is a policy violation.</p>
<p>Have a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/06/policy-violation.jpg"><img class="aligncenter size-large wp-image-1966" title="policy-violation" src="http://dotnetstories.files.wordpress.com/2012/06/policy-violation.jpg?w=1024&#038;h=580" alt="" width="1024" height="580" /></a></p>
<p>At this point I hope you have a good understanding of what <strong>PBM</strong> can do for enforcing policies in an SQL Server instance.</p>
<p>Hope this helps!!!!</p>
<br />Filed under: <a href='http://dotnetstories.wordpress.com/category/sql-server/'>Sql Server</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2008/'>SQL Server 2008</a> Tagged: <a href='http://dotnetstories.wordpress.com/tag/conditions/'>conditions</a>, <a href='http://dotnetstories.wordpress.com/tag/facets/'>facets</a>, <a href='http://dotnetstories.wordpress.com/tag/policies/'>policies</a>, <a href='http://dotnetstories.wordpress.com/tag/policy-based-management/'>Policy Based Management</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dotnetstories.wordpress.com/1956/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dotnetstories.wordpress.com/1956/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=1956&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dotnetstories.wordpress.com/2012/06/05/policy-based-management-in-sql-server-2008r2/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/ecbfd3584f3b2c0c3f528bacdfc15e0a?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96" medium="image">
			<media:title type="html">fofo</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/06/policy1.jpg?w=1024" medium="image">
			<media:title type="html">policy1</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/06/policy2.jpg?w=1024" medium="image">
			<media:title type="html">policy2</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/06/policy-31.jpg?w=1024" medium="image">
			<media:title type="html">policy-3</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/06/policy-violation.jpg?w=1024" medium="image">
			<media:title type="html">policy-violation</media:title>
		</media:content>
	</item>
		<item>
		<title>Automating SQL Server Administration Tasks</title>
		<link>http://dotnetstories.wordpress.com/2012/05/23/automating-sql-server-administration-tasks/</link>
		<comments>http://dotnetstories.wordpress.com/2012/05/23/automating-sql-server-administration-tasks/#comments</comments>
		<pubDate>Wed, 23 May 2012 20:55:38 +0000</pubDate>
		<dc:creator>fofo</dc:creator>
				<category><![CDATA[Sql Server 2005]]></category>
		<category><![CDATA[SQL Server 2008]]></category>
		<category><![CDATA[alerts]]></category>
		<category><![CDATA[jobs]]></category>
		<category><![CDATA[notifications]]></category>

		<guid isPermaLink="false">http://dotnetstories.wordpress.com/?p=1889</guid>
		<description><![CDATA[In this post I would like to demonstrate with hands-on examples how to use automation (jobs,alerts,operators,notifications) in SQL Server to automate SQL Server administration tasks.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=1889&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>In this post I would like to demonstrate with hands-on examples how to use automation (jobs,alerts,operators,notifications) in SQL Server to automate SQL Server administration tasks.</p>
<p>The importance of automating tasks is profound.The following tasks could be automated</p>
<ul>
<li>Backing up the database</li>
<li>Checking data integrity</li>
<li>Dropping and re-creating indexes</li>
<li>Transferring data</li>
</ul>
<p>We can automate all these tasks to occur on a regular schedule.A common example is to automate the <strong>Backup of a database</strong>, every Saturday night e.g at 12.00 am.</p>
<p>For task automation we need <strong>SQL Server service</strong> (<strong>MSSQLSERVER</strong>) to be up and running. The <strong>Windows application log</strong> is the location of all operating system messages for the Windows operating system.<strong>MSSQLSERVER</strong> service is needed to write events to the Windows application log.  We can have a look into the Windows application log using the <strong>Windows Event Viewer</strong>.<strong>SQL Server Agent service</strong> must also be up and running.<strong>SQL Server Agent&#8217;s</strong> role is to take an action after a notification comes through the Windows application log.</p>
<p>Before I move on, let me talk a bit about <strong>SQL Server Agent.</strong> <strong>SQL Server Agent</strong> executes jobs and fire alerts. Some people are confused with the terms <strong>jobs</strong> and <strong>alerts</strong>.Jobs and alerts are defined separately and can be executed independently.A job can invoke an alert but the opposite is also possible.When we have alerts we can also have <strong>notifications</strong> of one or more <strong>operators</strong> by e-mail using <strong>Database Mail</strong>. Have a look in this <a href="http://dotnetstories.wordpress.com/2012/05/11/configuring-database-mail-in-sql-server/">post</a> of mine to see how to configure <strong>Database Mail</strong> in SQL Server.</p>
<p>There are some steps involved when creating a job</p>
<ul>
<li>Create the job and its steps</li>
<li>Create a schedule of the job execution if the job is not to be executed on demand</li>
<li>Notify operators about the status of the job</li>
</ul>
<p>We can define a job and its steps in various ways.We can use T-SQL statements in a job step. We can also use commands that start the execution of a utility like <strong>bcp</strong> command.In a job step we can also execute a program that is developed with Visual Basic or C#. We must always define what happens when a job step fails.A job step starts the next job step when it is successful.If a job step fails any job steps that follow will not be executed.We must define how many times a job step should be retried.</p>
<p>We can create a job using various ways</p>
<ul>
<li>We can launch SSMS and define a job through SSMS</li>
<li>By using system stored procedures (sp_add,sp_add_jobstep)</li>
</ul>
<p>Let&#8217;s begin with our hands-on examples</p>
<p>We need an instance of SQL Server running in our machine. I have SQL Server 2008 R2 Developer edition installed in my machine.</p>
<p>In case you do not have an instance of Enterprise edition or Developer edition  of SQL Server 2008 / R2, you can download it <a href="http://msdn.microsoft.com/en-us/evalcenter/ff459612.aspx" target="_blank">here</a> . This is a trial version.</p>
<p>1) Launch SQL Server Management studio and connect to the desired instance of  SQL Server.</p>
<p>2) I will create a simple job that backs up the <strong>AdventureWorks</strong> database.You can follow my example by backing up any database you wish.</p>
<p>3) Expand <strong>SQL Server Agent</strong>,right-click <strong>Jobs</strong> and then click <strong>New Job.<br />
</strong>The <strong>New Job </strong>window appears.I will fill in a name for the job and a description in the <strong>General</strong> tab</p>
<p>Have a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/05/backup-job.jpg"><img class="aligncenter size-large wp-image-1937" title="backup-job" src="http://dotnetstories.files.wordpress.com/2012/05/backup-job.jpg?w=1024&#038;h=578" alt="" width="1024" height="578" /></a></p>
<p>Please note that all jobs are enabled by default. SQL Server Agent disables jobs if the job schedule is defined  at a specific time that has passed.It also disables jobs that are defined on a recurring basis with an end date that has also passed.</p>
<p>4) Now click on the <strong>Steps</strong> tab and click <strong>New.</strong>The <strong>New Job Step</strong> dialog appears.We must enter a name for the job step.In the <strong>Type</strong> drop down list, I choose Transact-SQL script ( T-SQL ) because the backup of the database will be executed using <strong>T-SQL</strong>.In the <strong>Database</strong> drop down list I choose the <strong>master</strong> database.I do that because master system database must be current if we want to backup a database.</p>
<p>Have a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/05/backup-job-1.jpg"><img class="aligncenter size-large wp-image-1938" title="backup-job-1" src="http://dotnetstories.files.wordpress.com/2012/05/backup-job-1.jpg?w=1024&#038;h=574" alt="" width="1024" height="574" /></a></p>
<p>The T-SQL statement for backing up the database is</p>
<pre class="brush: sql; title: ; notranslate">

BACKUP DATABASE [AdventureWorks] TO
DISK = N'C:\Program Files\Microsoft SQL Server\MSSQL10_50.MSSQLSERVER\MSSQL\Backup\AdventureWorks.bak'
WITH NOFORMAT, NOINIT,  NAME = N'AdventureWorks-Full Database Backup', SKIP, NOREWIND, NOUNLOAD,  STATS = 10
GO

</pre>
<p>5) Now click on the <strong>Advanced</strong> tab to specify <strong>retry attempts and retry interval</strong> .Then click <strong>OK</strong></p>
<p>Ηave a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/05/backup-job-3.jpg"><img class="aligncenter size-large wp-image-1941" title="backup-job-3" src="http://dotnetstories.files.wordpress.com/2012/05/backup-job-3.jpg?w=1024&#038;h=578" alt="" width="1024" height="578" /></a></p>
<p>6) Now we are ready to create a <strong>Job schedule.</strong>Each created job can be executed on demand or by using a schedule.A scheduled job can occur at a specific time or on a recurring schedule.Select the <strong>Schedules</strong> tab and click <strong>New.<br />
</strong>I fill in a name for the schedule and the <strong>schedule type</strong> is <strong>recurring</strong>.It occurs every Monday each week at 9 am starting on 25/05/2012</p>
<p>Have a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/05/backup-job-4.jpg"><img class="aligncenter size-large wp-image-1942" title="backup-job-4" src="http://dotnetstories.files.wordpress.com/2012/05/backup-job-4.jpg?w=1024&#038;h=576" alt="" width="1024" height="576" /></a></p>
<p>Click <strong>OK</strong> on this window.Click <strong>OK</strong> once more to finish creating the job.</p>
<p>7) If you want to create a notification about a job status you need to create an operator first.I will use SSMS to create an operator.Expand <strong>SQL Server Agent</strong> node and ten right-click on <strong>Operators </strong>and then click <strong>New Operator</strong>.Enter the name of the operator and then specify one or more methods of notifying the operator.</p>
<p>Have a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/05/backup-job-5.jpg"><img class="aligncenter size-large wp-image-1946" title="backup-job-5" src="http://dotnetstories.files.wordpress.com/2012/05/backup-job-5.jpg?w=1024&#038;h=578" alt="" width="1024" height="578" /></a></p>
<p>8) To notify one or more operators after the job finishes we need to select the job we just created and select <strong>Properties</strong> and then we need to select the <strong>Notifications</strong> tab.Then we just need to check the corresponding objects.</p>
<p>Have a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/05/backup-job-6.jpg"><img class="aligncenter size-large wp-image-1947" title="backup-job-6" src="http://dotnetstories.files.wordpress.com/2012/05/backup-job-6.jpg?w=1024&#038;h=575" alt="" width="1024" height="575" /></a></p>
<p>Then click <strong>OK</strong></p>
<p>9) If you want to find out information about a job you can have a look at the <strong>sysjobhistory</strong> table of the <strong>msdb</strong> database.You can also select a job and right-click on it and then click <strong>View History</strong>.The <strong>Log File Viewer</strong> dialog box shows the history log of the job.We can find information such as</p>
<ul>
<li>Whether the job completed successfully or not</li>
<li>Duration of the job</li>
<li>Operators who were notified</li>
<li>Date and time when the job step occurred</li>
</ul>
<p>10) Alerts as mentioned earlier can be used to respond to potential problems,system errors and user-defined errors.Information about execution of  jobs ans system error messages is stored in Windows application log.The <strong>SQL Server Agent</strong> reads the log and then compares the stored messages with the alerts defined for the system.If there is a match, <strong>SQL Server Agent</strong> fires the alert.Let&#8217;s create an alert that responds to a system error.Expand SQL Server Agent node, right-click <strong>Alerts</strong> and click <strong>New Alert</strong>.In the <strong>New Alert</strong> dialog box I enter the name (deadlock_alert) of the alert.I will create an alert that will respond to deadlocks.In the <strong>Error number</strong> I enter <strong>1205<br />
</strong>and select the alert type to be SQL Server event alert.</p>
<p>Have a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/05/backup-job-8.jpg"><img class="aligncenter size-large wp-image-1950" title="backup-job-8" src="http://dotnetstories.files.wordpress.com/2012/05/backup-job-8.jpg?w=1024&#038;h=574" alt="" width="1024" height="574" /></a></p>
<p>Now we need to define the response for the alert.Click on the <strong>Response</strong> page.I check the <strong>Notify operators</strong> and then in the operator list I select the operator I created earlier.</p>
<p>Have a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/05/backup-job-7.jpg"><img class="aligncenter size-large wp-image-1949" title="backup-job-7" src="http://dotnetstories.files.wordpress.com/2012/05/backup-job-7.jpg?w=1024&#038;h=578" alt="" width="1024" height="578" /></a></p>
<p>To recap SQL Server allows us to automate administrator tasks such as index maintenance and database backups.We can create jobs,operators,notifications and alerts.<strong> SQL Server Agent</strong> must be up and running to have automation.</p>
<p>Hope it helps!!!</p>
<br />Filed under: <a href='http://dotnetstories.wordpress.com/category/sql-server-2005/'>Sql Server 2005</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2008/'>SQL Server 2008</a> Tagged: <a href='http://dotnetstories.wordpress.com/tag/alerts/'>alerts</a>, <a href='http://dotnetstories.wordpress.com/tag/jobs/'>jobs</a>, <a href='http://dotnetstories.wordpress.com/tag/notifications/'>notifications</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dotnetstories.wordpress.com/1889/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dotnetstories.wordpress.com/1889/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=1889&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dotnetstories.wordpress.com/2012/05/23/automating-sql-server-administration-tasks/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/ecbfd3584f3b2c0c3f528bacdfc15e0a?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96" medium="image">
			<media:title type="html">fofo</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/05/backup-job.jpg?w=1024" medium="image">
			<media:title type="html">backup-job</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/05/backup-job-1.jpg?w=1024" medium="image">
			<media:title type="html">backup-job-1</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/05/backup-job-3.jpg?w=1024" medium="image">
			<media:title type="html">backup-job-3</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/05/backup-job-4.jpg?w=1024" medium="image">
			<media:title type="html">backup-job-4</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/05/backup-job-5.jpg?w=1024" medium="image">
			<media:title type="html">backup-job-5</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/05/backup-job-6.jpg?w=1024" medium="image">
			<media:title type="html">backup-job-6</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/05/backup-job-8.jpg?w=1024" medium="image">
			<media:title type="html">backup-job-8</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/05/backup-job-7.jpg?w=1024" medium="image">
			<media:title type="html">backup-job-7</media:title>
		</media:content>
	</item>
		<item>
		<title>Configuring Database Mail in SQL Server</title>
		<link>http://dotnetstories.wordpress.com/2012/05/11/configuring-database-mail-in-sql-server/</link>
		<comments>http://dotnetstories.wordpress.com/2012/05/11/configuring-database-mail-in-sql-server/#comments</comments>
		<pubDate>Thu, 10 May 2012 23:28:49 +0000</pubDate>
		<dc:creator>fofo</dc:creator>
				<category><![CDATA[Sql Server]]></category>
		<category><![CDATA[Sql Server 2005]]></category>
		<category><![CDATA[SQL Server 2008]]></category>
		<category><![CDATA[Database Mail]]></category>

		<guid isPermaLink="false">http://dotnetstories.wordpress.com/?p=1917</guid>
		<description><![CDATA[In this email I will demonstrate in detail how to set up Database Mail.I am sure that this feature is well documented  elsewhere in the web but I decided to give it a go.

Database Mail is a very useful feature.Both the Maintenance Plan Wizard and Maintenance Plan Designer have the ability to send e-mails to DBAs.

We can use the SQL Server Management Studio to set up Database Mail.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=1917&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>In this email I will demonstrate in detail how to set up <strong>Database Mail</strong>.I am sure that this feature is well documented  elsewhere in the web but I decided to give it a go.</p>
<p><strong>Database Mail</strong> is a very useful feature.Both the <strong>Maintenance Plan Wizard</strong> and <strong>Maintenance Plan Designer</strong> have the ability to send e-mails to DBAs.</p>
<p>We can use the SQL Server Management Studio to set up <strong>Database Mail</strong>.</p>
<p>We need an instance of SQL Server running in our machine. I have<strong> SQL Server 2008 R2 Developer</strong> edition installed in my machine.</p>
<p>In case you do not have an instance of <strong>Enterprise edition or Developer edition  of SQL Server 2008 / R2</strong>, you can download it <a href="http://msdn.microsoft.com/en-us/evalcenter/ff459612.aspx" target="_blank">here</a> . This is a trial version.</p>
<p>1) Launch SQL Server Management studio and connect to the desired instance of the SQL Server.</p>
<p>2) Navigate to the <strong>Management</strong> folder , right-click on <strong>Database Mail </strong>and select <strong>Configure Database</strong> <strong>Mail</strong>.<strong><br />
</strong>You will see the Splash screen and after you click <strong>Next</strong> , the wizard starts off with the <strong>Select Configuration Task</strong></p>
<p>Ηave a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/05/database-mail-1.jpg"><img class="aligncenter size-large wp-image-1918" title="database-mail-1" src="http://dotnetstories.files.wordpress.com/2012/05/database-mail-1.jpg?w=1024&#038;h=574" alt="" width="1024" height="574" /></a></p>
<p>3) We select the option <strong>Set up Database Mail by performing the following tasks</strong>.Click on <strong>Next</strong> to continue</p>
<p>4)If <strong>Database Mail</strong> has not been enabled you will see a message like the one in the picture below.Click <strong>Yes</strong></p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/05/database-mail-2.jpg"><img class="aligncenter size-large wp-image-1919" title="database-mail-2" src="http://dotnetstories.files.wordpress.com/2012/05/database-mail-2.jpg?w=1024&#038;h=577" alt="" width="1024" height="577" /></a></p>
<p>5) If you are interested on how to turn on <strong>Database Mail</strong> in SQL Server you can type the following T-SQL statement in a new query window</p>
<pre class="brush: sql; title: ; notranslate">

sp_CONFIGURE 'show advanced', 1
GO
RECONFIGURE
GO
sp_CONFIGURE 'Database Mail XPs', 1
GO
RECONFIGURE
GO

</pre>
<p>6) In the next screen you will be prompted to create a <strong>Database Mail Profile</strong>.</p>
<p>Have a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/05/database-mail-3.jpg"><img class="aligncenter size-large wp-image-1923" title="database-mail-3" src="http://dotnetstories.files.wordpress.com/2012/05/database-mail-3.jpg?w=1024&#038;h=577" alt="" width="1024" height="577" /></a></p>
<p>A profile is basically a collection of SMTP accounts that can be used by SQL Server to send messages.When SQL Server sends a message it is sent to the profile and then the profile is responsible for seeing that the e-mail has been delivered.</p>
<p>We must enter a profile name,an optional description and then we add and configure an <strong>SMTP</strong> account.</p>
<p>Ι add in the <strong>Profile name</strong> :<strong> SQL Server Admin</strong></p>
<p>I add in the <strong>Description</strong> : <strong>This is a SQL Server Admin profile</strong></p>
<p>Then click the <strong>Add</strong> button.The <strong>New Database Mail Account</strong> screen appears</p>
<p>Have a look at the picture below to see what one might enter in the available fields.</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/05/database-mail-4.jpg"><img class="aligncenter size-large wp-image-1926" title="database-mail-4" src="http://dotnetstories.files.wordpress.com/2012/05/database-mail-4.jpg?w=1024&#038;h=577" alt="" width="1024" height="577" /></a></p>
<p>Ι use <strong>Basic Authentication</strong> which requires to provide a user name and a password. Then I click <strong>OK</strong>.You will return to the original mail profile screen.</p>
<p>7) We now need to continue with the <strong>Database Mail Configuration</strong> Wizard.Click on Next to reach the <strong>Manage Profile Security</strong> screen.</p>
<p>Now we must assign a profile as <strong>public</strong> as <strong>private</strong>.Only specific roles or users have access to private profiles.A public profile allows any user or role with access to send mail.I will keep things simple and I will create a public profile.Then click <strong>Next </strong>to move on to the <strong>Configure System Parameters </strong>screen</p>
<p>Have a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/05/database-mail-5.jpg"><img class="aligncenter size-large wp-image-1927" title="database-mail-5" src="http://dotnetstories.files.wordpress.com/2012/05/database-mail-5.jpg?w=1024&#038;h=577" alt="" width="1024" height="577" /></a></p>
<p>Then click <strong>Next</strong> on the wizard and then we move to the summary screen.We click <strong>Finish</strong> to close the Wizard.</p>
<p>Have a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/05/database-mail-6.jpg"><img class="aligncenter size-large wp-image-1928" title="database-mail-6" src="http://dotnetstories.files.wordpress.com/2012/05/database-mail-6.jpg?w=1024&#038;h=579" alt="" width="1024" height="579" /></a></p>
<p>Click <strong>Close</strong>.The profile and the SMTP account has been created</p>
<p>8)Now we need to check if <strong>Database Mail</strong> works correctly.We can send a test email.Select the <strong>Database Mail</strong> folder, right-click and select <strong>Send Test E-mail</strong></p>
<p>Ηave a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/05/database-mail-7.jpg"><img class="aligncenter size-large wp-image-1929" title="database-mail-7" src="http://dotnetstories.files.wordpress.com/2012/05/database-mail-7.jpg?w=1024&#038;h=574" alt="" width="1024" height="574" /></a></p>
<p>In the <strong>To</strong> field  enter an email address and then click <strong>Send Test E-mail</strong>.It worked fine in my case and the e-mail was successfully sent.</p>
<p>You can use T-SQL to send an email. In a new query window type</p>
<pre class="brush: sql; title: ; notranslate">

USE msdb
GO
EXEC sp_send_dbmail @profile_name='SQL Server Admin',
@recipients='nkantzelis@q-training.gr',
@subject='Hello Admin!!!! Test message',
@body='This is a test message.
Database Mail works!!!!'

</pre>
<p>9) You can find information about sent emails,failed emails and more by typing the following T-SQL statements in a query window.</p>
<pre class="brush: sql; title: ; notranslate">

SELECT *
FROM sysmail_mailitems
GO

SELECT *
FROM sysmail_sentitems
GO

SELECT *
FROM sysmail_faileditems
GO

SELECT *
FROM sysmail_log
GO

</pre>
<p>Hope it helps!!!!!</p>
<br />Filed under: <a href='http://dotnetstories.wordpress.com/category/sql-server/'>Sql Server</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2005/'>Sql Server 2005</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2008/'>SQL Server 2008</a> Tagged: <a href='http://dotnetstories.wordpress.com/tag/database-mail/'>Database Mail</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dotnetstories.wordpress.com/1917/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dotnetstories.wordpress.com/1917/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=1917&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dotnetstories.wordpress.com/2012/05/11/configuring-database-mail-in-sql-server/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/ecbfd3584f3b2c0c3f528bacdfc15e0a?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96" medium="image">
			<media:title type="html">fofo</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/05/database-mail-1.jpg?w=1024" medium="image">
			<media:title type="html">database-mail-1</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/05/database-mail-2.jpg?w=1024" medium="image">
			<media:title type="html">database-mail-2</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/05/database-mail-3.jpg?w=1024" medium="image">
			<media:title type="html">database-mail-3</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/05/database-mail-4.jpg?w=1024" medium="image">
			<media:title type="html">database-mail-4</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/05/database-mail-5.jpg?w=1024" medium="image">
			<media:title type="html">database-mail-5</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/05/database-mail-6.jpg?w=1024" medium="image">
			<media:title type="html">database-mail-6</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/05/database-mail-7.jpg?w=1024" medium="image">
			<media:title type="html">database-mail-7</media:title>
		</media:content>
	</item>
		<item>
		<title>Create and manage partitioned tables in SQL Server</title>
		<link>http://dotnetstories.wordpress.com/2012/05/05/create-and-manage-partitioned-tables-in-sql-server/</link>
		<comments>http://dotnetstories.wordpress.com/2012/05/05/create-and-manage-partitioned-tables-in-sql-server/#comments</comments>
		<pubDate>Sat, 05 May 2012 19:59:22 +0000</pubDate>
		<dc:creator>fofo</dc:creator>
				<category><![CDATA[Sql Server]]></category>
		<category><![CDATA[Sql Server 2005]]></category>
		<category><![CDATA[SQL Server 2008]]></category>
		<category><![CDATA[table partitioning]]></category>

		<guid isPermaLink="false">http://dotnetstories.wordpress.com/?p=1520</guid>
		<description><![CDATA[In this post I would like to explain with some hands-on examples how to create a partitioned table in SQL Server.

I know there are samples of how to use secure data in BOL,MSDN and elsewhere but I thought that it would be a good idea to give it a try with hands-on examples.

Many people have found useful my style of posting. They feel they can follow the steps and master the technology/topic.

We all know that at some point our database is going to grow and possibly become huge. In a database that it is e.g 3 Terabytes in size, we could have tables with million of rows.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=1520&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>In this post I would like to explain with some hands-on examples how to create a <strong>partitioned table</strong> in SQL Server and then manage the partitions.</p>
<p>I know there are samples of how to do that in BOL,MSDN and elsewhere but I thought that it would be a good idea to give it a try with hands-on examples.</p>
<p>Many people have found useful my style of posting. They feel they can follow the steps and master the technology/topic.</p>
<p>We all know that at some point our database is going to grow and possibly become huge. In a database that it is e.g 3 Terabytes in size, we could have tables with million of rows.</p>
<p>When we execute queries against tables with million of rows then we will probably get slow response times.We could partition the tables which means dividing the rows in various partitions based on column values.When you execute queries against tables that are partitioned may be much faster because SQL Server is able to search through a single partition to find the records needed instead of the entire table.</p>
<p>If you want to create a partitioned table you must.</p>
<p>1) Firstly, we have to select the table that must be partitioned.</p>
<p>2) Then we need to select the column on which the partition will take place.</p>
<p>3) Then we need to create a partition function. This is a function that specifies how to partition the table by defining partition key boundaries for each subset of data.</p>
<p>4) Then we need to create as partition scheme.It maps individual partitions to filegroups.</p>
<p>When creating a partition scheme we must bear in mind the following</p>
<ul>
<li>Each partition must map to a filegroup</li>
<li>A single filegroup can contain more than one partition</li>
<li>Each partition can have its own filegroup</li>
<li>All partitions can map to the same filegroup</li>
<li>Defining extra filegroups allows for adding new partitions in the future</li>
<li>A partition scheme uses only one partition function</li>
</ul>
<p>Let’s move on to the hands-on examples.</p>
<p>We need an instance of SQL Server running in our machine. I have<strong> SQL Server 2008 R2 Developer</strong> edition installed in my machine.</p>
<p>In case you do not have an instance of <strong>Enterprise edition or Developer edition  of SQL Server 2008 / R2</strong>, you can download it from <a href="http://msdn.microsoft.com/en-us/evalcenter/ff459612.aspx" target="_blank">here</a> . This is a trial version.</p>
<p>We need a database as well. I will create a brand new database later on.</p>
<p>1) Launch SQL Server Management studio and connect to the desired instance of the SQL Server.We will create a new database and then we will create 4 filegroups that will map to 4 physical files.</p>
<p>In an new query window type the following T-SQL statements and execute them</p>
<pre class="brush: sql; title: ; notranslate">

USE master
GO
CREATE DATABASE UNIVERSITY
GO

ALTER DATABASE UNIVERSITY ADD FILEGROUP UNIVERSITY_DATA_1
GO
ALTER DATABASE UNIVERSITY ADD FILEGROUP UNIVERSITY_DATA_2
GO
ALTER DATABASE UNIVERSITY ADD FILEGROUP UNIVERSITY_DATA_3
GO
ALTER DATABASE UNIVERSITY ADD FILEGROUP UNIVERSITY_DATA_4
GO

ALTER DATABASE UNIVERSITY
ADD FILE
( NAME = UNIVERSITY1,
FILENAME = 'C:\DATA\UNIVERSITY_1.ndf',
SIZE = 100MB,
MAXSIZE = 300MB,
FILEGROWTH = 50MB)
TO FILEGROUP UNIVERSITY_DATA_1
GO

ALTER DATABASE UNIVERSITY
ADD FILE
( NAME = UNIVERSITY2,
FILENAME = 'C:\DATA\UNIVERSITY_2.ndf',
SIZE = 100MB,
MAXSIZE = 300MB,
FILEGROWTH = 50MB)
TO FILEGROUP UNIVERSITY_DATA_2
GO

ALTER DATABASE UNIVERSITY
ADD FILE
( NAME = UNIVERSITY3,
FILENAME = 'D:\DATA\UNIVERSITY_3.ndf',
SIZE = 100MB,
MAXSIZE = 300MB,
FILEGROWTH = 50MB)
TO FILEGROUP UNIVERSITY_DATA_3
GO

ALTER DATABASE UNIVERSITY
ADD FILE
( NAME = UNIVERSITY4,
FILENAME = 'd:\DATA\UNIVERSITY_4.ndf',
SIZE = 100MB,
MAXSIZE = 300MB,
FILEGROWTH = 50MB)
TO FILEGROUP UNIVERSITY_DATA_4
GO

</pre>
<p>2) I will create a table that has information regarding student registration.I will create a column in that table that is called &#8220;RegistrationDate&#8221;.This will be the partition column.Note that I have not created the table yet. This is a good column for the partition because we can partition on different date ranges e.g put the registration dates for the past year in one partition and the registration dates for the period 2009 in another.</p>
<p>3) Now we need to create the partition function.In the same or a new query window type the following T-SQL statements and execute them</p>
<pre class="brush: sql; title: ; notranslate">

USE University
GO

CREATE PARTITION FUNCTION pf_regdate (DATETIME)
AS RANGE RIGHT FOR VALUES ('2008-01-01','2009-01-01','2010-01-01')

--by running this command you can see in that partition will go a record with registration date of 2009-06-01

SELECT $PARTITION.pf_regdate('2009-06-01')

</pre>
<p>I create 4 partitions. All the values that are less than <strong>01-01-2008</strong> will go into their own partition (1st partition).All the values greater than <strong>01-010-2010</strong> will go into the last partition (4th partition)</p>
<p>4)  We now need to create the partition scheme.In the same or a new query window type the following T-SQL statement and execute it</p>
<pre class="brush: sql; title: ; notranslate">

CREATE PARTITION SCHEME ps_regdate AS PARTITION pf_regdate
TO (UNIVERSITY_DATA_1,UNIVERSITY_DATA_2,UNIVERSITY_DATA_3,UNIVERSITY_DATA_4)

</pre>
<p>I need one partition per filegroup at a minimum.</p>
<p>5) Now we must define that we want to create a table on a partition scheme for the specified column.In the same or a new query window type the following T-SQL statements and execute them</p>
<pre class="brush: sql; title: ; notranslate">

CREATE TABLE StudentRegistration
(
id INT IDENTITY(1, 1)NOT NULL,
discount tinyint NOT NULL ,
Paid BIT NOT NULL ,
Confirmed BIT NOT NULL ,
RegistrationDate DATETIME NOT NULL
CONSTRAINT pk_registration PRIMARY KEY (id,RegistrationDate)
) ON ps_regdate (RegistrationDate)

GO

ALTER TABLE StudentRegistration
WITH CHECK

ADD CONSTRAINT CK_discount
CHECK (discount &gt;= 20 AND discount &lt;= 50)

</pre>
<p>6 )Now let&#8217;s insert some data into the table.I will insert only 10 records but as I said in the beginning we apply partitioning on tables that have millions of rows.</p>
<p>In the same or a new query window type the following T-SQL statements and execute them</p>
<pre class="brush: sql; title: ; notranslate">

USE University

SET IDENTITY_INSERT StudentRegistration ON
INSERT [dbo].[StudentRegistration] ([id], [discount], [Paid], [Confirmed],

[RegistrationDate]) VALUES (94495, 25, 1, 1, '02-03-2008')

INSERT [dbo].[StudentRegistration] ([id], [discount], [Paid], [Confirmed], [RegistrationDate])
VALUES (94496, 48, 1, 0, '02-03-2009')

INSERT [dbo].[StudentRegistration] ([id], [discount], [Paid], [Confirmed], [RegistrationDate])
VALUES (94500, 42, 1, 1, '03-03-2005')

INSERT [dbo].[StudentRegistration] ([id], [discount], [Paid], [Confirmed], [RegistrationDate])
VALUES (94501, 42, 1, 1, '04-03-2005')

INSERT [dbo].[StudentRegistration] ([id], [discount], [Paid], [Confirmed], [RegistrationDate])
VALUES (94502, 32, 0, 1, '01-07-2010')

INSERT [dbo].[StudentRegistration] ([id], [discount], [Paid], [Confirmed], [RegistrationDate])
VALUES (94502, 32, 0, 1, '01-07-2010')

INSERT [dbo].[StudentRegistration] ([id], [discount], [Paid], [Confirmed], [RegistrationDate])
VALUES (94503, 32, 0, 1, '01-07-2011')

INSERT [dbo].[StudentRegistration] ([id], [discount], [Paid], [Confirmed], [RegistrationDate])
VALUES (94504, 32, 0, 1, '01-07-2006')

INSERT [dbo].[StudentRegistration] ([id], [discount], [Paid], [Confirmed], [RegistrationDate])
VALUES (94506, 32, 0, 1, '01-07-2008')

INSERT [dbo].[StudentRegistration] ([id], [discount], [Paid], [Confirmed], [RegistrationDate])
VALUES (94509, 32, 0, 1, '07-07-2009')

SET IDENTITY_INSERT StudentRegistration OFF

</pre>
<p>7) We can see the newly created partitions and the rows in each of them by executing the T-SQL statement below.</p>
<pre class="brush: sql; title: ; notranslate">

SELECT OBJECT_Name(object_id),
* FROM sys.partitions
WHERE OBJECT_Name(object_id) = 'StudentRegistration'

</pre>
<p>Have a look at the picture below to see what I mean</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/05/partition.jpg"><img class="aligncenter size-large wp-image-1908" title="partition" src="http://dotnetstories.files.wordpress.com/2012/05/partition.jpg?w=1024&#038;h=550" alt="" width="1024" height="550" /></a></p>
<p>8) Νο Ι am going to demonstrate how to manage partitions.I am going to show you how to archive a partition into a separate table and then split an existing partition in two.</p>
<p>Let&#8217;s assume that we want to archive the records from the first partition (rows with registration date earlier than <strong>1-1-2008</strong>) into a separate table.</p>
<p>Firstly I need to create the table and then move the records.Type the following T-SQL statements in a new query window and execute them</p>
<pre class="brush: sql; title: ; notranslate">

USE UNIVERSITY
GO

CREATE TABLE StudentRegistrationHistory
(
id INT IDENTITY(1, 1)NOT NULL,
discount tinyint NOT NULL ,
Paid BIT NOT NULL ,
Confirmed BIT NOT NULL ,
RegistrationDate DATETIME NOT NULL
CONSTRAINT pk_registration_history PRIMARY KEY (id,RegistrationDate)
) ON UNIVERSITY_DATA_1

ALTER TABLE dbo.StudentRegistration SWITCH PARTITION 1 TO StudentRegistrationHistory

--will see 3 records with registration date earlier than 1-1-2008
SELECT * FROM StudentRegistrationHistory

--all the other records remaining will be in the StudentRegistration table in the same partitions

SELECT * FROM StudentRegistration

</pre>
<p>I create the table-history/archive table.</p>
<p>I accomplish the move from <strong>partition 1</strong> of the first table to<strong> StudentRegistrationHistory</strong>  by executing the statement</p>
<p>&#8220;ALTER TABLE dbo.StudentRegistration SWITCH PARTITION 1 TO StudentRegistrationHistory &#8220;</p>
<p>Then I execute SELECT statements to see that the move actually happened.Partition 1  exists but has no records</p>
<p>9) Moving on let&#8217;s demonstrate how to split an existing partition to two partitions. I want to change my last partition that holds records from 1-1-2010 onward to two partitions that have records from 1-1-2010 to 1-1-2011 and another one from 1-1-2011 to present.</p>
<p>Type the following T-SQL statements in a new query window and execute them</p>
<pre class="brush: sql; title: ; notranslate">

USE UNIVERSITY
GO

ALTER DATABASE UNIVERSITY ADD FILEGROUP UNIVERSITY_DATA_5
GO

ALTER DATABASE UNIVERSITY
ADD FILE
( NAME = UNIVERSITY5,
FILENAME = 'D:\DATA\UNIVERSITY_5.ndf',
SIZE = 100MB,
MAXSIZE = 300MB,
FILEGROWTH = 50MB)
TO FILEGROUP UNIVERSITY_DATA_5
GO

ALTER PARTITION SCHEME ps_regdate

NEXT USED UNIVERSITY_DATA_5

ALTER PARTITION FUNCTION pf_regdate()
SPLIT RANGE ('2011-01-01')

SELECT OBJECT_Name(object_id),
* FROM sys.partitions
WHERE OBJECT_Name(object_id) = 'StudentRegistration'

</pre>
<p>I create a new filegroup.Then I add a file to the newly created filegroup.Then I change the <strong>partition scheme</strong>  and finally split the records into the new partition using the <strong>SPLIT</strong> operator.</p>
<p>When I execute the last statement</p>
<p>(SELECT OBJECT_Name(object_id),<br />
* FROM sys.partitions<br />
WHERE OBJECT_Name(object_id) = &#8216;StudentRegistration&#8217;)</p>
<p>I see that I have 5 partitions now.Have a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/05/partition5.jpg"><img class="aligncenter  wp-image-1911" title="partition5" src="http://dotnetstories.files.wordpress.com/2012/05/partition5.jpg?w=1024&#038;h=550" alt="" width="1024" height="550" /></a></p>
<p>10) Now I want to <strong>merge</strong> the records from the partition with values from  <strong>1-1-2008 to 1-1-2009</strong> with the partition with values from <strong>1-1-2009 </strong> to <strong>1-1-2010</strong>.</p>
<p>Type the following T-SQL statements in a new query window and execute them</p>
<pre class="brush: sql; title: ; notranslate">

USE UNIVERSITY
GO

ALTER PARTITION FUNCTION pf_regdate()
MERGE RANGE ('2009-01-01')

SELECT OBJECT_Name(object_id),
* FROM sys.partitions
WHERE OBJECT_Name(object_id) = 'StudentRegistration'

</pre>
<p>I use the MERGE operator to achieve that by altering the partitioning function.When I execute the last statement I see that I have 4 partitions now.</p>
<p>Hope it helps!!!</p>
<br />Filed under: <a href='http://dotnetstories.wordpress.com/category/sql-server/'>Sql Server</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2005/'>Sql Server 2005</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2008/'>SQL Server 2008</a> Tagged: <a href='http://dotnetstories.wordpress.com/tag/table-partitioning/'>table partitioning</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dotnetstories.wordpress.com/1520/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dotnetstories.wordpress.com/1520/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=1520&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dotnetstories.wordpress.com/2012/05/05/create-and-manage-partitioned-tables-in-sql-server/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/ecbfd3584f3b2c0c3f528bacdfc15e0a?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96" medium="image">
			<media:title type="html">fofo</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/05/partition.jpg?w=1024" medium="image">
			<media:title type="html">partition</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/05/partition5.jpg?w=1024" medium="image">
			<media:title type="html">partition5</media:title>
		</media:content>
	</item>
		<item>
		<title>Collation and SQL Server Data management</title>
		<link>http://dotnetstories.wordpress.com/2012/05/02/collation-and-sql-server-data-management/</link>
		<comments>http://dotnetstories.wordpress.com/2012/05/02/collation-and-sql-server-data-management/#comments</comments>
		<pubDate>Tue, 01 May 2012 23:45:13 +0000</pubDate>
		<dc:creator>fofo</dc:creator>
				<category><![CDATA[Sql Server]]></category>
		<category><![CDATA[Sql Server 2005]]></category>
		<category><![CDATA[SQL Server 2008]]></category>
		<category><![CDATA[collation]]></category>

		<guid isPermaLink="false">http://dotnetstories.wordpress.com/?p=1893</guid>
		<description><![CDATA[In this rather short post I would like to explain the concept of  collation in SQL Server. This is a concept that stills confuses SQL Server DBAs and developers. I know that is a well documented feature (in books &#38; the web) but I will give it a shot.In this post I will show you how collation affects the storage and behavior of your character data.Data is global nowadays.That means that at some point we will deal with databases that have data from different languages<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=1893&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>In this rather short post I would like to explain the concept of  <strong>collation</strong> in SQL Server. This is a concept that stills confuses SQL Server DBAs and developers. I know that is a well documented feature (in books &amp; the web) but I will give it a shot.</p>
<p>In this post I will show you how collation affects the storage and behavior of your character data.Data is global nowadays.That means that at some point we will deal with databases that have data from different languages.That it why we must understand <strong>collation</strong>. Collation affects various factors.It affects the sort order of the data. It also affects the case and accent sensitivity for the various string comparisons.</p>
<p>We will need an instance of SQL Server running in our machine.You can download and install the free SQL Server Express edition,  <a href="http://www.microsoft.com/sqlserver/en/us/editions/express.aspx" target="_blank">here</a>.</p>
<p>During the installation of an SQL Server instance you are prompted to select the <strong>collation</strong> for the instance.</p>
<p>1) Launch SSMS and connect to an instance of SQL Server.Then select the instance of the SQL Server and select <strong>Properties</strong>.In the <strong>General</strong> tab you will see the collation of the server.Have a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/05/sql-server-collation.jpg"><img class="aligncenter size-full wp-image-1894" title="sql server collation" src="http://dotnetstories.files.wordpress.com/2012/05/sql-server-collation.jpg?w=460&#038;h=327" alt="" width="460" height="327" /></a></p>
<p>2)We can specify the collation at a database level. Select the database and then select <strong>Properties</strong> and then <strong>Options</strong> from the popup window.</p>
<p>In that window we can specify the collation for the database .If not it will keep the same collation as the server.</p>
<p>Have a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/05/database-collation.jpg"><img class="aligncenter size-large wp-image-1897" title="database collation" src="http://dotnetstories.files.wordpress.com/2012/05/database-collation.jpg?w=1024&#038;h=550" alt="" width="1024" height="550" /></a></p>
<p>Please note that we can specify the collation at the column level as well.</p>
<p>You cannot specify collation for variable and parameters.</p>
<p>3) In this first example I will show you how search results are affected in a table that uses the same collation (<strong>SQL_Latin1_General</strong>) but <strong>different case sensitivity</strong> and <strong>accen</strong>t settings. I will use the <strong>TEMPDB</strong> database. In an new query window type and execute the following T-SQL code.</p>
<pre class="brush: sql; title: ; notranslate">

USE tempdb
GO

CREATE TABLE products
(
ID int IDENTITY (1,1),
Product_1 nvarchar(50) COLLATE SQL_Latin1_General_Cp1_CS_AS NULL,
Product_2 nvarchar(50) COLLATE SQL_Latin1_General_Cp1_CI_AS NULL,
Product_3 nvarchar(50) COLLATE SQL_Latin1_General_Cp1_CI_AI NULL
)

INSERT INTO products VALUES ('écrire au crayon','écrire au crayon','écrire au crayon'),
('baseball hat','baseball hat','baseball hat')

-- this will not bring back any results since the collation is case sensitive
SELECT * FROM products WHERE Product_1 ='BASEBALL HAT'

-- this will bring back  results since the collation is case insensitive
SELECT * FROM products WHERE Product_2='BASEBALL HAT'

-- this will bring back  results since the collation is case insensitive
SELECT * FROM products WHERE Product_3='BASEBALL HAT'

-- this will  bring back results since we use case sensitive search
SELECT * FROM products WHERE Product_1 ='baseball hat'

-- this will not bring back any results since the collation is accent sensitive
--note we have ecrire au crayon and not écrire au crayon
SELECT * FROM products WHERE Product_1 ='ecrire au crayon'

--this will fail as well since it is accent sensitive
SELECT * FROM products WHERE Product_2 ='ecrire au crayon'

--this will succeed since it is accent insensitive
SELECT * FROM products WHERE Product_3 ='ecrire au crayon'

--this will succeed since it is accent sensitive
SELECT * FROM products WHERE Product_1 ='écrire au crayon'

</pre>
<p>In this t-sql snippet we create a table that has 3 columns with same collation (<strong>SQL_Latin1_General</strong>) but <strong>different case sensitivity</strong> and <strong>accen</strong>t settings.Then we insert some values. Then I perform some <strong>SELECT</strong> operations and in some cases I am successful and I get back results but in some other cases I am not. Please have a look at the comments in the code.</p>
<p>4) In this example I will show you why we get errors when we join tables with different collations. I will use the <strong>TEMPDB</strong> database. In an new query window type and execute the following T-SQL code.</p>
<pre class="brush: sql; title: ; notranslate">

USE tempdb
GO

CREATE TABLE footballers(
[id] [int] IDENTITY(1,1) NOT NULL,
[firstname] [varchar](50) NOT NULL,
[lastname] [varchar](50) NOT NULL,
[isActive] [bit] NULL,
teamcode[varchar](50) NOT NULL
)

INSERT [dbo].[footballers] ( [firstname], [lastname], [isActive],teamcode) VALUES ( N'Robbie', N'Fowler', 1,'Liv')
INSERT [dbo].[footballers] ( [firstname], [lastname], [isActive],teamcode) VALUES ( N'Steven', N'Gerrard', 1,'Liv')
INSERT [dbo].[footballers] ( [firstname], [lastname], [isActive],teamcode) VALUES ( N'Kenny', N'Dalglish', 0,'Liv')
INSERT [dbo].[footballers] ( [firstname], [lastname], [isActive],teamcode) VALUES ( N'John', N'Terry', 0,'Che')

CREATE TABLE Team(

code [varchar](50) NOT NULL,
name [varchar](50)  NOT NULL
)

INSERT Team(code,name) VALUES ('Liv','Liverpool')
INSERT Team(code,name) VALUES ('Che','Chelsea')

SELECT * FROM footballers JOIN Team
ON footballers.teamcode = team.code

CREATE TABLE GreekTeam
(
code [nvarchar](50) COLLATE Greek_100_BIN
NOT NULL ,
name [nvarchar](50) COLLATE Greek_100_BIN
NOT NULL
)

INSERT GreekTeam(code,name) VALUES (N'ΠΑΟ',N'Παναθηναικός')
INSERT GreekTeam(code,name) VALUES (N'ΟΣΦΠ',N'Ολυμπιακός')

SELECT * FROM GreekTeam

--this will fail

SELECT * FROM footballers JOIN GreekTeam
ON footballers.teamcode = GreekTeam.code
</pre>
<p>I create a table <strong>Footballers</strong> and I insert some data in it. I create a table called <strong>Team</strong> and I insert some data in it. Then I execute a JOIN statement and I get back results.</p>
<p>Then I create another table <strong>GreekTeam</strong> that uses the <strong>Greek_100_BIN</strong> collation.I insert some values in it.Then I try to execute another JOIN statement between <strong>Footballers</strong> table and the <strong>GreekTeam</strong> table. This results in an error, &#8220;Cannot resolve the collation conflict between &#8220;Greek_100_BIN&#8221; and &#8220;Latin1_General_CI_AI&#8221; in the equal to operation.&#8221;</p>
<p>This happens because the two tables do not use the same code page and it can&#8217;t compare the two values because they are stored differently.</p>
<p>5) Let&#8217;s have a look on another example and find out how collation affects the sorting of data.</p>
<p>In a new query window type the following T-SQL statements and execute them</p>
<pre class="brush: sql; title: ; notranslate">

USE TempDB
CREATE TABLE MyTable
(col1 varchar(10) COLLATE Latin1_General_CS_AS NULL,
col2 varchar(10) COLLATE Latin1_General_BIN)
GO

USE TempDB
INSERT MyTable
VALUES ('Alpha','Alpha')
INSERT MyTable
VALUES ('Bravo','Bravo')
INSERT MyTable
VALUES ('Charlie','Charlie')
INSERT MyTable
VALUES ('alpha','alpha')
INSERT MyTable
VALUES ('bravo','bravo')
INSERT MyTable
VALUES ('charlie','charlie')
GO

SELECT col1
FROM MyTable
ORDER BY col1 ASC

SELECT col2
FROM MyTable
ORDER BY col2 ASC

</pre>
<p>We create a table with 2 different columns and different collations.Then we insert some values. Finally we execute 2 select statements.When you execute them you will see that the data is returned back to us from the relational engine of SQL Server with different sort order.</p>
<p>Hope it helps!!!</p>
<br />Filed under: <a href='http://dotnetstories.wordpress.com/category/sql-server/'>Sql Server</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2005/'>Sql Server 2005</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2008/'>SQL Server 2008</a> Tagged: <a href='http://dotnetstories.wordpress.com/tag/collation/'>collation</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dotnetstories.wordpress.com/1893/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dotnetstories.wordpress.com/1893/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=1893&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dotnetstories.wordpress.com/2012/05/02/collation-and-sql-server-data-management/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/ecbfd3584f3b2c0c3f528bacdfc15e0a?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96" medium="image">
			<media:title type="html">fofo</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/05/sql-server-collation.jpg" medium="image">
			<media:title type="html">sql server collation</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/05/database-collation.jpg?w=1024" medium="image">
			<media:title type="html">database collation</media:title>
		</media:content>
	</item>
		<item>
		<title>The importance of filegroups in database performance</title>
		<link>http://dotnetstories.wordpress.com/2012/04/20/the-importance-of-filegroups-in-database-performance/</link>
		<comments>http://dotnetstories.wordpress.com/2012/04/20/the-importance-of-filegroups-in-database-performance/#comments</comments>
		<pubDate>Fri, 20 Apr 2012 18:52:01 +0000</pubDate>
		<dc:creator>fofo</dc:creator>
				<category><![CDATA[Sql Server]]></category>
		<category><![CDATA[Sql Server 2005]]></category>
		<category><![CDATA[SQL Server 2008]]></category>
		<category><![CDATA[filegroups]]></category>

		<guid isPermaLink="false">http://dotnetstories.wordpress.com/?p=1880</guid>
		<description><![CDATA[In this post I would like to explain you with a hands-on example the importance of filegroups in Sql Server databases.

This is not a post that experienced/advnanced DBAS/developers should read. The target audience is beginners (Level 100).

Most beginners in SQL Server  do not understand the concept of filegroups and why we need them.

Ι am going to start with a short introduction on data files,log files.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=1880&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>In this post I would like to explain you with a hands-on example the importance of filegroups in Sql Server databases.</p>
<p>This is not a post that experienced/advnanced DBAS/developers should read. The target audience is beginners (Level 100).</p>
<p>Most beginners in SQL Server  do not understand the concept of filegroups and why we need them.</p>
<p>Ι am going to start with a short introduction on data files,log files.</p>
<p>We all know that our data is stored eventually in operating system files, the same way the data we write in our <strong>Word documents</strong> are stored in <strong>.docx</strong> files in the physical disk.Every SQL Server database has two operating system files types:</p>
<ul>
<li>a data file</li>
<li>a log file</li>
</ul>
<p>A data file contains data and objects such as tables,indexes,stored procedures and views. Every database has one primary data file.The recommended file name extension for primary data file is <strong>.mdf</strong>.We can have secondary data files.The recommended file name extension for secondary data files is <strong>.ndf</strong></p>
<p>The transaction log files hold the log information that is needed to recover the database. There must be at least one log file for each database. The recommended file name extension for transaction logs is <strong>.ldf.</strong></p>
<p><strong>Filegroups</strong> are named collections of files and are used to increase database performance,simplify data placement and administrative tasks such as backup and restore operations.</p>
<p><strong>Filegroups</strong> enable data placement, because we can create a table in a specific filegroup. This improves performance, because all I/O for a specific table can be directed at a specific disk.</p>
<p>If you have tables that are frequently used together, you should put them on separate filegroups and physical drives.A good example could be  to put tables in the same join queries in different filegroups.</p>
<p><strong></strong> If you have a heavily used table, it can be put on one file in one filegroup, located on one disk, and the other less heavily accessed tables in the database can be put on the other files in another filegroup, located on a second disk.You can also put  the non-clustered indexes of a table in one filegroup located on one disk and the table in another filegroup on another disk.</p>
<p>Files cannot be moved to different filegroups and a file cannot be a member of more than one filegroup.We cannot have log files in filegroups.Filegroups can contain only data files.</p>
<p>We will need an instance of SQL Server running in our machine.You can download and install the free SQL Server Express edition,  <a href="http://www.microsoft.com/sqlserver/en/us/editions/express.aspx" target="_blank">here</a>.</p>
<p>Launch SSMS and connect to an instance of SQL Server. In a new query window type the following T-SQL statements and execute them</p>
<pre class="brush: sql; title: ; notranslate">

USE master
GO
CREATE DATABASE TEST
GO

ALTER DATABASE TEST ADD FILEGROUP TEST_DATA_1
GO
ALTER DATABASE TEST ADD FILEGROUP TEST_DATA_2
GO

ALTER DATABASE TEST
ADD FILE
( NAME = TEST1,
FILENAME = 'C:\DATA\TEST_1.ndf',
SIZE = 1MB,
MAXSIZE = 10MB,
FILEGROWTH = 1MB)
TO FILEGROUP TEST_DATA_1
GO

ALTER DATABASE TEST
ADD FILE
( NAME = TEST2,
FILENAME = 'd:\data\TEST_2.ndf',
SIZE = 1MB,
MAXSIZE = 10MB,
FILEGROWTH = 1MB)
TO FILEGROUP Test_DATA_2
GO

USE TEST
GO

CREATE TABLE TABLE1
(
ID INT IDENTITY(1,1),
NAME VARCHAR(100),
CONSTRAINT PK_TAB1 PRIMARY KEY(ID)
) ON TEST_DATA_1

GO

CREATE TABLE TABLE2
(
ID INT IDENTITY(1,1),
NAME VARCHAR(100),
CONSTRAINT PK_TAB2 PRIMARY KEY(ID)
) ON TEST_DATA_2

GO



</pre>
<p>First we create a database that is called <strong>TEST</strong>.Then we create two filegroups (<strong>Test_DATA_1,Test_DATA_2</strong>).Then we add two secondary data files to each of the filegroups.<strong> </strong>Then we have this scenario. We want to create two tables that we know that will be heavily accessed by our application.We want to place them in different filegroups.The last two SQL batches show how to accomplish that.</p>
<p>When I select the <strong>TEST</strong> database, right-click on it,select <strong>Properties</strong> and the <strong>Files</strong> and then <strong>Filegroups</strong> I have a visual representation of the operations I just issued.</p>
<p>Have a look below to see what I mean</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/04/files.jpg"><img class="aligncenter size-large wp-image-1884" title="files" src="http://dotnetstories.files.wordpress.com/2012/04/files.jpg?w=1024&#038;h=550" alt="" width="1024" height="550" /></a></p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/04/filegroups.jpg"><img class="aligncenter size-large wp-image-1885" title="filegroups" src="http://dotnetstories.files.wordpress.com/2012/04/filegroups.jpg?w=1024&#038;h=550" alt="" width="1024" height="550" /></a></p>
<p>&nbsp;</p>
<p>Hope it helps!!!</p>
<br />Filed under: <a href='http://dotnetstories.wordpress.com/category/sql-server/'>Sql Server</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2005/'>Sql Server 2005</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2008/'>SQL Server 2008</a> Tagged: <a href='http://dotnetstories.wordpress.com/tag/filegroups/'>filegroups</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dotnetstories.wordpress.com/1880/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dotnetstories.wordpress.com/1880/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=1880&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dotnetstories.wordpress.com/2012/04/20/the-importance-of-filegroups-in-database-performance/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/ecbfd3584f3b2c0c3f528bacdfc15e0a?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96" medium="image">
			<media:title type="html">fofo</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/04/files.jpg?w=1024" medium="image">
			<media:title type="html">files</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/04/filegroups.jpg?w=1024" medium="image">
			<media:title type="html">filegroups</media:title>
		</media:content>
	</item>
		<item>
		<title>Looking into SPARSE Columns in SQL Server</title>
		<link>http://dotnetstories.wordpress.com/2012/04/19/looking-into-sparse-columns-in-sql-server/</link>
		<comments>http://dotnetstories.wordpress.com/2012/04/19/looking-into-sparse-columns-in-sql-server/#comments</comments>
		<pubDate>Wed, 18 Apr 2012 23:06:55 +0000</pubDate>
		<dc:creator>fofo</dc:creator>
				<category><![CDATA[Sql Server]]></category>
		<category><![CDATA[SQL Server 2008]]></category>
		<category><![CDATA[sparse columns]]></category>

		<guid isPermaLink="false">http://dotnetstories.wordpress.com/?p=1875</guid>
		<description><![CDATA[In this rather short post I would like to provide you with a hands-on example on SPARSE columns. Sparse columns are very useful when we have tables and columns with many zero and NULL values.This feature was introduced in SQL Server 2008.We have a better more efficient way to manage empty space through SPARSE columns by enabling NULL data to consume no space at all. <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=1875&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>In this rather short post I would like to provide you with a hands-on example on <strong>SPARSE</strong> columns. <strong>SPARSE</strong> columns are very useful when we have tables and columns with many <strong>zero</strong> and <strong>NULL</strong> values.This feature was introduced in SQL Server 2008.We have a better, more efficient way to manage empty space through <strong>SPARSE</strong> columns by enabling <strong>NULL</strong> data to consume no space at all.</p>
<p>We will need an instance of SQL Server running in our machine.You can download and install the free SQL Server Express edition,  <a href="http://www.microsoft.com/sqlserver/en/us/editions/express.aspx" target="_blank">here</a>.</p>
<p>Launch SSMS and connect to an instance of SQL Server. In a new query window type the following T-SQL statements and execute them</p>
<pre class="brush: sql; title: ; notranslate">

USE tempdb
GO

CREATE TABLE tblUnsparsed
(
ID INT IDENTITY(1, 1) ,
OrderID  INT ,
Size VARCHAR(100) ,
OrderDate DATETIME
)
GO

CREATE TABLE tblSparsed
(
ID INT IDENTITY(1, 1) ,
OrderID  INT SPARSE ,
Size VARCHAR(100) SPARSE ,
OrderDate DATETIME SPARSE
)
GO

INSERT INTO tblUnsparsed VALUES (NULL,NULL, NULL)
GO 200000

INSERT INTO tblSparsed VALUES (NULL, NULL, NULL)
GO 200000

SELECT * FROM tblUnsparsed

SELECT * FROM tblSparsed

sp_spaceused 'tblUnsparsed'
GO
sp_spaceused 'tblSparsed'
GO

DROP TABLE tblSparsed
GO
DROP TABLE tblUnsparsed
GO

</pre>
<p>I create two tables.The second table has the SPARSE statement in the column definition.I insert 200.000 rows of NULL values in both of the tables.Then I check to see if the rows were inserted.200.000 rows were inserted in both tables.Then I use the <strong>sp_spaceused</strong> system stored procedure to find out how much space was consumed.</p>
<p>You will see a big difference when SQL Server returns the results.</p>
<p>Have a look in the picture below to see what I got</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/04/sparsed.jpg"><img class="aligncenter size-large wp-image-1876" title="sparsed" src="http://dotnetstories.files.wordpress.com/2012/04/sparsed.jpg?w=1024&#038;h=550" alt="" width="1024" height="550" /></a></p>
<p>We cannot apply the SPARSE statement to columns where their data types are :image,text,geography,geometry.We cannot apply rules or defaults to <strong>SPARSE</strong> columns. We cannot have Data Compression and Merge replication does not work either.In case the <strong>SPARSE</strong> columns have data in them they will take 4 more bytes than the normal column. So please be careful when you use <strong>SPARSE</strong> columns.</p>
<p>Hope it helps!!!!</p>
<br />Filed under: <a href='http://dotnetstories.wordpress.com/category/sql-server/'>Sql Server</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2008/'>SQL Server 2008</a> Tagged: <a href='http://dotnetstories.wordpress.com/tag/sparse-columns/'>sparse columns</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dotnetstories.wordpress.com/1875/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dotnetstories.wordpress.com/1875/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=1875&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dotnetstories.wordpress.com/2012/04/19/looking-into-sparse-columns-in-sql-server/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/ecbfd3584f3b2c0c3f528bacdfc15e0a?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96" medium="image">
			<media:title type="html">fofo</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/04/sparsed.jpg?w=1024" medium="image">
			<media:title type="html">sparsed</media:title>
		</media:content>
	</item>
		<item>
		<title>Looking into the various ways you can find out the Recovery model of databases in an SQL Server instance</title>
		<link>http://dotnetstories.wordpress.com/2012/04/18/looking-into-the-various-ways-you-can-find-out-the-recovery-model-of-databases-in-an-sql-server-instance/</link>
		<comments>http://dotnetstories.wordpress.com/2012/04/18/looking-into-the-various-ways-you-can-find-out-the-recovery-model-of-databases-in-an-sql-server-instance/#comments</comments>
		<pubDate>Wed, 18 Apr 2012 14:47:57 +0000</pubDate>
		<dc:creator>fofo</dc:creator>
				<category><![CDATA[Sql Server]]></category>
		<category><![CDATA[Sql Server 2005]]></category>
		<category><![CDATA[SQL Server 2008]]></category>
		<category><![CDATA[recovery model]]></category>

		<guid isPermaLink="false">http://dotnetstories.wordpress.com/?p=1867</guid>
		<description><![CDATA[This is going to be a rather short post. I have been teaching the Official Microsoft SQL Server courses in my capacity as MCT and to be totally honest with you I enjoy it very much.One of my favorite subject is Backup &#38; Restore. This is maybe the main DBA task.There is no way to explain adequately Backup &#38; Restore without talking about Recovery models.Well some people in the class did not know how to find the recovery model for each database in the server. Another common question is how to find out the recovery models for all databases in one go.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=1867&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>This is going to be a rather short post. I have been teaching the Official Microsoft SQL Server courses in my capacity as MCT and to be totally honest with you I enjoy it very much.One of my favorite subject is <strong>Backup &amp; Restore</strong>. This is maybe the main DBA task.There is no way to explain adequately <strong>Backup &amp; Restore</strong> without talking about <strong>Recovery models</strong>.Well some people in the class did not know how to find the recovery model for each database in the server. Another common question is how to find out the recovery models for all databases at once.</p>
<p>I will show you the various ways I know on how to accomplish that.</p>
<p>We will need an instance of SQL Server running in our machine.You can download and install the free SQL Server Express edition,  <a href="http://www.microsoft.com/sqlserver/en/us/editions/express.aspx" target="_blank">here</a>.</p>
<ul>
<li>This is the obvious way (most people should know that even if they are beginners in SQL Server).</li>
</ul>
<p>I choose the database, in this case <strong>AdventureWorks2008R2</strong>, select <strong>Properties</strong>, then select <strong>Options</strong>.In this case I see that the recovery model is <strong>FULL</strong></p>
<p>Have a look at the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/04/recovery-1.jpg"><img class="aligncenter size-large wp-image-1868" title="recovery-1" src="http://dotnetstories.files.wordpress.com/2012/04/recovery-1.jpg?w=1024&#038;h=550" alt="" width="1024" height="550" /></a></p>
<ul>
<li>There is a way to find out the recovery model for all databases from SSMS. Select the Databases node.From the menu select <strong>View</strong> and then <strong>Object Explorer Details</strong>. Have a look in the picture below to see the output.You must select from the available columns to show the <strong>Recovery Model </strong>column<strong><br />
</strong></li>
</ul>
<p><a href="http://dotnetstories.files.wordpress.com/2012/04/recovery-2.jpg"><img class="aligncenter size-large wp-image-1869" title="recovery-2" src="http://dotnetstories.files.wordpress.com/2012/04/recovery-2.jpg?w=1024&#038;h=550" alt="" width="1024" height="550" /></a></p>
<ul>
<li>We can find out the Recovery Model for each database in the SQL Server using. In a query window type and then execute the following T-SQL code</li>
</ul>
<pre class="brush: sql; title: ; notranslate">

USE master
GO

SELECT name AS [DBName],
recovery_model_desc AS [Recovery_Model]
FROM sys.databases
GO

</pre>
<ul>
<li>We can find out the recovery model for a specific database by typing and executing the following T-SQL statements in a new query window</li>
</ul>
<pre class="brush: sql; title: ; notranslate">

USE master
GO

SELECT 'AdventureWorks2008R2' AS [DBName],
DATABASEPROPERTYEX('AdventureWorks2008R2', 'Recovery')
AS [Recovery_Model]
GO

</pre>
<p>Hope it helps!!!</p>
<br />Filed under: <a href='http://dotnetstories.wordpress.com/category/sql-server/'>Sql Server</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2005/'>Sql Server 2005</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2008/'>SQL Server 2008</a> Tagged: <a href='http://dotnetstories.wordpress.com/tag/recovery-model/'>recovery model</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dotnetstories.wordpress.com/1867/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dotnetstories.wordpress.com/1867/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=1867&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dotnetstories.wordpress.com/2012/04/18/looking-into-the-various-ways-you-can-find-out-the-recovery-model-of-databases-in-an-sql-server-instance/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/ecbfd3584f3b2c0c3f528bacdfc15e0a?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96" medium="image">
			<media:title type="html">fofo</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/04/recovery-1.jpg?w=1024" medium="image">
			<media:title type="html">recovery-1</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/04/recovery-2.jpg?w=1024" medium="image">
			<media:title type="html">recovery-2</media:title>
		</media:content>
	</item>
		<item>
		<title>Looking into Nested Transactions and Rollback operations</title>
		<link>http://dotnetstories.wordpress.com/2012/04/18/looking-into-nested-transactions-and-rollback-operations/</link>
		<comments>http://dotnetstories.wordpress.com/2012/04/18/looking-into-nested-transactions-and-rollback-operations/#comments</comments>
		<pubDate>Tue, 17 Apr 2012 23:45:15 +0000</pubDate>
		<dc:creator>fofo</dc:creator>
				<category><![CDATA[Sql Server]]></category>
		<category><![CDATA[Sql Server 2005]]></category>
		<category><![CDATA[SQL Server 2008]]></category>
		<category><![CDATA[rollback transaction]]></category>
		<category><![CDATA[savepoint]]></category>
		<category><![CDATA[transactions]]></category>

		<guid isPermaLink="false">http://dotnetstories.wordpress.com/?p=1857</guid>
		<description><![CDATA[In this post I would like to show you what happens when we issue ROLLBACK operations in nested transactions

Have a look in this post of mine to find out more information about transactions.A lot of people are confused on how a Rollback statement impacts a nested transaction.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=1857&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>In this post I would like to show you what happens when we issue <strong>ROLLBACK</strong> operations in nested transactions.</p>
<p>Have a look in this <a href="http://dotnetstories.wordpress.com/2011/11/09/a-thorough-look-into-transactions-in-sql-server/">post</a> of mine to find out more information about transactions.A lot of people are confused on how a <strong>Rollback</strong> statement impacts a nested transaction.</p>
<p>I will move on with my hands-on examples.I will use the<strong> tempdb</strong> database.</p>
<p>We will need an instance of SQL Server running in our machine.You can download and install the free SQL Server Express edition from <a href="http://www.microsoft.com/sqlserver/en/us/editions/express.aspx" target="_blank">here</a>.</p>
<p>Launch SSMS and connect to an instance of SQL Server.In a new query window type</p>
<pre class="brush: sql; title: ; notranslate">

USE TempDB
GO
CREATE TABLE TranTable (ID INT, name VARCHAR(100))
GO

SELECT ID, name
FROM TranTable;

BEGIN TRANSACTION
SELECT @@TRANCOUNT tranCount;
INSERT INTO TranTable (ID,  name)
VALUES (1, 'Nick');

SELECT ID, name
FROM TranTable;

BEGIN TRANSACTION
SELECT @@TRANCOUNT tranCount;
INSERT INTO TranTable (ID, name)
VALUES (2, 'George');

SELECT ID, name
FROM TranTable;

--we issue a rollback

ROLLBACK

--we have no values in the table

SELECT ID, name
FROM TranTable;

--this is 0
--nested transaction rollsback to the outmost transaction

SELECT @@TRANCOUNT tranCount;

DROP TABLE TranTable
GO

</pre>
<p>I am creating a table and I insert some values in the table by starting explicitly a transaction. At the same time I query the <strong>@@Trancount</strong> global variable.It basically returns the number of  <strong>BEGIN TRANSACTION</strong> statements that have occurred on the current connection.Then I do the same in a second explicit transaction (lines 17-20).<strong>@@Trancount is 2</strong> at this point.Then we issue a <strong>Rollback</strong>statement. Then  we issue a &#8220;SELECT ID, name FROM TranTable;&#8221;. We notice that there is no data in the table and the <strong>@Trancount is 0</strong> at this point.This means that a<strong> Rollback</strong> statement rollsback to the outmost transaction that was defined. I want to show a similar example where I use the exact same T-SQL as before but I <strong>commit</strong> the transaction before I issue a <strong>rollback</strong> statement .In a new query window type</p>
<pre class="brush: sql; title: ; notranslate">

USE TempDB
GO
CREATE TABLE TranTable (ID INT, name VARCHAR(100))
GO

SELECT ID, name
FROM TranTable;

BEGIN TRANSACTION
SELECT @@TRANCOUNT tranCount;
INSERT INTO TranTable (ID,  name)
VALUES (1, 'Nick');

SELECT ID, name
FROM TranTable;

BEGIN TRANSACTION
SELECT @@TRANCOUNT tranCount;
INSERT INTO TranTable (ID, name)
VALUES (2, 'George');

COMMIT TRANSACTION

SELECT ID, name
FROM TranTable;

--we issue a rollback

ROLLBACK

--we have no values in the table

SELECT ID, name
FROM TranTable;

--this is 0
--nested transaction rollsback to the outmost transaction

SELECT @@TRANCOUNT tranCount;

DROP TABLE TranTable
GO

</pre>
<p>Execute lines 1-20.Then execute lines 22-25. We commit the transaction and then we check to see if the 2 rows reside on the table. <strong>@@Trancount is 2</strong> at this point.Then we issue a<strong> rollback</strong> statement.Then we issue a &#8220;SELECT ID, name FROM TranTable;&#8221;. We notice that there is no data in the table and the <strong>@Trancount is 0</strong> at this point.This means that a<strong> Rollback</strong> statement rollsback again to the outmost transaction that was defined.</p>
<p>In this last example I will demonstrate how we can avoid rolling back to the outmost transaction by using savepoints.</p>
<p>In a new query window type</p>
<pre class="brush: sql; title: ; notranslate">

USE TempDB
GO
CREATE TABLE TranTable (ID INT, name VARCHAR(100))
GO

SELECT ID, name
FROM TranTable;

BEGIN TRANSACTION Tran1
SAVE TRANSACTION Tran1
SELECT @@TRANCOUNT tranCount;
INSERT INTO TranTable (ID,  name)
VALUES (1, 'Nick');

SELECT ID, name
FROM TranTable;

BEGIN TRANSACTION Tran2
SAVE TRANSACTION Tran2
SELECT @@TRANCOUNT tranCount;
INSERT INTO TranTable (ID, name)
VALUES (2, 'George');

SELECT ID, name
FROM TranTable;

--we issue a rollback to a savepoint

ROLLBACK TRANSACTION Tran2

--we have one row in the table

SELECT ID, name
FROM TranTable;

--@@TRANCOUNT  is 2

SELECT @@TRANCOUNT tranCount;

ROLLBACK TRANSACTION Tran1

SELECT ID, name
FROM TranTable;

--@@TRANCOUNT  is 2

SELECT @@TRANCOUNT tranCount;

ROLLBACK

--@@TRANCOUNT  is 0

SELECT @@TRANCOUNT tranCount;

DROP TABLE TranTable
GO

</pre>
<p>Execute lines <strong>1-22</strong>. We issue the same statements as before but as you can clearly see we save the first and second explicit transactions (Tran 1, Tran2).We then confirm that we have 2 rows in the table.Then we issue a rollback statement to the specific savepoint in line <strong>30</strong>.Then we see that we have the first row still in the table by executing the lines <strong>34-35</strong>.The <strong>@@TRANCOUNT</strong> is 2 at this point. Then we issue a rollback statement to a specific savepoint in line <strong>42</strong>.Then we see that there are no rows in the table by executing the lines <strong>42-43</strong>.The <strong>@@TRANCOUNT</strong> is 2 at this point.Then we issue a ROLLBACK statement. The <strong>@@TRANCOUNT</strong> is 0 at this point.This is the only way (savepoints) to return to a point within our transaction when issuing <strong>Rollback</strong> statements to those savepoints  .</p>
<p>Hope it helps!!!</p>
<br />Filed under: <a href='http://dotnetstories.wordpress.com/category/sql-server/'>Sql Server</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2005/'>Sql Server 2005</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2008/'>SQL Server 2008</a> Tagged: <a href='http://dotnetstories.wordpress.com/tag/rollback-transaction/'>rollback transaction</a>, <a href='http://dotnetstories.wordpress.com/tag/savepoint/'>savepoint</a>, <a href='http://dotnetstories.wordpress.com/tag/transactions/'>transactions</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dotnetstories.wordpress.com/1857/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dotnetstories.wordpress.com/1857/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=1857&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dotnetstories.wordpress.com/2012/04/18/looking-into-nested-transactions-and-rollback-operations/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/ecbfd3584f3b2c0c3f528bacdfc15e0a?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96" medium="image">
			<media:title type="html">fofo</media:title>
		</media:content>
	</item>
		<item>
		<title>Looking into DENY and GRANT security permissions</title>
		<link>http://dotnetstories.wordpress.com/2012/04/13/looking-into-deny-and-grant-security-permissions/</link>
		<comments>http://dotnetstories.wordpress.com/2012/04/13/looking-into-deny-and-grant-security-permissions/#comments</comments>
		<pubDate>Fri, 13 Apr 2012 16:49:04 +0000</pubDate>
		<dc:creator>fofo</dc:creator>
				<category><![CDATA[Sql Server]]></category>
		<category><![CDATA[Sql Server 2005]]></category>
		<category><![CDATA[SQL Server 2008]]></category>
		<category><![CDATA[DENY]]></category>
		<category><![CDATA[GRANT]]></category>
		<category><![CDATA[REVERT]]></category>
		<category><![CDATA[REVOKE]]></category>

		<guid isPermaLink="false">http://dotnetstories.wordpress.com/?p=1850</guid>
		<description><![CDATA[In this post I would like to talk about DENY and GRANT security permissions. I will provide some examples that will demonstrate that DENY takes precedence over GRANT but there are some special cases.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=1850&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>In this post I would like to talk about DENY and GRANT security permissions. I will provide some examples that will demonstrate that DENY takes precedence over GRANT but there are some special cases.</p>
<p>We use DENY and GRANT  T-Sql commands to give permissions on a securable (e.g table) to a principal (a custom user we have created).Most developers are familiar with their use but there are some special cases I would like to point out.</p>
<p>We will need an instance of SQL Server running in our machine.You can download and install the free SQL Server Express edition from <a href="http://www.microsoft.com/sqlserver/en/us/editions/express.aspx" target="_blank">here</a>.</p>
<p>Launch <strong>SQL Server</strong> and in a new query window type the following</p>
<pre class="brush: sql; title: ; notranslate">

USE master
GO

CREATE DATABASE TestDB
GO

USE TestDB
GO

CREATE USER John WITHOUT LOGIN;
GO

CREATE TABLE Person (ID int, name VARCHAR(50), lastname VARCHAR(50))
GO

INSERT INTO Person VALUES (1,'george','Harisson'), (2,'Jill','Diamond'), (3,'Jane','Littlewood')
GO

SELECT * FROM Person

GRANT SELECT ON Person to PUBLIC

DENY SELECT (lastname) ON Person TO john
GO

EXECUTE AS user='John'

--this will fail
SELECT * FROM Person
GO
--this will work
SELECT ID, name FROM Person
GO

REVERT

GO

REVOKE SELECT (lastname) ON Person TO john

</pre>
<p>We create a sample database, a new user <strong>John</strong>  and a new table, <strong>Person</strong>.Then we insert some values to the <strong>Person</strong> table.Then we deny (DENY) the SELECT operation for the <strong>lastname</strong> column of the <strong>Person</strong> table to user <strong>John</strong>.Then we change the user to <strong>John</strong>. Then we do a <strong>Select * from Person</strong> , which fails but the <strong>Select ID,name from Person</strong> operation succeeds for the user <strong>John</strong>.User <strong>John</strong> has no <strong>DENY</strong> permissions for the <strong>ID</strong> and <strong>name</strong> columns so the <strong>SELECT</strong> operation succeeds.Then we do a <strong>REVERT</strong> operation and <strong>REVOKE</strong> the <strong>SELECT</strong> <strong>lastname</strong> permission on table <strong>Person</strong> to user <strong>John.</strong></p>
<p>Let&#8217;s have a look on a second example</p>
<p>In the same query window type</p>
<pre class="brush: sql; title: ; notranslate">

DENY SELECT ON Person TO john

GRANT SELECT (name) ON Person TO john
GO

EXECUTE AS user='john'
--this will fail
SELECT * FROM Person
--this will succeed
SELECT name FROM Person
REVERT

USE master
GO
DROP DATABASE TestDB

</pre>
<p>We issue a <strong>DENY</strong> statement for a <strong>SELECT</strong> operation on the <strong>Person </strong>table for the <strong>John</strong> user.Then we issue a <strong>GRANT</strong> statement for a <strong>SELECT</strong> operation on the <strong>name</strong> column of the <strong>Person</strong> table.Then we change the user to <strong>John</strong>.Then we do a <strong>Select * from Person</strong>, which fails but the <strong>Select name from Person</strong> operation succeeds for the user <strong>John</strong>.So in this case we see that a <strong>GRANT</strong> statement takes precedence over <strong>DENY</strong> when it comes to column-level <strong>GRANT</strong> permissions.</p>
<p>Finally we issue a <strong>REVERT</strong> operation to revert to the <strong>dbo</strong> user and then we drop the <strong>TestDB</strong> database.</p>
<p>Hope it helps!!!</p>
<br />Filed under: <a href='http://dotnetstories.wordpress.com/category/sql-server/'>Sql Server</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2005/'>Sql Server 2005</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2008/'>SQL Server 2008</a> Tagged: <a href='http://dotnetstories.wordpress.com/tag/deny/'>DENY</a>, <a href='http://dotnetstories.wordpress.com/tag/grant/'>GRANT</a>, <a href='http://dotnetstories.wordpress.com/tag/revert/'>REVERT</a>, <a href='http://dotnetstories.wordpress.com/tag/revoke/'>REVOKE</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dotnetstories.wordpress.com/1850/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dotnetstories.wordpress.com/1850/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=1850&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dotnetstories.wordpress.com/2012/04/13/looking-into-deny-and-grant-security-permissions/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/ecbfd3584f3b2c0c3f528bacdfc15e0a?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96" medium="image">
			<media:title type="html">fofo</media:title>
		</media:content>
	</item>
		<item>
		<title>Performance problems issued by duplicate indexes</title>
		<link>http://dotnetstories.wordpress.com/2012/04/10/performance-problems-because-of-duplicate-indexes/</link>
		<comments>http://dotnetstories.wordpress.com/2012/04/10/performance-problems-because-of-duplicate-indexes/#comments</comments>
		<pubDate>Tue, 10 Apr 2012 11:05:02 +0000</pubDate>
		<dc:creator>fofo</dc:creator>
				<category><![CDATA[general .net]]></category>
		<category><![CDATA[Sql Server]]></category>
		<category><![CDATA[Sql Server 2005]]></category>
		<category><![CDATA[SQL Server 2008]]></category>
		<category><![CDATA[duplicate indexes]]></category>

		<guid isPermaLink="false">http://dotnetstories.wordpress.com/?p=1831</guid>
		<description><![CDATA[In this post I would like to show you with a hands-on example why it is a bad idea to have duplicate indexes on SQL Server tables.

Some people believe that having duplicate indexes will improve the performance. There will be no performance gain for SELECT statements and there will be a performance degradation for INSERT,UPDATE,DELETE statements.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=1831&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>In this post I would like to show you with a hands-on example why it is a bad idea to have duplicate indexes on SQL Server tables.</p>
<p>Some people believe that having duplicate indexes will improve the performance. There will be no performance gain for <strong>SELECT</strong> statements and there will be a performance degradation for <strong>INSERT,UPDATE,DELETE</strong> statements.</p>
<p>Have a look in this <a href="../2012/04/07/the-dangers-of-over-indexing-your-sql-server-tables/">post</a> if you want to learn about the possible dangers of over indexing your tables.</p>
<p>We will need a database to work with. I will use <strong>AdventureWorks2008R2</strong>. You can download it <a href="http://msftdbprodsamples.codeplex.com/releases/view/55926" target="_blank">here</a> .</p>
<p>We will need an instance of SQL Server running in our machine.You can download and install the free SQL Server Express edition from <a href="http://www.microsoft.com/sqlserver/en/us/editions/express.aspx" target="_blank">here</a>.</p>
<p>I will create a new empty table in the <strong>AdventureWorks2008R2</strong> .I will call it <strong>NewPerson</strong> and it will be based on the <strong>Person</strong> table and I will insert the same data that resides in the <strong>Person</strong> table to the <strong>NewPerson</strong> tables.There will be no indexes on the <strong>NewPerson</strong> table.I will use <strong>STATISTICS TIME ON</strong>,<strong>STATISTICS IO ON</strong> to measure the time needed to insert the rows to the table together with the IO operations.Then I will truncate the table. I will create multiple duplicate indexes on the <strong>FirstName</strong> column and I will insert the same data again in the table. I will then compare the time needed to insert rows in the table without any indexes and a table with multiple duplicate indexes.</p>
<p>Launch SQL Server Management Studio and log in the instance of SQL Server that <strong>AdventureWorks2008R2</strong> is attached. In an new query window type</p>
<pre class="brush: sql; title: ; notranslate">

USE [AdventureWorks2008R2]
GO

SET NOCOUNT ON

SELECT *
INTO [Person].[NewPerson]
FROM [Person].[Person]
WHERE 3 = 4
GO

SET STATISTICS TIME ON
SET STATISTICS IO ON
GO

INSERT INTO [AdventureWorks2008R2].[Person].[NewPerson]
([BusinessEntityID]
,[PersonType]
,[NameStyle]
,[Title]
,[FirstName]
,[MiddleName]
,[LastName]
,[Suffix]
,[EmailPromotion]
,[AdditionalContactInfo]
,[Demographics]
,[rowguid]
,[ModifiedDate])
SELECT    [BusinessEntityID]
,[PersonType]
,[NameStyle]
,[Title]
,[FirstName]
,[MiddleName]
,[LastName]
,[Suffix]
,[EmailPromotion]
,[AdditionalContactInfo]
,[Demographics]
,[rowguid]
,[ModifiedDate]
FROM [Person].[Person]
GO

SELECT * FROM Person.NewPerson

</pre>
<p>Have a look at the statistics output. In my case I got the results shown in the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/04/statistics-time.jpg"><img class="aligncenter size-large wp-image-1836" title="statistics-time" src="http://dotnetstories.files.wordpress.com/2012/04/statistics-time.jpg?w=1024&#038;h=550" alt="" width="1024" height="550" /></a></p>
<p>Now I will truncate the table and I will create non-clustered duplicate indexes on the <strong>NewPerson</strong> table for the <strong>FirstName</strong> column.Then I will insert the same data again and make a note of the statistics again.Type the following in the same query window.</p>
<pre class="brush: sql; title: ; notranslate">

TRUNCATE TABLE [Person].[NewPerson]
GO

CREATE NONCLUSTERED INDEX [IX_FirstName]
ON [Person].[NewPerson]
([FirstName] ASC) ON [PRIMARY]
GO

CREATE NONCLUSTERED INDEX [IX_FirstName1]
ON [Person].[NewPerson]
([FirstName] ASC) ON [PRIMARY]
GO

CREATE NONCLUSTERED INDEX [IX_FirstName2]
ON [Person].[NewPerson]
([FirstName] ASC) ON [PRIMARY]
GO

CREATE NONCLUSTERED INDEX [IX_FirstName3]
ON [Person].[NewPerson]
([FirstName] ASC) ON [PRIMARY]
GO

CREATE NONCLUSTERED INDEX [IX_FirstName4]
ON [Person].[NewPerson]
([FirstName] ASC) ON [PRIMARY]
GO

CREATE NONCLUSTERED INDEX [IX_FirstName5]
ON [Person].[NewPerson]
([FirstName] ASC) ON [PRIMARY]
GO

INSERT INTO [AdventureWorks2008R2].[Person].[NewPerson]
([BusinessEntityID]
,[PersonType]
,[NameStyle]
,[Title]
,[FirstName]
,[MiddleName]
,[LastName]
,[Suffix]
,[EmailPromotion]
,[AdditionalContactInfo]
,[Demographics]
,[rowguid]
,[ModifiedDate])
SELECT    [BusinessEntityID]
,[PersonType]
,[NameStyle]
,[Title]
,[FirstName]
,[MiddleName]
,[LastName]
,[Suffix]
,[EmailPromotion]
,[AdditionalContactInfo]
,[Demographics]
,[rowguid]
,[ModifiedDate]
FROM [Person].[Person]
GO
</pre>
<p>Have a look again at the statistics output. In my case I got the results shown in the picture below.<br />
They are significantly greater (140% or higher).</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/04/statistics-time-2.jpg"><img class="aligncenter size-large wp-image-1837" title="statistics-time-2" src="http://dotnetstories.files.wordpress.com/2012/04/statistics-time-2.jpg?w=1024&#038;h=550" alt="" width="1024" height="550" /></a></p>
<p>The <strong>NewPerson</strong> table has <strong>19972</strong> rows only. If it had 2-3 million rows the elapsed time in the second case it would be 20(2000%) times greater.In any case drop duplicate indexes.</p>
<p>Hope it helps!!!!</p>
<br />Filed under: <a href='http://dotnetstories.wordpress.com/category/general-net/'>general .net</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server/'>Sql Server</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2005/'>Sql Server 2005</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2008/'>SQL Server 2008</a> Tagged: <a href='http://dotnetstories.wordpress.com/tag/duplicate-indexes/'>duplicate indexes</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dotnetstories.wordpress.com/1831/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dotnetstories.wordpress.com/1831/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=1831&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dotnetstories.wordpress.com/2012/04/10/performance-problems-because-of-duplicate-indexes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/ecbfd3584f3b2c0c3f528bacdfc15e0a?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96" medium="image">
			<media:title type="html">fofo</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/04/statistics-time.jpg?w=1024" medium="image">
			<media:title type="html">statistics-time</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/04/statistics-time-2.jpg?w=1024" medium="image">
			<media:title type="html">statistics-time-2</media:title>
		</media:content>
	</item>
		<item>
		<title>Applying UNIQUE indexes on columns with Multiple NULLS</title>
		<link>http://dotnetstories.wordpress.com/2012/04/07/applying-unique-indexes-on-columns-with-multiple-nulls/</link>
		<comments>http://dotnetstories.wordpress.com/2012/04/07/applying-unique-indexes-on-columns-with-multiple-nulls/#comments</comments>
		<pubDate>Sat, 07 Apr 2012 15:41:25 +0000</pubDate>
		<dc:creator>fofo</dc:creator>
				<category><![CDATA[SQL Server 2008]]></category>
		<category><![CDATA[filtered indexes]]></category>
		<category><![CDATA[unique indexes]]></category>

		<guid isPermaLink="false">http://dotnetstories.wordpress.com/?p=1823</guid>
		<description><![CDATA[In this demo I would like to show you with a hands-on example how to apply unique  indexes on column that contains multiple NULL values.

Some people believe that this is not possible and think that if you want to enforce a unique index on a column, that column must have only one NULL value.I will show you that with the help of filtered indexes we can have unique indexes on columns with multiple NULL values.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=1823&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>In this demo I would like to show you with a hands-on example how to apply unique  indexes on column that contains multiple <strong>NULL</strong> values.</p>
<p>Some people believe that this is not possible and think that if you want to enforce a unique index on a column, that column must have only one NULL value.I will show you that with the help of <strong>filtered indexes</strong> we can have <strong>unique indexes</strong> on columns with multiple NULL values.</p>
<p>Have a look in this <a href="http://dotnetstories.wordpress.com/2012/04/07/the-dangers-of-over-indexing-your-sql-server-tables/">post</a> if you want to learn about the possible dangers of over indexing your tables.</p>
<p>We will need an instance of SQL Server running in our machine.You can download and install the free <strong>SQL Server Express</strong> edition from <a href="http://www.microsoft.com/sqlserver/en/us/editions/express.aspx" target="_blank">here</a>.</p>
<p>In this example I will use <strong>tempdb</strong>.Launch SQL Server Management Studio and log in  SQL Server. In an new query window type</p>
<pre class="brush: sql; title: ; notranslate">
USE tempdb
 GO

CREATE TABLE MyTable
 (
 id INT IDENTITY(1, 1)
 PRIMARY KEY ,
 team VARCHAR(15)
 )

INSERT  MyTable
 VALUES  ( 'Liverpool' ),
 ( 'Barca' ),
 ( 'Real' ),
 ( 'Man Utd' )

SELECT * FROM MyTable

CREATE UNIQUE INDEX UNQ_team_MyTable ON MyTable(team)
 GO

INSERT MyTable Values (NULL)
 GO

Select * from MyTable

--this will create an error
 INSERT MyTable Values (NULL)
 GO

</pre>
<p>I create a table and I insert some values. Then I create a unique index on a column and insert a NULL value in it. So far so good. If I insert a second NULL value in the same table I get an error.In the same query window type</p>
<pre class="brush: sql; title: ; notranslate">

DROP INDEX  MyTable.UNQ_team_MyTable
 GO

--filtered index
 CREATE UNIQUE INDEX UNQ_team_MyTable ON MyTable(team)
 WHERE team IS NOT NULL
 GO
 --this will not fail
 INSERT mytable Values (NULL)
 GO
 --this will not fail
 INSERT mytable Values (NULL)
 GO
 --this will not fail
 INSERT mytable Values (NULL)
 GO
 --this will not fail
 INSERT mytable Values (NULL)
 GO

Select * from mytable
 GO

--this will fail
 INSERT mytable
 VALUES ('Barca')

--this will succeed

INSERT mytable Values (NULL)
 GO

</pre>
<p>I drop the index and create a <strong>filtered index</strong> on the column where the value is different than NULL.Then I insert multiple <strong>NULL</strong> values in the <strong>team</strong> column with no problem at all. If I try to insert a value in the <strong>team</strong> column of the table that already exists I will get an error from SQL Server. Note that <strong>filtered indexes</strong> are introduced in SQL Server 2008.Filters can only be applied to non-clustered indexes.</p>
<p>Hope it helps!!!!</p>
<br />Filed under: <a href='http://dotnetstories.wordpress.com/category/sql-server-2008/'>SQL Server 2008</a> Tagged: <a href='http://dotnetstories.wordpress.com/tag/filtered-indexes/'>filtered indexes</a>, <a href='http://dotnetstories.wordpress.com/tag/unique-indexes/'>unique indexes</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dotnetstories.wordpress.com/1823/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dotnetstories.wordpress.com/1823/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=1823&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dotnetstories.wordpress.com/2012/04/07/applying-unique-indexes-on-columns-with-multiple-nulls/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/ecbfd3584f3b2c0c3f528bacdfc15e0a?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96" medium="image">
			<media:title type="html">fofo</media:title>
		</media:content>
	</item>
		<item>
		<title>The dangers of over indexing your SQL Server tables</title>
		<link>http://dotnetstories.wordpress.com/2012/04/07/the-dangers-of-over-indexing-your-sql-server-tables/</link>
		<comments>http://dotnetstories.wordpress.com/2012/04/07/the-dangers-of-over-indexing-your-sql-server-tables/#comments</comments>
		<pubDate>Fri, 06 Apr 2012 21:24:24 +0000</pubDate>
		<dc:creator>fofo</dc:creator>
				<category><![CDATA[Sql Server]]></category>
		<category><![CDATA[Sql Server 2005]]></category>
		<category><![CDATA[SQL Server 2008]]></category>
		<category><![CDATA[Indexes]]></category>

		<guid isPermaLink="false">http://dotnetstories.wordpress.com/?p=1813</guid>
		<description><![CDATA[I have been teaching SQL Server recently in my capacity as MCT and to be totally honest with you I enjoy it very much.The topic that kept popping up all the time was indexes and how to use indexes to achieve high performance in SQL Server .Indexes is a vast subject and well documented elsewhere on the web.In most cases the answer is "it depends" when someone asks me if a column in a particular tables needs to be indexed.<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=1813&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></description>
				<content:encoded><![CDATA[<p>I have been teaching SQL Server recently in my capacity as MCT and to be totally honest with you I enjoy it very much.The topic that kept popping up all the time was <strong>indexes</strong> and how to use indexes to achieve high performance in SQL Server .<strong>Indexes</strong> is a vast subject and well documented elsewhere on the web.In most cases the answer is &#8220;it depends&#8221; when someone asks me if a column in a particular table needs to be indexed. It is a very challenging and iterative process.Not only you need to create the right indexes but also you need to analyse them and maintain them.In most cases the customer &#8220;lies&#8221; about the database access patterns and the numbers of users that will be using the database .Because of that we might choose the wrong indexes because we have made the wrong assumptions about workloads.We must always bear in mind the type and frequency of the queries.Data modifications (INSERT,UPDATE,DELETE statements ) are impacted by indexes.The main points for someone to keep in mind when considering index creation is that he must know his data,know his workload and finally know how SQL Server works.</p>
<p>I will beginning a series of small posts looking into the various aspects of indexes hoping to shed some light on their use and misuse.A lot of times I find out that every time a DBA or developer detects a performance degradation tends to create indexes on the tables of the backend (SQL Server in this case).If there is a problem with performance it does not mean that index creation is always the answer. Many reasons could cause performance bottlenecks (e.g the hardware cannot withstand the workload,excessive blocking or little reuse of the execution plans)</p>
<p>In this post I will show you why it is a bad idea to <strong>over index</strong> your SQL Server tables.Over indexing consumes unnecessary disk space and the query optimiser may use the less efficient index thus the less efficient execution plan.</p>
<p>We will need a database to work with. I will use <strong>AdventureWorks2008R2</strong>. You can download it <a href="http://msftdbprodsamples.codeplex.com/releases/view/55926" target="_blank">here</a> .</p>
<p>We will need an instance of SQL Server running in our machine.You can download and install the free SQL Server Express edition from <a href="http://www.microsoft.com/sqlserver/en/us/editions/express.aspx" target="_blank">here</a>.</p>
<p>I will create a new empty table in the <strong>AdventureWorks2008R2</strong> .I will call it <strong>NewPerson</strong> and it will be based on the <strong>Person</strong> table and I will insert the same data that resides in the <strong>Person</strong> table to the <strong>NewPerson</strong> tables.There will be no indexes on the <strong>NewPerson</strong> table.I will use <strong>STATISTICS TIME ON</strong> to measure the time needed to insert the rows to the</p>
<p>Launch SQL Server Management Studio and log in the instance of SQL Server that <strong>AdventureWorks2008R2</strong> is attached. In an new query window type</p>
<pre class="brush: sql; title: ; notranslate">

USE [AdventureWorks2008R2]
GO

SET NOCOUNT ON

SELECT *
INTO [Person].[NewPerson]
FROM [Person].[Person]
WHERE 3 = 4
GO

SET STATISTICS TIME ON
GO

INSERT INTO [AdventureWorks2008R2].[Person].[NewPerson]
([BusinessEntityID]
,[PersonType]
,[NameStyle]
,[Title]
,[FirstName]
,[MiddleName]
,[LastName]
,[Suffix]
,[EmailPromotion]
,[AdditionalContactInfo]
,[Demographics]
,[rowguid]
,[ModifiedDate])
SELECT    [BusinessEntityID]
,[PersonType]
,[NameStyle]
,[Title]
,[FirstName]
,[MiddleName]
,[LastName]
,[Suffix]
,[EmailPromotion]
,[AdditionalContactInfo]
,[Demographics]
,[rowguid]
,[ModifiedDate]
FROM [Person].[Person]
GO

SELECT * FROM Person.NewPerson

</pre>
<p>Have a look at the statistics output. In my case I got the results shown in the picture below</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/04/execution.jpg"><img class="aligncenter size-large wp-image-1816" title="execution" src="http://dotnetstories.files.wordpress.com/2012/04/execution.jpg?w=1024&#038;h=550" alt="" width="1024" height="550" /></a></p>
<p>Now I will truncate the table and I will create a clustered and non clustered indexes on almost every column of the <strong>NewPerson</strong> table.Then I will insert the same data again and make a note of the statistics again.Type the following in the same query window.</p>
<pre class="brush: sql; title: ; notranslate">

TRUNCATE TABLE [Person].[NewPerson]
GO

ALTER TABLE [Person].[NewPerson]
ADD CONSTRAINT [PK_BusinessEntityID]
PRIMARY KEY CLUSTERED
([BusinessEntityID] ASC,
[LastName] ASC) ON [PRIMARY]
GO

CREATE NONCLUSTERED INDEX [IX_NewPersonType]
ON [Person].[NewPerson]
([PersonType] ASC) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [IX_NameStyle]
ON [Person].[NewPerson]
([NameStyle] ASC) ON [PRIMARY]
GO

CREATE NONCLUSTERED INDEX [IX_FirstName]
ON [Person].[NewPerson]
([FirstName] ASC) ON [PRIMARY]
GO

CREATE NONCLUSTERED INDEX [IX_MiddleName]
ON [Person].[NewPerson]
([MiddleName] ASC) ON [PRIMARY]
GO

CREATE NONCLUSTERED INDEX [IX_NSuffix]
ON [Person].[NewPerson]
([Suffix] ASC) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [IX_EmailPromotion]
ON [Person].[NewPerson]
([EmailPromotion] ASC) ON [PRIMARY]
GO

CREATE NONCLUSTERED INDEX [IX_rowguid]
ON [Person].[NewPerson]
([rowguid] ASC) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [IX_ModifiedDate]
ON [Person].[NewPerson]
([ModifiedDate] ASC) ON [PRIMARY]
GO

INSERT INTO [AdventureWorks2008R2].[Person].[NewPerson]
([BusinessEntityID]
,[PersonType]
,[NameStyle]
,[Title]
,[FirstName]
,[MiddleName]
,[LastName]
,[Suffix]
,[EmailPromotion]
,[AdditionalContactInfo]
,[Demographics]
,[rowguid]
,[ModifiedDate])
SELECT    [BusinessEntityID]
,[PersonType]
,[NameStyle]
,[Title]
,[FirstName]
,[MiddleName]
,[LastName]
,[Suffix]
,[EmailPromotion]
,[AdditionalContactInfo]
,[Demographics]
,[rowguid]
,[ModifiedDate]
FROM [Person].[Person]
GO

</pre>
<p>Have a look again at the statistics output. In my case I got the results shown in the picture below.<br />
They are significantly greater (40% or higher).</p>
<p><a href="http://dotnetstories.files.wordpress.com/2012/04/execution-1.jpg"><img class="aligncenter size-large wp-image-1817" title="execution-1" src="http://dotnetstories.files.wordpress.com/2012/04/execution-1.jpg?w=1024&#038;h=550" alt="" width="1024" height="550" /></a></p>
<p>In this case we see that over indexing does not help the performance of the system.We should drop the ones we do not need.   The <strong>NewPerson</strong> table has <strong>19972</strong> rows only. If it had 2-3 million rows the elapsed time in the second case it would be 10(1000%) times greater.</p>
<p>Hope it helps!!!!</p>
<br />Filed under: <a href='http://dotnetstories.wordpress.com/category/sql-server/'>Sql Server</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2005/'>Sql Server 2005</a>, <a href='http://dotnetstories.wordpress.com/category/sql-server-2008/'>SQL Server 2008</a> Tagged: <a href='http://dotnetstories.wordpress.com/tag/indexes/'>Indexes</a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/dotnetstories.wordpress.com/1813/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/dotnetstories.wordpress.com/1813/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=dotnetstories.wordpress.com&#038;blog=1661705&#038;post=1813&#038;subd=dotnetstories&#038;ref=&#038;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://dotnetstories.wordpress.com/2012/04/07/the-dangers-of-over-indexing-your-sql-server-tables/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:content url="http://2.gravatar.com/avatar/ecbfd3584f3b2c0c3f528bacdfc15e0a?s=96&#38;d=http%3A%2F%2F2.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96" medium="image">
			<media:title type="html">fofo</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/04/execution.jpg?w=1024" medium="image">
			<media:title type="html">execution</media:title>
		</media:content>

		<media:content url="http://dotnetstories.files.wordpress.com/2012/04/execution-1.jpg?w=1024" medium="image">
			<media:title type="html">execution-1</media:title>
		</media:content>
	</item>
	</channel>
</rss>
