<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:blogger="http://schemas.google.com/blogger/2008" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0"><channel><atom:id>tag:blogger.com,1999:blog-495177919198719500</atom:id><lastBuildDate>Thu, 13 Jun 2013 21:09:00 +0000</lastBuildDate><category>Integration Services SSIS</category><category>Link List</category><category>MySQL</category><category>SQL Server Training</category><category>SQL Server Denali</category><category>SQL Server General</category><category>Reporting Services SSRS</category><category>Analysis Services SSAS</category><category>SQL Azure</category><category>SQL Server 2012</category><category>SQL Data Services</category><category>Transact SQL T-SQL</category><category>SQL Server Administration</category><category>Business Intelligence</category><title>Microsoft Sql Server Tutorials</title><description>Learn how to use SQL Server 2005 and SQL Server 2008 R2 with free SQL Server tutorials.</description><link>http://www.sqlservercurry.com/</link><managingEditor>noreply@blogger.com (Suprotim Agarwal)</managingEditor><generator>Blogger</generator><openSearch:totalResults>661</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/sqlservercurry/blog" /><feedburner:info uri="sqlservercurry/blog" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><feedburner:emailServiceId>sqlservercurry/blog</feedburner:emailServiceId><feedburner:feedburnerHostname>http://feedburner.google.com</feedburner:feedburnerHostname><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-5284789532407449267</guid><pubDate>Wed, 12 Jun 2013 18:05:00 +0000</pubDate><atom:updated>2013-06-12T11:05:09.530-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Transact SQL T-SQL</category><category domain="http://www.blogger.com/atom/ns#">SQL Server 2012</category><title>SQL Server 2012 Query Pagination</title><description>In SQL Server 2012, Microsoft introduced a couple of T-SQL Enhancements. One of them is Query Pagination which we will explore today.&lt;br /&gt;
&lt;br /&gt;
For this demonstration, I am using the Northwind database to demonstrate this new feature. Now in most of the applications, a common requirement is how to fetch the data from the database servers, page wise. &lt;br /&gt;
In earlier versions of SQL Server like SQL Server 2005/2008/R2, we can implement Pagination by using different techniques. For example, we implement pagination using ROW_NUMBER() function or CTE - Common Table Expression.&lt;br /&gt;
&lt;br /&gt;
In SQL Server 2012, Microsoft has introduced Pagination as a part of Select query in a Order By clause. Now you will have to use OFFSET and FETCH NEXT with the order by clause. &lt;br /&gt;
&lt;br /&gt;
Let's take a look at a few examples. I am using Northwind database for this demonstration. I have created a Stored Procedure which takes two parameters. First parameter takes the page number and the second parameter ask you to fetch the no. of records for that page. The stored procedure code is as below - &lt;br /&gt;
&lt;br /&gt;
&lt;img alt="query-pagination" border="0" src="http://lh6.ggpht.com/-OdtYLmgkmm4/Ubix9QgYliI/AAAAAAAADNo/x8tea8VOybc/query-pagination%25255B2%25255D.png?imgmax=800" height="161" style="background-image: none; border-bottom: 0px; border-left: 0px; border-right: 0px; border-top: 0px; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="query-pagination" width="597" /&gt;&lt;br /&gt;
&lt;br /&gt;
If you execute the above stored procedure &lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Consolas; font-size: small;"&gt;EXEC FetchPagedRecords 2,10&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Consolas; font-size: small;"&gt;&lt;br /&gt;&lt;/span&gt;
you will get the following results -&lt;br /&gt;
&lt;br /&gt;
&lt;img alt="image" border="0" src="http://lh5.ggpht.com/-zscum-5gz6w/Ubix-ZfDwhI/AAAAAAAADNw/d0FF7nY5B3o/image%25255B2%25255D.png?imgmax=800" height="242" style="background-image: none; border-bottom: 0px; border-left: 0px; border-right: 0px; border-top: 0px; display: inline; margin: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="image" width="395" /&gt;&lt;br /&gt;
&lt;br /&gt;
The OFFSET specifies the number of rows to skip before it starts returning the rows and FETCH NEXT specifies the number of rows to be returned.&lt;br /&gt;
&lt;br /&gt;
Microsoft has introduced an easy way of implementing Data Paging in SQL Server 2012 by adding OFFSET and FETCH NEXT in an Order By clause. I hope you will use it in your applications.&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/MOj3R4K8RY4" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/MOj3R4K8RY4/sql-server-2012-query-pagination_12.html</link><author>noreply@blogger.com (Pravinkumar Dabade)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh6.ggpht.com/-OdtYLmgkmm4/Ubix9QgYliI/AAAAAAAADNo/x8tea8VOybc/s72-c/query-pagination%25255B2%25255D.png?imgmax=800" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2013/06/sql-server-2012-query-pagination_12.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-4640815391884466013</guid><pubDate>Sat, 01 Jun 2013 05:10:00 +0000</pubDate><atom:updated>2013-05-31T22:10:52.927-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Transact SQL T-SQL</category><title>Get Temporary Table Column Names in SQL Server</title><description>&lt;div align="justify"&gt;
When a temporary table is created, a record gets created in tempdb’s sys.tables and tempdb.sys.columns system table. You can query these tables to get meta data information from a temporary table.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
Here’s how to retrieve a Temporary Table’s Column Names. Let’s first create a temporary table with some columns&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;create table #SomeTmpTbl      &lt;br /&gt;(      &lt;br /&gt;col1 int,      &lt;br /&gt;col2 varchar(20),      &lt;br /&gt;col3 datetime      &lt;br /&gt;)&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Now query the tempdb.sys.columns in the following manner&lt;br /&gt;
&lt;br /&gt;
&lt;pre&gt;&lt;code&gt;&lt;span style="font-size: small;"&gt;select * from tempdb.sys.columns where object_id =
object_id('tempdb..#SomeTmpTbl');&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;br /&gt;
and there you go!&lt;br /&gt;
&lt;br /&gt;
&lt;img alt="query-temp-tables" border="0" height="129" src="http://lh6.ggpht.com/-c_YqqMkfHAk/T6i1pMHFvpI/AAAAAAAACjA/I5LNCF4Kh24/query-temp-tables%25255B8%25255D.png?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="query-temp-tables" width="554" /&gt;&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/M1F-Lwj_1Sw" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/M1F-Lwj_1Sw/get-temporary-table-column-names-in-sql.html</link><author>noreply@blogger.com (Suprotim Agarwal)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh6.ggpht.com/-c_YqqMkfHAk/T6i1pMHFvpI/AAAAAAAACjA/I5LNCF4Kh24/s72-c/query-temp-tables%25255B8%25255D.png?imgmax=800" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2013/05/get-temporary-table-column-names-in-sql.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-8160159677381142496</guid><pubDate>Tue, 14 May 2013 04:37:00 +0000</pubDate><atom:updated>2013-05-13T21:37:00.684-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Transact SQL T-SQL</category><category domain="http://www.blogger.com/atom/ns#">SQL Server Administration</category><title>Finding Last Backup Date in SQL Server 2005</title><description>&lt;div align="justify"&gt;
I saw a question on the SO forums asking for&amp;nbsp;a simple way to find the last backup date of a SQL Server 2005 database. Here's one approach.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;img alt="SQL Server Last Backup" border="0" height="220" src="http://lh5.ggpht.com/-bEtseTcl-eo/TkDbABIMtJI/AAAAAAAACH4/_Iugr0etti8/image%25255B2%25255D.png?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="SQL Server Last Backup" width="506" /&gt;&lt;br /&gt;
&lt;br /&gt;
During a backup operation, the following tables are updated in&amp;nbsp;SQL Server 2005: &lt;br /&gt;
&lt;ul&gt;
&lt;li&gt;msdb.dbo.backupfile, &lt;/li&gt;
&lt;li&gt;     &lt;div align="justify"&gt;
msdb.dbo.backupmediaset , &lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;     &lt;div align="justify"&gt;
msdb.dbo.backupmediafamily and &lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;     &lt;div align="justify"&gt;
msdb.dbo.backupset. &lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;div align="justify"&gt;
Here we are doing a&amp;nbsp;join between sys.sysdatabase and msdb.dbo.backupset which gets us the database name, the last backup date and who took the backup. sys.sysdatabase is queried as it contains one row for each database in an instance of Microsoft SQL Server.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
OUTPUT&lt;br /&gt;
&lt;br /&gt;
&lt;img alt="SQL Server BackUp" border="0" height="138" src="http://lh3.ggpht.com/-4uiL3ELFT-I/TkDbBOhmMFI/AAAAAAAACIA/sa3_Q7Gw-2Y/image%25255B9%25255D.png?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="SQL Server BackUp" wseridth="377" /&gt;&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/NSDiGyHFgLU" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/NSDiGyHFgLU/finding-last-backup-date-in-sql-server.html</link><author>noreply@blogger.com (Suprotim Agarwal)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh5.ggpht.com/-bEtseTcl-eo/TkDbABIMtJI/AAAAAAAACH4/_Iugr0etti8/s72-c/image%25255B2%25255D.png?imgmax=800" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2013/05/finding-last-backup-date-in-sql-server.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-4552013890187329883</guid><pubDate>Wed, 01 May 2013 09:25:00 +0000</pubDate><atom:updated>2013-05-03T02:48:25.769-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Transact SQL T-SQL</category><title>SQL Server NTILE Function</title><description>&lt;div style="text-align: justify;"&gt;
The SQL Server NTILE() function divides the result set into a specified number of even sized group (approximate division) and assigns a ranking value to these groups.&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
The NTILE() function is similar to other ranking functions like the &lt;a href="http://www.sqlservercurry.com/2009/04/rank-vs-denserank-with-example-using.html"&gt;RANK() and DENSE_RANK()&lt;/a&gt;, except that NTILE takes one parameter of type int/bigint that specifies the number of groups into which each partition must be divided.&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
Let us see an example. We will query the Products table of the Northwind database and divide it into 5 groups ordered by the UnitsInStock:&lt;/div&gt;
&lt;br /&gt;
&lt;img alt="NTILE SQL Server" border="0" height="81" src="http://lh6.ggpht.com/-Y9hiSKIoi0I/TfhQAWcE_OI/AAAAAAAACB4/yjfSVH52Njs/image%25255B2%25255D.png?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="NTILE SQL Server" width="428" /&gt;&lt;br /&gt;
&lt;br /&gt;
OUTPUT&lt;br /&gt;
&lt;br /&gt;
&lt;img alt="NTILE SQL Server Demo" border="0" height="158" src="http://lh4.ggpht.com/-zEomxHcFU5U/TfhQBPUTZLI/AAAAAAAACB8/LJGkpK9UwFU/image%25255B5%25255D.png?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="NTILE SQL Server Demo" width="443" /&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div align="justify"&gt;
On running the query, the results are divided into 5 groups, but because the total number of rows (48) is not divisible by the number of groups (5), NTILE puts 10 rows in the first three group and the remaining two groups have 9 rows each.&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/Drv1DXjOXx0" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/Drv1DXjOXx0/sql-server-ntile-function.html</link><author>noreply@blogger.com (Suprotim Agarwal)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh6.ggpht.com/-Y9hiSKIoi0I/TfhQAWcE_OI/AAAAAAAACB4/yjfSVH52Njs/s72-c/image%25255B2%25255D.png?imgmax=800" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2013/05/sql-server-ntile-function.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-2227272651772067701</guid><pubDate>Thu, 25 Apr 2013 11:46:00 +0000</pubDate><atom:updated>2013-04-25T04:46:55.834-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">SQL Server Administration</category><title>Activity Monitor in SQL Server</title><description>Activity Monitor is a handy tool in SQL Server to quickly see performance hot spots in the server, network and database activity. One of the most commonly performed tasks in Activity Monitor is to kill a stubborn connection that will not release its resources. In SQL Server 2008, this tool has undergone a major facelift from the 2005 version and helps in tracking your SQL server performance even better than before. &lt;br /&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
To view Activity Monitor in SQL Server 2008, right-click the instance name and choose Activity Monitor or click the Activity Monitor icon on the standard toolbar. &lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;img alt="image" border="0" height="305" src="http://lh4.ggpht.com/-SybfpSLeUgI/TttqDjr6DxI/AAAAAAAACXI/6kMXda3aZcI/image%25255B13%25255D.png?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; margin: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="image" width="422" /&gt; &lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
In SQL Server 2005, Activity Monitor can be viewed in SSMS by connecting to the server with Object Explorer, expanding ‘Management’, and then double-click ‘Activity Monitor’.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
Once opened, the Activity Monitor Dashboard View contains four graphs which can help identity any abnormal activity in the Database. The graphs include % Processor Time (SQL Server), Waiting Tasks, Database I/O and Batch Requests. The default graph refresh rate is 10 seconds, but you change that easily by right-clicking any of the four graphs and selecting the appropriate refresh interval.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
This snapshot is very helpful to get a quick performance snapshot without the need to use other monitoring tool for the same purpose.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;img alt="SQL Activity Monitor" border="0" height="201" src="http://lh4.ggpht.com/-UJioY8Q-PYg/TttqE5thC4I/AAAAAAAACXQ/z_J-v-xQaq4/image%25255B10%25255D.png?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="SQL Activity Monitor" width="590" /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
This dashboard also contains expandable/collapsible panes to view detailed information about Processes, Resources, I/O and&amp;nbsp;Expensive Queries. Just click on the expand button to the right of each pane to view the detailed information. Here’s a quick overview of the different graphs and what they show.&lt;/div&gt;
&lt;dl&gt;
&lt;dt&gt;     &lt;div align="justify"&gt;
&lt;strong&gt;Processor Time&lt;/strong&gt; - The percentage of elapsed time that the processor spends to execute non-idle threads for the instance across all CPUs. The Processes pane gives information on what processes are running, what resources are being utilized etc. You can even right-click a process and choose Trace Process in SQL Server Profiler. Presto!&lt;/div&gt;
&lt;/dt&gt;
&lt;dt&gt;&lt;div align="justify"&gt;
&amp;nbsp;&lt;/div&gt;
&lt;/dt&gt;
&lt;dt&gt;     &lt;div align="justify"&gt;
&lt;strong&gt;Waiting Tasks&lt;/strong&gt; - The number of tasks that are waiting for processor, I/O, or memory resources. The Resource Waits pane details the processes waiting for other resources on&amp;nbsp;the server.&amp;nbsp; It shows the latest information from several &lt;a href="http://www.sqlservercurry.com/2010/12/list-dynamic-management-views-dmv-by.html" title="List of SQL Server DMV"&gt;DMVs&lt;/a&gt; like the sys.dm_os_wait_stats&lt;/div&gt;
&lt;/dt&gt;
&lt;dt&gt;&lt;div align="justify"&gt;
&amp;nbsp;&lt;/div&gt;
&lt;/dt&gt;
&lt;dt&gt;     &lt;div align="justify"&gt;
&lt;strong&gt;Database I/O&lt;/strong&gt; – Information on data and log files for system and user databases. Provides the transfer rate in MB/Sec, of data from memory to disk, disk to memory, or disk to disk. The pane contains information to&amp;nbsp;quickly detect a contention in disk I/O.&lt;/div&gt;
&lt;/dt&gt;
&lt;dt&gt;&lt;div align="justify"&gt;
&amp;nbsp;&lt;/div&gt;
&lt;/dt&gt;
&lt;strong&gt;
&lt;/strong&gt;
&lt;dt&gt;&lt;strong&gt;     &lt;/strong&gt;&lt;div align="justify"&gt;
&lt;strong&gt;Batch Requests/sec&lt;/strong&gt; - The number of SQL Server batches that are received by the instance. The Expensive Query pane lets you find and tune expensive queries. You can even right-click any query and view its graphical execution plan. Also see &lt;a href="http://www.sqlservercurry.com/2010/07/find-most-time-consuming-code-in-your.html"&gt;Find the Most Time Consuming Code in your SQL Server Database&lt;/a&gt;&lt;/div&gt;
&lt;/dt&gt;
&lt;dt&gt;&lt;div align="justify"&gt;
&lt;/div&gt;
&lt;/dt&gt;
&lt;/dl&gt;
&lt;div align="justify"&gt;
&lt;strong&gt;Note:&lt;/strong&gt; To view the Activity Monitor in SQL Server, a user must have VIEW SERVER STATE permission. Also make sure you close the Activity Monitor tool in SSMS when it is not required. &lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/26p_BoAhILM" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/26p_BoAhILM/activity-monitor-in-sql-server.html</link><author>noreply@blogger.com (Suprotim Agarwal)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh4.ggpht.com/-SybfpSLeUgI/TttqDjr6DxI/AAAAAAAACXI/6kMXda3aZcI/s72-c/image%25255B13%25255D.png?imgmax=800" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2013/04/activity-monitor-in-sql-server.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-8482046235469694201</guid><pubDate>Mon, 15 Apr 2013 09:55:00 +0000</pubDate><atom:updated>2013-04-25T04:59:03.556-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Transact SQL T-SQL</category><category domain="http://www.blogger.com/atom/ns#">SQL Server Administration</category><title>SQL Server Management Objects 2008 (SMO) New Features</title><description>&lt;div align="justify"&gt;
MSDN defines SMO as - &lt;em&gt;SQL Server Management Objects (SMO) is a collection of objects that are designed for programming all aspects of managing Microsoft SQL Server&lt;/em&gt;. In this article we will practically explore some features of &lt;a href="http://msdn.microsoft.com/en-us/library/ms162169.aspx" target="_blank"&gt;SQL Server Management Objects&lt;/a&gt;.&lt;/div&gt;
&lt;br /&gt;
&lt;div align="justify"&gt;
To start with, let’s create a ‘Windows Application’ using Visual Studio and name the application as ‘SMOExamples’ as shown below – &lt;/div&gt;
&lt;br /&gt;
&lt;a href="http://lh5.ggpht.com/_RHQ2YLJe5_U/TbUH9FzLz7I/AAAAAAAAAPo/oVXlpkC70F8/s1600-h/clip_image001%5B5%5D.png"&gt;&lt;img alt="SQL Server SMO app" border="0" height="474" src="http://lh4.ggpht.com/_RHQ2YLJe5_U/TbUH90pTp4I/AAAAAAAAAPs/GMfwPKlBkFs/clip_image001_thumb%5B2%5D.png?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="SQL Server SMO app" width="566" /&gt;&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
Now let’s add the following references to our project. &lt;br /&gt;
&lt;ol&gt;
&lt;li&gt;Microsoft.SqlServer.ConnectionInfo.dll&lt;/li&gt;
&lt;li&gt;Microsoft.SqlServer.Smo.dll&lt;/li&gt;
&lt;li&gt;Microsoft.SqlServer.SmoExtended.dll&lt;/li&gt;
&lt;li&gt;Microsoft.SqlServer.SqlEnum.dll&lt;/li&gt;
&lt;li&gt;Microsoft.SqlServer.Management.Sdk.Sfc.dll&lt;/li&gt;
&lt;/ol&gt;
&lt;br /&gt;
&lt;div align="justify"&gt;
All these references can be found in the path – ‘C:\Program Files\Microsoft SQL Server\100\SDK\Assemblies’.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;
Now let’s design our windows form as shown below – &lt;/div&gt;
&lt;br /&gt;
&lt;a href="http://lh5.ggpht.com/_RHQ2YLJe5_U/TbUH-V7u2PI/AAAAAAAAAPw/QnW9XgeQOhQ/s1600-h/clip_image002%5B9%5D.png"&gt;&lt;img alt="SMO Examples" border="0" height="326" src="http://lh5.ggpht.com/_RHQ2YLJe5_U/TbUH_MfTZYI/AAAAAAAAAP0/xVh6uD_Ycpk/clip_image002_thumb%5B6%5D.png?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="SMO Examples" width="338" /&gt;&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div align="justify"&gt;
Declare an object which will make a connection to our SQL Server instance. For this, we will use a class called ‘Server’ as shown below – &lt;/div&gt;
&lt;br /&gt;
&lt;div align="justify"&gt;
&lt;span style="font-family: Courier New;"&gt;Server srv = new Server(Environment.MachineName);&lt;/span&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div align="justify"&gt;
Now to test the connection to the SQL Server, let’s write the following code on the Form Load event as shown below – &lt;/div&gt;
&lt;br /&gt;
&lt;a href="http://lh3.ggpht.com/_RHQ2YLJe5_U/TbUH_jfvTtI/AAAAAAAAAP4/OXnurUPrTZY/s1600-h/image%5B7%5D.png"&gt;&lt;img alt="SMO test connection" border="0" height="82" src="http://lh5.ggpht.com/_RHQ2YLJe5_U/TbUIARN2X9I/AAAAAAAAAP8/MeOsw6KQDvM/image_thumb%5B5%5D.png?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="SMO test connection" width="567" /&gt;&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div align="justify"&gt;
Now let’s create a Database and Table with the name ‘SMOExample’ and ‘SMOTable’ respectively by writing the following code on the click event of ‘Create Database And Table’ button – &lt;/div&gt;
&lt;br /&gt;
&lt;a href="http://lh5.ggpht.com/_RHQ2YLJe5_U/TbUIBKYPRXI/AAAAAAAAAQA/fcyVVo9HwM0/s1600-h/image%5B13%5D.png"&gt;&lt;img alt="SMO Create Database" border="0" height="197" src="http://lh6.ggpht.com/_RHQ2YLJe5_U/TbUIByoRL_I/AAAAAAAAAQE/3M3KzkqQtT0/image_thumb%5B9%5D.png?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="SMO Create Database" width="535" /&gt;&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div align="justify"&gt;
For creating a database, we have used a class called ‘Database’ which takes two parameters in the constructor. First one is the SQL Server instance in which we have to create a database. Second is the database name.&lt;/div&gt;
&lt;br /&gt;
&lt;div align="justify"&gt;
Now let’s create a script for all the tables available in our database. Write bee following code on the click event of ‘Generate Script’ button – &lt;/div&gt;
&lt;br /&gt;
&lt;a href="http://lh4.ggpht.com/_RHQ2YLJe5_U/TbUICa5JMrI/AAAAAAAAAQI/oZrrhmvJMyw/s1600-h/image%5B19%5D.png"&gt;&lt;img alt="SQL Server SMO Generate Tables" border="0" height="293" src="http://lh6.ggpht.com/_RHQ2YLJe5_U/TbUIDAOYfWI/AAAAAAAAAQM/fheE5V396x8/image_thumb%5B13%5D.png?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="SQL Server SMO Generate Tables" width="585" /&gt;&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div align="justify"&gt;
For generating the script files we are using a ‘Scripter’ class.&lt;/div&gt;
&lt;br /&gt;
&lt;div align="justify"&gt;
Now let’s write the code to take a backup of the complete database. Write the following code on the click event of ‘Backup Database’ button – &lt;/div&gt;
&lt;br /&gt;
&lt;a href="http://lh5.ggpht.com/_RHQ2YLJe5_U/TbUID3LwPYI/AAAAAAAAAQQ/YGcI8oJDT6k/s1600-h/image%5B22%5D.png"&gt;&lt;img alt="SQL Server SMO BackUp Database" border="0" height="171" src="http://lh4.ggpht.com/_RHQ2YLJe5_U/TbUIEq80nEI/AAAAAAAAAQU/T7qfFFsU1I8/image_thumb%5B14%5D.png?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="SQL Server SMO BackUp Database" width="498" /&gt;&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div align="justify"&gt;
To take a backup of the database, we are using a ‘Backup’ class and to decide what will be the device of backup, we are using ‘BackupDeviceItem’ class.&lt;/div&gt;
&lt;br /&gt;
&lt;div align="justify"&gt;
Now the last thing we will explore is how to verify the backup, which is taken before we restore it to another server/same server. Write the following code on the ‘Verify Backup’ button – &lt;/div&gt;
&lt;br /&gt;
&lt;a href="http://lh6.ggpht.com/_RHQ2YLJe5_U/TbUIFDhoE0I/AAAAAAAAAQY/P8jeIKWp-Fg/s1600-h/image%5B27%5D.png"&gt;&lt;img alt="SQL Server SMO Verify Database" border="0" height="243" src="http://lh4.ggpht.com/_RHQ2YLJe5_U/TbUIFgKIWrI/AAAAAAAAAQc/MD5cj6YVgWA/image_thumb%5B17%5D.png?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="SQL Server SMO Verify Database" width="527" /&gt;&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div align="justify"&gt;
Now run your project. This will show you the date and time created for ‘AdventureWorks’ database as shown below – &lt;/div&gt;
&lt;br /&gt;
&lt;a href="http://lh4.ggpht.com/_RHQ2YLJe5_U/TbUIGdC5WGI/AAAAAAAAAQg/KA6MXhjlwoQ/s1600-h/clip_image001%5B6%5D%5B2%5D.png"&gt;&lt;img alt="clip_image001[6]" border="0" height="124" src="http://lh3.ggpht.com/_RHQ2YLJe5_U/TbUIHFk5XiI/AAAAAAAAAQk/ccyPhAakn_Q/clip_image001%5B6%5D_thumb.png?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; margin: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="clip_image001[6]" width="386" /&gt;&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
Now let’s click the button ‘Create Database and Table’ and observe the results in SQL Server Management&lt;br /&gt;
&amp;nbsp;Studio Object Explorer – &lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://lh6.ggpht.com/_RHQ2YLJe5_U/TbUIHjwSpoI/AAAAAAAAAQo/rqhxmyJfBhE/s1600-h/clip_image002%5B10%5D%5B2%5D.png"&gt;&lt;img alt="clip_image002[10]" border="0" height="86" src="http://lh4.ggpht.com/_RHQ2YLJe5_U/TbUIISlli8I/AAAAAAAAAQs/rvYluNUsOnk/clip_image002%5B10%5D_thumb.png?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; margin: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="clip_image002[10]" width="289" /&gt;&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
Now click on the button ‘Generate Script’. Go to ‘C:\’ and verify the script file – &lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://lh5.ggpht.com/_RHQ2YLJe5_U/TbUII7kExnI/AAAAAAAAAQw/pWcZhNFJS_k/s1600-h/clip_image004%5B4%5D.jpg"&gt;&lt;img alt="clip_image004" border="0" height="278" src="http://lh3.ggpht.com/_RHQ2YLJe5_U/TbUIJi17hlI/AAAAAAAAAQ0/fujyhLVOhzE/clip_image004_thumb%5B1%5D.jpg?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="clip_image004" width="585" /&gt;&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div align="justify"&gt;
Finally click the button ‘Backup Database’ and check your ‘C:\’ drive. Your backup file should have been created. Similarly click on ‘Verify Backup’ button and it will show you a success message.&lt;/div&gt;
&lt;br /&gt;
&lt;div align="justify"&gt;
Summary – In this article we saw few capabilities of SQL Server Management Objects (SMO).&lt;/div&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;a href="http://cid-2c5f5b0560e374cb.office.live.com/self.aspx/.Public/SQLServerCurry/SMOExamples.zip" target="_blank"&gt;Download the source code&lt;/a&gt;&lt;/strong&gt;&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/quVnmfzrlqU" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/quVnmfzrlqU/sql-server-management-objects-2008-smo.html</link><author>noreply@blogger.com (Pravinkumar Dabade)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh4.ggpht.com/_RHQ2YLJe5_U/TbUH90pTp4I/AAAAAAAAAPs/GMfwPKlBkFs/s72-c/clip_image001_thumb%5B2%5D.png?imgmax=800" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2013/04/sql-server-management-objects-2008-smo.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-7919733440203338892</guid><pubDate>Tue, 09 Apr 2013 05:52:00 +0000</pubDate><atom:updated>2013-04-08T22:52:46.359-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Transact SQL T-SQL</category><category domain="http://www.blogger.com/atom/ns#">SQL Server Administration</category><title>List Empty Tables in SQL Server</title><description>I was recently doing a clean up of my website database. I remember creating some tables on my database but never adding any new rows to it.&lt;br /&gt;
&lt;br /&gt;
Here’s a simple query to list all empty tables in your SQL Server database that uses a &lt;a href="http://www.sqlservercurry.com/search?q=dmv"&gt;Dynamic Management View&lt;/a&gt; called dm_db_partition_stats which returns page and row-count information for every partition in the current database.&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Consolas; font-size: small;"&gt;;WITH EmptyRows AS     &lt;br /&gt;(      &lt;br /&gt;&amp;nbsp;&amp;nbsp; SELECT SUM(row_count) AS [TotalRows],       &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; OBJECT_NAME(OBJECT_ID) AS TableName      &lt;br /&gt;&amp;nbsp;&amp;nbsp; FROM sys.dm_db_partition_stats      &lt;br /&gt;&amp;nbsp;&amp;nbsp; WHERE index_id = 0 OR index_id = 1      &lt;br /&gt;&amp;nbsp;&amp;nbsp; GROUP BY OBJECT_ID      &lt;br /&gt;)      &lt;br /&gt;SELECT * FROM EmptyRows      &lt;br /&gt;WHERE [TotalRows] = 0&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Consolas;"&gt;&lt;/span&gt;&lt;br /&gt;
&lt;strong&gt;OUTPUT&lt;/strong&gt; &lt;br /&gt;
&amp;nbsp;&amp;nbsp; &lt;br /&gt;
&lt;img alt="image" border="0" height="286" src="http://lh6.ggpht.com/-PjkfyB81D6s/UWOsTV8t-CI/AAAAAAAAC_U/gIrssXLiDH0/image%25255B2%25255D.png?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; margin: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="image" width="252" /&gt;&lt;br /&gt;
&lt;br /&gt;
Note that the results from the view is only approximate. If there are any active transactions that are inserting or deleting rows, the count may not include it.&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/rp2qGrq-VOU" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/rp2qGrq-VOU/list-empty-tables-in-sql-server.html</link><author>noreply@blogger.com (Suprotim Agarwal)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh6.ggpht.com/-PjkfyB81D6s/UWOsTV8t-CI/AAAAAAAAC_U/gIrssXLiDH0/s72-c/image%25255B2%25255D.png?imgmax=800" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2013/04/list-empty-tables-in-sql-server.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-2802693208831525703</guid><pubDate>Thu, 28 Mar 2013 05:45:00 +0000</pubDate><atom:updated>2013-03-27T22:45:59.179-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Transact SQL T-SQL</category><category domain="http://www.blogger.com/atom/ns#">SQL Server Administration</category><category domain="http://www.blogger.com/atom/ns#">SQL Server 2012</category><title>Does my SQL Server Database Support Compression?</title><description>Some time back, Praveen Dabade had written a nice article on SQL Server &lt;a href="http://www.sqlservercurry.com/2011/05/compressed-tables-and-indexes-in-sql.html"&gt;Compressed Tables and Indexes in SQL Server&lt;/a&gt; where he explained how compression is now supported on ‘ROW and PAGE’ for tables and indexes. However did you know that compression is an enterprise-level feature?&lt;br /&gt;
&lt;br /&gt;
How do determine what Enterprise Edition features are enabled on your database? Well you can use the &lt;em&gt;sys.dm_persisted_sku_features&lt;/em&gt; DMV to find what Enterprise Edition features are    &lt;br /&gt;enabled on your database.&lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://www.sqlservercurry.com/search?q=DMV" title="SQL DMV"&gt;Learn more about Dynamic Management Views (DMV’s) here&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
Here’s the query for the same&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Consolas; font-size: small;"&gt;SELECT feature_name,feature_id     &lt;br /&gt;FROM      &lt;br /&gt;sys.dm_db_persisted_sku_features;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Running this query will list all edition-specific features that are enabled in the current database. Some of the database changing features restricted to the SQL Server&amp;nbsp;Enterprise 
or Developer editions are Compression, Partitioning, ChangeCapture etc.&lt;br /&gt;
&lt;br /&gt;
This DMV is also useful in situations where you are planning to move a database from a higher to a lower edition. Eg: From Enterprise to Standard edition. Running the query will tell you if there are any Enterprise Edition features enabled that may not work when you move to a lower edition. &lt;br /&gt;
&lt;br /&gt;
The DMV will return no rows if no features restricted to a particular edition are used by the database.&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/CmoXy2cEDTo" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/CmoXy2cEDTo/does-my-sql-server-database-support.html</link><author>noreply@blogger.com (Suprotim Agarwal)</author><thr:total>0</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2013/03/does-my-sql-server-database-support.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-5333133524858495819</guid><pubDate>Tue, 12 Mar 2013 05:40:00 +0000</pubDate><atom:updated>2013-03-11T22:40:08.961-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Transact SQL T-SQL</category><title>Order By Clause and Literal Values in SQL Server</title><description>The literal value in an ORDER BY Clause can be an ordinal position of the column or just literal values depending on the usage &lt;br /&gt;
&lt;br /&gt;
Ordinal position in a table refers to the position of the column in a table. It can also point to the column number in a SELECT statement&lt;br /&gt;
&lt;br /&gt;
Consider the following set of data&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Consolas; font-size: small;"&gt;create table testing(id int, names varchar(100))&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Consolas; font-size: small;"&gt;insert into testing(id,names)     &lt;br /&gt;select 18,'Charles' union all      &lt;br /&gt;select 20,'Ashok' union all      &lt;br /&gt;select 19,'Birla' ;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;The select statement&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Consolas; font-size: small;"&gt;select * from testing order by 1&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Consolas;"&gt;&lt;/span&gt;&lt;br /&gt;
returns the following result&lt;br /&gt;
&lt;br /&gt;
&lt;img alt="image" border="0" height="128" src="http://lh3.ggpht.com/-N1ZYz4UlLfM/UT6-7jAmukI/AAAAAAAAAc4/3JiBNheEvok/image%25255B4%25255D.png?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="image" width="205" /&gt;&lt;br /&gt;
&lt;br /&gt;The number 1 in Order by clause refers to the first column listed in the SELECT statement so the result is ordered by column ID&lt;br /&gt;
&lt;br /&gt;
The select statement&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Consolas; font-size: small;"&gt;select * from testing order by 2&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
returns the following result&lt;br /&gt;
&lt;br /&gt;&lt;img alt="image" border="0" height="123" src="http://lh6.ggpht.com/-q8AiT8RyAIk/UT6-8WMWcpI/AAAAAAAAAdA/JELautMduyo/image%25255B7%25255D.png?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; margin: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="image" width="194" /&gt;&lt;br /&gt;
As you see the resultset is ordered by second column NAMES&lt;br /&gt;
&lt;br /&gt;
But if you use a CASE expression and a number, it will behave differently. Suppose you want to keep the name Birla in first row and sort other rows by alphabetical order of remaining names, you can use the following statement&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Consolas; font-size: small;"&gt;select * from testing order by case when names='Birla' then 1 else 2 end,names&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Consolas;"&gt;&lt;/span&gt;&lt;br /&gt;
The result is&lt;br /&gt;
&lt;br /&gt;
&lt;img alt="image" border="0" height="122" src="http://lh4.ggpht.com/-3qaI8IEkTho/UT6-82lSPHI/AAAAAAAAAdI/nQTBWUQ-EwI/image%25255B10%25255D.png?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; margin: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="image" width="188" /&gt;&lt;br /&gt;
&lt;br /&gt;In the above statement, number 1 and 2 are literal values and do not refer any columns. If the name is Birla, a value 1 is assigned otherwise 2 is assigned and ordered by these values first, then ordered by names.&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/fZOiYUyjzT8" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/fZOiYUyjzT8/order-by-clause-and-literal-values-in.html</link><author>noreply@blogger.com (Madhivanan)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh3.ggpht.com/-N1ZYz4UlLfM/UT6-7jAmukI/AAAAAAAAAc4/3JiBNheEvok/s72-c/image%25255B4%25255D.png?imgmax=800" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2013/03/order-by-clause-and-literal-values-in.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-5026888941662179633</guid><pubDate>Wed, 27 Feb 2013 06:26:00 +0000</pubDate><atom:updated>2013-02-26T22:26:11.003-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Transact SQL T-SQL</category><category domain="http://www.blogger.com/atom/ns#">SQL Server Administration</category><title>Computed columns - Persisted Vs Non-persisted‏</title><description>Computed columns are derived columns based on other existing columns in the same table. Refer to this post &lt;a href="http://www.sqlservercurry.com/2012/11/computed-columns-in-sql-server.html"&gt;http://www.sqlservercurry.com/2012/11/computed-columns-in-sql-server.html&lt;/a&gt; for more information:&lt;br /&gt;
&lt;br /&gt;
There are two types of computed columns namely persisted and non-persisted. &lt;br /&gt;
&lt;br /&gt;
There are some major differences between these two&lt;br /&gt;
&lt;br /&gt;
1. Non-persisted columns are calculated on the fly (ie when the SELECT query is executed) whereas persisted columns are calculated as soon as data is stored in the table.&lt;br /&gt;
&lt;br /&gt;
2. Non-persisted columns do not consume any space as they are calculated only when you SELECT the column. Persisted columns consume space for the data&lt;br /&gt;
&lt;br /&gt;
3. When you SELECT data from these columns Non-persisted columns are slower than Persisted columns&lt;br /&gt;
&lt;br /&gt;
Consider the following set of code&lt;br /&gt;
&lt;br /&gt;
&lt;img alt="sql-persisted-code" border="0" height="193" src="http://lh6.ggpht.com/--ypZZSRpYog/US2mtZ17TvI/AAAAAAAAAcc/Ii8GSk_D7cg/sql-persisted-code%25255B10%25255D.png?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="sql-persisted-code" width="590" /&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Consolas; font-size: small;"&gt;create table #t1(col1 int, col2 as col1*0.20)&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Consolas; font-size: small;"&gt;insert into #t1 (col1)     &lt;br /&gt;select top 5000 row_number() over (order by s1.name) from sys.objects as s1 cross join sys.objects as s2 &lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;&lt;span style="font-family: Consolas; font-size: small;"&gt;create table #t2(col1 int, col2 as col1*0.20 persisted)&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Consolas; font-size: small;"&gt;insert into #t2 (col1)     &lt;br /&gt;select top 5000 row_number() over (order by s1.name) from sys.objects as s1 cross join sys.objects as s2&lt;/span&gt; &lt;br /&gt;
&lt;br /&gt;Run the following code to understand that table with persisted computed columns consumes more space when compared to a table with non-persisted computed columns. Refer the column named data&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Consolas; font-size: small;"&gt;exec tempdb..spaceused #t1     &lt;br /&gt;exec tempdb..spaceused #t2&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;img alt="sql-persisted-nonpresisted" border="0" height="212" src="http://lh5.ggpht.com/-y2MPz9k2VnM/US2mtz-4FMI/AAAAAAAAAck/4eBYRmBDPUs/sql-persisted-nonpresisted%25255B9%25255D.png?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="sql-persisted-nonpresisted" width="590" /&gt;&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/sHehRVHtX1c" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/sHehRVHtX1c/computed-columns-persisted-vs-non.html</link><author>noreply@blogger.com (Madhivanan)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh6.ggpht.com/--ypZZSRpYog/US2mtZ17TvI/AAAAAAAAAcc/Ii8GSk_D7cg/s72-c/sql-persisted-code%25255B10%25255D.png?imgmax=800" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2013/02/computed-columns-persisted-vs-non.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-8200414026061727929</guid><pubDate>Thu, 14 Feb 2013 08:30:00 +0000</pubDate><atom:updated>2013-02-14T00:30:04.157-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Transact SQL T-SQL</category><title>Parsename to Extract Precision and Scale values‏ From Decimal Numbers</title><description>The numeric datatype stores numbers with precision and scale. Suppose you want to extract only a precision or a scale, you can do it via many ways. One of the ways is to make use of the PARSENAME function.&lt;br /&gt;
&lt;br /&gt;
Consider the following example&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Consolas; font-size: small;"&gt;declare @amount decimal(12,2)     &lt;br /&gt;set @amount=87234.50      &lt;br /&gt;select parsename(@amount,2) as precision, parsename(@amount,1) as scale&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
The result is&lt;br /&gt;
&lt;br /&gt;
precision&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; scale   &lt;br /&gt;---------&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; --------    &lt;br /&gt;87234&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 50&lt;br /&gt;
&lt;br /&gt;
Parsename is used to extract specified part of a name. In general, it is used to extract names from four part object names separated by a dot. Argument number 1 extracts the last part of a string, and the 2nd argument extracts the next last part.&lt;br /&gt;
&lt;br /&gt;
This way we can effectively make use of parsename function to extract precision and scale values from the decimal numbers.&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/k4L8rHAkazo" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/k4L8rHAkazo/parsename-to-extract-precision-and.html</link><author>noreply@blogger.com (Madhivanan)</author><thr:total>0</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2013/02/parsename-to-extract-precision-and.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-6239143112374492551</guid><pubDate>Sat, 09 Feb 2013 08:15:00 +0000</pubDate><atom:updated>2013-02-09T00:15:56.940-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Transact SQL T-SQL</category><category domain="http://www.blogger.com/atom/ns#">SQL Server 2012</category><title>Error Handling in SQL Server with THROW</title><description>Continuing our &lt;a href="http://www.sqlservercurry.com/search/label/SQL%20Server%202012"&gt;&lt;strong&gt;series on SQL Server 2012&lt;/strong&gt;&lt;/a&gt;, today we will talk about THROW. In versions prior to SQL Sever 2012, we used @@RAISE_ERROR to generate error messages dynamically or using the sys.messages catalog.&lt;br /&gt;
&lt;br /&gt;
Consider the following example&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Consolas; font-size: small;"&gt;SELECT ROUND(800.0, -3)&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
On executing this statement, you get the following error:&lt;br /&gt;
&lt;br /&gt;
&lt;img alt="image" border="0" height="98" src="http://lh3.ggpht.com/-AxBMK68fEAM/URYFZg5KYNI/AAAAAAAAC0M/9zyXHX0YyXA/image%25255B2%25255D.png?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; margin: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="image" width="508" /&gt;&lt;br /&gt;
&lt;br /&gt;
because the value does not fit into the decimal data type.&lt;br /&gt;
&lt;br /&gt;
You can use @@RAISERROR to raise a message&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Consolas; font-size: small;"&gt;BEGIN TRY     &lt;br /&gt;SELECT ROUND(800.0, -3)      &lt;br /&gt;END TRY      &lt;br /&gt;BEGIN CATCH      &lt;br /&gt;DECLARE @ErrorMsg nvarchar(1000), @Severity int      &lt;br /&gt;SELECT @ErrorMsg = ERROR_MESSAGE(),      &lt;br /&gt;@Severity = ERROR_SEVERITY()      &lt;br /&gt;RAISERROR (@ErrorMsg, @Severity, 1)      &lt;br /&gt;END CATCH&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Note: The old syntax of RAISERROR syntax specifying the error number and message number got deprecated (RAISEERROR 50005 ‘Exception Occurred’). Instead the new syntax RAISEERROR(50005, 10, 1) allowed you to specify the messageid, severity and state). For new applications use THROW.&lt;br /&gt;
&lt;br /&gt;
However in SQL Server 2012, there’s a better way to this without much efforts – THROW. Consider the following code&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Consolas; font-size: small;"&gt;BEGIN TRY     &lt;br /&gt;SELECT ROUND(800.0, -3)      &lt;br /&gt;END TRY      &lt;br /&gt;BEGIN CATCH      &lt;br /&gt;THROW      &lt;br /&gt;END CATCH&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;img alt="image" border="0" height="111" src="http://lh4.ggpht.com/-0_YJ5OKk3H0/URYFaZpwOeI/AAAAAAAAC0U/MFFWMkXyfl8/image%25255B5%25255D.png?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; margin: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="image" width="510" /&gt;&lt;br /&gt;
&lt;br /&gt;
As you can see, with just one word THROW, we were able to handle the error with grace and get a result too.&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/Pp-JKAzh8S8" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/Pp-JKAzh8S8/error-handling-in-sql-server-with-throw.html</link><author>noreply@blogger.com (Suprotim Agarwal)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh3.ggpht.com/-AxBMK68fEAM/URYFZg5KYNI/AAAAAAAAC0M/9zyXHX0YyXA/s72-c/image%25255B2%25255D.png?imgmax=800" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2013/02/error-handling-in-sql-server-with-throw.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-250055411294120505</guid><pubDate>Thu, 24 Jan 2013 10:04:00 +0000</pubDate><atom:updated>2013-01-24T02:04:29.505-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Transact SQL T-SQL</category><title>A Smart Way of Using SQL Server Books On Line (BOL) without the Index Option</title><description>SQL Server's help file also known as &lt;em&gt;Books On Line or BOL&lt;/em&gt; is a great resource to learn about all the features of SQL Server. You can make use of index option for detailed search. But there is also a simple yet smart way.&lt;br /&gt;
&lt;br /&gt;
Suppose you are working in SSMS (SQL Server Management Studio) and while working on it, if you want to get more information on a function, you can invoke BOL by pressing F1. If the cursor is inside a function/system procedure/system variables, the BOL opens with the topic specific from where it is invoked&lt;br /&gt;
&lt;br /&gt;
For example, you have typed system procedure sp_monitor in your query window and you want to learn more about it, make sure the cursor is placed anywhere within the word and press F1. BOL opens with the topic sp_monitor.&lt;br /&gt;
&lt;br /&gt;
You can also highlight any function and press F1, BOL opens with that topic&lt;br /&gt;
&lt;br /&gt;
Consider the following piece of code&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Consolas; font-size: small;"&gt;EXEC sp_monitor     &lt;br /&gt;GO      &lt;br /&gt;SELECT GETDATE()      &lt;br /&gt;GO&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Consolas; font-size: small;"&gt;select * from sys.objects where create_date&amp;gt;'20000101'&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Now highlight sp_monitor and press F1 as shown here&lt;br /&gt;
&lt;br /&gt;
&lt;img alt="sql-bol" border="0" height="229" src="http://lh4.ggpht.com/-iI2iM53A25A/UQEGaH0a0JI/AAAAAAAAAbw/UOVUz0ehAVk/sql-bol%25255B8%25255D.png?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="sql-bol" width="435" /&gt;&lt;br /&gt;
&lt;br /&gt;
BOL opens with the topic sp_monitor&lt;br /&gt;
&lt;br /&gt;
&lt;img alt="sp_monitor" border="0" height="232" src="http://lh4.ggpht.com/-ysYFrhMizNo/UQEGbSG1k9I/AAAAAAAAAb4/DgZLJg9C2UA/sp_monitor%25255B5%25255D.png?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="sp_monitor" width="590" /&gt; &lt;br /&gt;
&amp;nbsp;&amp;nbsp; &lt;br /&gt;
No highlight GETDATE() and press F1. &lt;br /&gt;
&lt;br /&gt;
&lt;img alt="highlight-getdate" border="0" height="226" src="http://lh4.ggpht.com/-7TaxgTH7np8/UQEGcKcmkqI/AAAAAAAAAb8/Bij4gEKsE5A/highlight-getdate%25255B8%25255D.png?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="highlight-getdate" width="430" /&gt;&lt;br /&gt;
&lt;br /&gt;
BOL opens displaying information about GETDATE(). &lt;br /&gt;
&lt;br /&gt;
&lt;img alt="getdate-bol" border="0" height="244" src="http://lh3.ggpht.com/-pb8kVUrVTZg/UQEGdVdMq-I/AAAAAAAAAcI/s3eHH-ZnY20/getdate-bol%25255B3%25255D.png?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="getdate-bol" width="590" /&gt;&lt;br /&gt;
&lt;br /&gt;
Similarly highlight sys.objects and press F1. BOL opens with the topic sys.objects.&lt;br /&gt;
&lt;br /&gt;
A simple yet smart way to make use of BOL without using the index option!&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/S4CHST6QwAY" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/S4CHST6QwAY/a-smart-way-of-using-sql-server-books.html</link><author>noreply@blogger.com (Madhivanan)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh4.ggpht.com/-iI2iM53A25A/UQEGaH0a0JI/AAAAAAAAAbw/UOVUz0ehAVk/s72-c/sql-bol%25255B8%25255D.png?imgmax=800" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2013/01/a-smart-way-of-using-sql-server-books.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-330535971113324197</guid><pubDate>Fri, 18 Jan 2013 09:24:00 +0000</pubDate><atom:updated>2013-01-18T01:24:18.105-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Transact SQL T-SQL</category><category domain="http://www.blogger.com/atom/ns#">SQL Server Administration</category><title>SQL Server SESSIONPROPERTY</title><description>There are various settings that can be done at the session level. For example, you can set ANSI_NULLS OFF for a session and ON for another session. If you want to know if a setting is ON or OFF before using the setting, you can make use of system function SESSIONPROPERTY.&lt;br /&gt;
&lt;br /&gt;
SESSIONPROPERTY will return 1 or 0 depending on whether the SET OPTION is ON or OFF&lt;br /&gt;
&lt;br /&gt;
Consider the following example&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Consolas; font-size: small;"&gt;SELECT SESSIONPROPERTY('ANSI_NULLS')&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
If the above statement returns 1, then ANSI_NULLS is ON in the session, otherwise it is OFF&lt;br /&gt;
Similarly the same function can be used to know the SET OPTION for various other options specified below&lt;br /&gt;
&lt;br /&gt;&lt;span style="font-family: Consolas; font-size: small;"&gt;QUOTED_IDENTIFIER     &lt;br /&gt;ARITHABORT      &lt;br /&gt;ANSI_NULL_DFLT_ON      &lt;br /&gt;ANSI_WARNINGS      &lt;br /&gt;ANSI_PADDING      &lt;br /&gt;CONCAT_NULL_YIELDS_NULL&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Consolas;"&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Consolas; font-size: small;"&gt;Eg:&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Consolas; font-size: small;"&gt;SELECT SESSIONPROPERTY('ANSI_WARNINGS')&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;img alt="image" border="0" height="230" src="http://lh5.ggpht.com/-7KREgs8eTmU/UPkUZgLB_LI/AAAAAAAAAbc/tr_Ohe0EiqE/image%25255B4%25255D.png?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="image" width="217" /&gt;&lt;br /&gt;
&lt;br /&gt;
If the above returns 1, then ANSI_WARNINGS is ON in the session, otherwise it is OFF.&lt;br /&gt;
&lt;br /&gt;
So when you are working in SQL Server, you can check the status of these settings using the SESSIONPROPERTY function&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/GX9JQnlHmPE" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/GX9JQnlHmPE/sql-server-sessionproperty.html</link><author>noreply@blogger.com (Madhivanan)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh5.ggpht.com/-7KREgs8eTmU/UPkUZgLB_LI/AAAAAAAAAbc/tr_Ohe0EiqE/s72-c/image%25255B4%25255D.png?imgmax=800" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2013/01/sql-server-sessionproperty.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-2475288194887595357</guid><pubDate>Tue, 15 Jan 2013 11:14:00 +0000</pubDate><atom:updated>2013-01-15T03:14:12.322-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Transact SQL T-SQL</category><title>Different Methods To Simulate Cross Join</title><description>A Cross join can be effectively used to produce larger dataset that can be used for various purposes. One usage is to generate number table using a CROSS JOIN.&lt;br /&gt;
&lt;br /&gt;
The following code generate series of numbers starting from 1&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Consolas; font-size: small;"&gt;select      &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; row_number() over (order by s1.name) as number       &lt;br /&gt;from      &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; sys.objects as s1 cross join sys.objects as s2&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Consolas;"&gt;&lt;/span&gt;&lt;br /&gt;
The resultset will have N*N rows where N is number of rows of catalog view sys.objects. If the rows of this view is 120, the resultset will have 120*120=14400 rows. So we will have a number table with values ranging from 1 to 14400.&lt;br /&gt;
&lt;br /&gt;
&lt;img alt="cross-join-result" border="0" height="290" src="http://lh3.ggpht.com/-_pb64jsTwA0/UPU5t6Um46I/AAAAAAAAAbA/233sTD241_U/cross-join-result%25255B4%25255D.png?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="cross-join-result" width="433" /&gt;&lt;br /&gt;
&lt;br /&gt;
The same functionality can be simulated without using CROSS JOIN&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;Method 1. Using Old Style Non-Ansi JOIN without WHERE clause&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Consolas; font-size: small;"&gt;select      &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; row_number() over (order by s1.name) as number       &lt;br /&gt;from      &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; sys.objects as s1 , sys.objects as s2&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Consolas;"&gt;&lt;/span&gt;&lt;br /&gt;
Because there is no WHERE clause, it becomes CROSS JOIN producing N*N rows&lt;br /&gt;
&lt;br /&gt;&lt;strong&gt;Method 2. Use INNER JOIN with no column matching&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Consolas; font-size: small;"&gt;select      &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; row_number() over (order by s1.name) as number       &lt;br /&gt;from      &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; sys.objects as s1 inner join sys.objects as s2      &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; on 1=1&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Because no columns are JOINed and 1=1 is always true, the above INNER JOIN will become a CROSS JOIN and produce N*N rows&lt;br /&gt;
&lt;br /&gt;
&lt;img alt="cross-join-result" border="0" height="290" src="http://lh6.ggpht.com/-GCUGlw488Ik/UPU5u4npKgI/AAAAAAAAAbE/vx4y6Mvubpw/cross-join-result%25255B8%25255D.png?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="cross-join-result" width="433" /&gt;&lt;br /&gt;
&lt;br /&gt;
Similarly LEFT and RIGHT JOINs with no column matching will behave the same like method 2&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/hAg-HH75nW0" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/hAg-HH75nW0/different-methods-to-simulate-cross-join.html</link><author>noreply@blogger.com (Madhivanan)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh3.ggpht.com/-_pb64jsTwA0/UPU5t6Um46I/AAAAAAAAAbA/233sTD241_U/s72-c/cross-join-result%25255B4%25255D.png?imgmax=800" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2013/01/different-methods-to-simulate-cross-join.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-1799159501251843084</guid><pubDate>Fri, 11 Jan 2013 09:31:00 +0000</pubDate><atom:updated>2013-01-11T01:32:11.601-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Transact SQL T-SQL</category><title>SQL Server - Time difference in HH hours, MM minutes and SS seconds format‏</title><description>Suppose you have two datetime values and want to express the difference in the format HH Hours, MM minutes and SS seconds. You can use CONVERT function with style 108 as shown below:   &lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Consolas; font-size: small;"&gt; declare @start_date datetime, @end_date datetime     &lt;br /&gt; select @start_date ='20121210 11:19:33', @end_date='20121210 19:28:12'&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Consolas; font-size: small;"&gt;&lt;br /&gt;select stuff(stuff(convert(varchar(10),@end_date-@start_date,108),6,1,' Minutes and '),3,1,' Hours, ')+' Seconds'&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Consolas;"&gt;&lt;/span&gt;&lt;br /&gt;
The variables @start_date and @end_date have values for start and end times. We can directly substract @start_date from @end_date and the result is still in datetime. CONVERT with style 108 will only extract time part in the format HH:MM:SS. &lt;br /&gt;
&lt;br /&gt;
Now replace : after HH with Hours, replace : after MM with minutes and , append ' Seconds' at the end of time and the result is at the format HH Hours, MM minutes and SS seconds. &lt;br /&gt;
&lt;br /&gt;
The result of the above code is&lt;br /&gt;
&lt;br /&gt;
&lt;span style="color: blue;"&gt;08 Hours, 08 Minutes and 39 Seconds&lt;/span&gt;&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/A3PAwIwL7ho" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/A3PAwIwL7ho/sql-server-time-difference-in-hh-hours.html</link><author>noreply@blogger.com (Madhivanan)</author><thr:total>0</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2013/01/sql-server-time-difference-in-hh-hours.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-7648623691592714596</guid><pubDate>Mon, 07 Jan 2013 17:38:00 +0000</pubDate><atom:updated>2013-01-07T09:38:42.169-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Transact SQL T-SQL</category><title>SQL Server - Which is faster INNER JOIN or LEFT JOIN?</title><description>You may be interested to know which is faster – the LEFT JOIN or INNER JOIN. Well, in general INNER JOIN will be faster because it only returns the rows matched in all joined tables based on the joined column. But LEFT JOIN will return all rows from a table specified LEFT and all matching rows from a table specified RIGHT.&lt;br /&gt;
&lt;br /&gt;
We can analyze this using the STATISTICS TIME ON option. Consider the sys.objects and sys.columns catalog views and join them using INNER and LEFT joins and see the result&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Consolas; font-size: small;"&gt;set statistics time on     &lt;br /&gt;select       &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; t1.object_id, t2.object_id       &lt;br /&gt;from       &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; sys.objects as t1 inner join sys.columns as t2      &lt;br /&gt;on t1.object_id=t2.object_id &lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Consolas; font-size: small;"&gt;select      &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; t1.object_id, t2.object_id       &lt;br /&gt;from       &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; sys.objects as t1 left join sys.columns as t2      &lt;br /&gt;on t1.object_id=t2.object_id &lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Consolas; font-size: small;"&gt;set statistics time off&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;The result is of SET STATISTICS TIME ON is&lt;br /&gt;
&lt;br /&gt;
&lt;img alt="sql-join-inner-left" border="0" height="230" src="http://lh5.ggpht.com/-Iolj617j3t0/UOsH4ix__zI/AAAAAAAAAak/yqnTGmdYyso/sql-join-inner-left%25255B9%25255D.png?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="sql-join-inner-left" width="353" /&gt;&lt;br /&gt;
Note:&amp;nbsp; You may think here that since the LEFT join returns more rows, so it is taking more time. Now eliminate the NULL object_ids from sys.columns and observe the result&lt;br /&gt;
&lt;br /&gt;&lt;span style="font-family: Consolas; font-size: small;"&gt;set statistics time on     &lt;br /&gt;select       &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; t1.object_id, t2.object_id       &lt;br /&gt;from       &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; sys.objects as t1 inner join sys.columns as t2      &lt;br /&gt;on t1.object_id=t2.object_id &lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Consolas; font-size: small;"&gt;select      &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; t1.object_id, t2.object_id       &lt;br /&gt;from       &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; sys.objects as t1 left join sys.columns as t2      &lt;br /&gt;on t1.object_id=t2.object_id       &lt;br /&gt;where       &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; t2.object_id is not null&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Consolas; font-size: small;"&gt;set statistics time off&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Consolas;"&gt;&lt;/span&gt;&lt;br /&gt;
The result is&lt;br /&gt;
&lt;br /&gt;
&lt;img alt="sql-join-inner-left-2" border="0" height="230" src="http://lh3.ggpht.com/-Xa8AY16fXq4/UOsH6fUy0qI/AAAAAAAAAas/xjJkqWy8bos/sql-join-inner-left-2%25255B8%25255D.png?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="sql-join-inner-left-2" width="380" /&gt;&lt;br /&gt;
So even though they both return the same number of rows, INNER JOIN is still faster.&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/3KwBqjJo_FE" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/3KwBqjJo_FE/sql-server-which-is-faster-inner-join.html</link><author>noreply@blogger.com (Madhivanan)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh5.ggpht.com/-Iolj617j3t0/UOsH4ix__zI/AAAAAAAAAak/yqnTGmdYyso/s72-c/sql-join-inner-left%25255B9%25255D.png?imgmax=800" height="72" width="72" /><thr:total>4</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2013/01/sql-server-which-is-faster-inner-join.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-3681519188865445432</guid><pubDate>Thu, 27 Dec 2012 04:14:00 +0000</pubDate><atom:updated>2012-12-26T20:14:48.534-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Transact SQL T-SQL</category><title>Remove Non-Alphabet Characters from a String–SQL Server</title><description>Suppose you have a string that contains numbers and other special characters and you want to keep only alphabets and remove all the other characters. One way to do this is to use a while loop that parses each character&lt;br /&gt;
&lt;br /&gt;
Here is the code for the same&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Consolas; font-size: small;"&gt;declare @str varchar(20)     &lt;br /&gt;set @str='ab12#89L(h12k'      &lt;br /&gt;Select @str as 'Original String'      &lt;br /&gt;declare @temp_str varchar(20), @i int      &lt;br /&gt;select @temp_str ='',@i=1      &lt;br /&gt;while @i&amp;lt;=len(@str)      &lt;br /&gt;begin      &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; set @temp_str=@temp_str+case when substring(@str,@i,1) like '[a-zA-Z]' then substring(@str,@i,1) else '' end      &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; set @i=@i+1      &lt;br /&gt;end      &lt;br /&gt;select @temp_str as 'String with Alphabets'&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Consolas;"&gt;&lt;/span&gt;&lt;br /&gt;
The code inside the While loop takes each character and check if it is in the range a-z or A-Z and appends that character with another variable. Other characters will not get appended. So at the end of while loop, the second variable will have only alphabets. &lt;br /&gt;
&lt;br /&gt;
&lt;img alt="image" border="0" height="233" src="http://lh6.ggpht.com/-I5BLMbH87kU/UNvK_883a1I/AAAAAAAAAaQ/JTpkFBYncZ4/image%25255B4%25255D.png?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="image" width="187" /&gt;&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/y-hd_Z7hN4I" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/y-hd_Z7hN4I/remove-non-alphabet-characters-from.html</link><author>noreply@blogger.com (Madhivanan)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh6.ggpht.com/-I5BLMbH87kU/UNvK_883a1I/AAAAAAAAAaQ/JTpkFBYncZ4/s72-c/image%25255B4%25255D.png?imgmax=800" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2012/12/remove-non-alphabet-characters-from.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-3789927499008517064</guid><pubDate>Mon, 17 Dec 2012 12:22:00 +0000</pubDate><atom:updated>2012-12-17T04:22:38.311-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Transact SQL T-SQL</category><category domain="http://www.blogger.com/atom/ns#">SQL Server Administration</category><title>Drop All Tables in Database whose name begins with tmp</title><description>Sometimes in your projects, you may be using staging tables for purposes like loading ,parsing and cleaning data. Suppose you name all of these table with the prefix tmp_ and after these tables have served their purpose, you now want to drop all these tables.&lt;br /&gt;
&lt;br /&gt;
Here are two ways to do this:&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;1. Execute Drop table statements&lt;/strong&gt; - Copy the result of this query which generates DROP TABLE statements and execute the result again. Eg:&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Consolas; font-size: small;"&gt;select 'drop table '+table_name from INFORMATION_SCHEMA.TABLES where table_name like 'tmp_%'&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
The above query generates drop table statements like&lt;br /&gt;
&lt;br /&gt;
&lt;span style="background-color: #a5a5a5;"&gt;drop table tmp_test     &lt;br /&gt;drop table tmp_emp      &lt;br /&gt;drop table tmp_data&lt;/span&gt;&lt;br /&gt;
&lt;span style="background-color: #a5a5a5;"&gt;&lt;/span&gt;&lt;br /&gt;
provided there are tables named tmp_test, tmp_test and tmp_data in the database.&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;2. Concatenate drop table statements and execute it as a whole&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Consolas; font-size: small;"&gt;declare @sql varchar(8000)     &lt;br /&gt;set @sql=''      &lt;br /&gt;select @sql=@sql+' drop table '+table_name from INFORMATION_SCEHMA.TABLES where table_name like 'tmp_%'      &lt;br /&gt;exec(@sql)&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
The variable @sql will have drop table statements concatenated for each table. It will have the string &lt;span style="font-family: Consolas; font-size: small;"&gt;drop table tmp_test drop table tmp_emp drop table tmp_data&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
When you execute the string generated from that variable, all those tables get dropped&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/T2A2n3CQ1RQ" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/T2A2n3CQ1RQ/drop-all-tables-in-database-whose-name.html</link><author>noreply@blogger.com (Madhivanan)</author><thr:total>0</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2012/12/drop-all-tables-in-database-whose-name.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-1770830089426544675</guid><pubDate>Fri, 14 Dec 2012 04:21:00 +0000</pubDate><atom:updated>2012-12-13T20:21:40.758-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Transact SQL T-SQL</category><title>SQL Server - Convert Month Number to Month Name</title><description>Here’s a simple way to convert a month number to month name in SQL Server&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Consolas; font-size: small;"&gt;DECLARE @monthNum int;     &lt;br /&gt;SET&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; @monthNum = 7;      &lt;br /&gt;SELECT&amp;nbsp; DateName( month , DateAdd( month , @MonthNum - 1 , '1900-01-01' ))      &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; as 'MonthName'      &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; WHERE @monthNum between 1 and 12&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Consolas;"&gt;&lt;/span&gt;&lt;br /&gt;
OUTPUT&lt;br /&gt;
&lt;br /&gt;
&lt;img alt="image" border="0" height="87" src="http://lh6.ggpht.com/-96LDVdNZ2dM/UMqoedpi4QI/AAAAAAAACsI/jIdt_NpojK8/image%25255B2%25255D.png?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; margin: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="image" width="177" /&gt;&lt;br /&gt;
&lt;br /&gt;
In order to get the MonthName of the current date, just use&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Consolas; font-size: small;"&gt;SELECT DATENAME(month, GETDATE()) AS 'MonthName'&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
OUTPUT&lt;br /&gt;
&lt;br /&gt;
&lt;img alt="image" border="0" height="83" src="http://lh6.ggpht.com/-e0QnBiEUUCA/UMqofsWJcKI/AAAAAAAACsQ/gxnUD8Zg5RM/image%25255B5%25255D.png?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; margin: 0px; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="image" width="177" /&gt;&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/9el1qgojZE4" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/9el1qgojZE4/sql-server-convert-month-number-to.html</link><author>noreply@blogger.com (Suprotim Agarwal)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh6.ggpht.com/-96LDVdNZ2dM/UMqoedpi4QI/AAAAAAAACsI/jIdt_NpojK8/s72-c/image%25255B2%25255D.png?imgmax=800" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2012/12/sql-server-convert-month-number-to.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-1269788162750685034</guid><pubDate>Fri, 30 Nov 2012 04:31:00 +0000</pubDate><atom:updated>2012-11-29T20:31:58.645-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Transact SQL T-SQL</category><title>Aggregates without GroupBy SQL Server</title><description>When you use aggregate functions along with non-aggregate columns, all non-aggregate columns should be part of GROUP BY Clause. However with the new version of aggregate functions with OVER(), it can be used without the usage of GROUP BY clause.&lt;br /&gt;
&lt;br /&gt;
Suppose you want to return the total number of rows along with a column, you can write count(*) as shown below&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Consolas; font-size: small;"&gt;select name,count(*) over () as total_rows from sysobjects&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Consolas;"&gt;&lt;/span&gt;&lt;br /&gt;
&lt;img alt="sql-aggregates" border="0" height="234" src="http://lh4.ggpht.com/-9XjQ7015GmY/ULg2VXIul5I/AAAAAAAAAZo/wOT8BKlWXhE/sql-aggregates%25255B4%25255D.png?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="sql-aggregates" width="325" /&gt;&lt;br /&gt;
&lt;br /&gt;
The result shows the value for name column along with total number of rows in total_rows column. Suppose you want to return total number of object types along with a column, you can write count(*) as shown below&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Consolas; font-size: small;"&gt;select name,xtype,count(*) over () as total_rows,count(*) over (partition by xtype order by xtype) as total_types from sysobjects&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Consolas;"&gt;&lt;/span&gt;&lt;br /&gt;
&lt;img alt="sql-aggregates-groupby" border="0" height="235" src="http://lh6.ggpht.com/-YDRgFgBsRbw/ULg2WTncAZI/AAAAAAAAAZw/CxxLOfc9S7Y/sql-aggregates-groupby%25255B4%25255D.png?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="sql-aggregates-groupby" width="407" /&gt;&lt;br /&gt;
&lt;br /&gt;
The results shows values for column name , xtype and total number of xtypes for each xtype value. This count differs for each xtype. &lt;br /&gt;
&lt;br /&gt;
This way you can make use of COUNT function without using GROUP BY Clause. From version 2012, you can also make use of SUM function for calculating running total using this technique which you can find at &lt;a href="http://www.sqlservercurry.com/2012/08/sql-server-2012-running-total-with-sum.html"&gt;http://www.sqlservercurry.com/2012/08/sql-server-2012-running-total-with-sum.html&lt;/a&gt;&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/u0omWwPFs5U" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/u0omWwPFs5U/aggregates-without-groupby-sql-server.html</link><author>noreply@blogger.com (Madhivanan)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh4.ggpht.com/-9XjQ7015GmY/ULg2VXIul5I/AAAAAAAAAZo/wOT8BKlWXhE/s72-c/sql-aggregates%25255B4%25255D.png?imgmax=800" height="72" width="72" /><thr:total>1</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2012/11/aggregates-without-groupby-sql-server.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-6081846036799292949</guid><pubDate>Wed, 21 Nov 2012 05:14:00 +0000</pubDate><atom:updated>2012-11-20T21:14:13.312-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Transact SQL T-SQL</category><title>SQL Server: Datetime vs Datetime2 Datatype</title><description>The datetime2 datatype was introduced in SQL Server 2008 along with the date and time datatypes.In this post, we will see some differences between the datetime and datetime2 datatypes.&lt;br /&gt;
&lt;br /&gt;
&lt;table border="1" cellpadding="2" cellspacing="0" style="width: 590px;"&gt;&lt;tbody&gt;
&lt;tr&gt;       &lt;td valign="top" width="36"&gt;&lt;strong&gt;SNo&lt;/strong&gt;&lt;/td&gt;        &lt;td valign="top" width="266"&gt;&lt;strong&gt;DATETIME&lt;/strong&gt;&lt;/td&gt;        &lt;td valign="top" width="286"&gt;&lt;strong&gt;DATETIME2&lt;/strong&gt;&lt;/td&gt;     &lt;/tr&gt;
&lt;tr&gt;       &lt;td valign="top" width="36"&gt;1&lt;/td&gt;        &lt;td valign="top" width="266"&gt;Minimum and maximum date ranges are            &lt;br /&gt;1900-01-01 and 9999-12-31 23:59:59.997&lt;/td&gt;        &lt;td valign="top" width="286"&gt;Minimum and maximum date ranges are            &lt;br /&gt;0001-01-01 and 9999-12-31 23:59:59.9999999&lt;/td&gt;     &lt;/tr&gt;
&lt;tr&gt;       &lt;td valign="top" width="36"&gt;2&lt;/td&gt;        &lt;td valign="top" width="266"&gt;Returns Current date with time with 3 fractional seconds precision &lt;/td&gt;        &lt;td valign="top" width="286"&gt;Return Current date with time with 7 fractional seconds precision &lt;/td&gt;     &lt;/tr&gt;
&lt;tr&gt;       &lt;td valign="top" width="36"&gt;3&lt;/td&gt;        &lt;td valign="top" width="266"&gt;Addition or subtraction to numbers is directly allowed. &lt;br /&gt;
&lt;span style="font-family: Consolas; font-size: small;"&gt;Ex SELECT GETDATE()+1, GETDATE()-1&lt;/span&gt;&lt;/td&gt;        &lt;td valign="top" width="286"&gt;Addition or subtraction to numbers is not directly allowed. The function DATEADD should be used            &lt;br /&gt;Ex &lt;br /&gt;
&lt;span style="font-family: Consolas; font-size: small;"&gt;SELECT DATEADD(DAY,1,SYSDATETIME()),DATEADD(DAY,-1,SYSDATETIME())&lt;/span&gt;&lt;/td&gt;     &lt;/tr&gt;
&lt;tr&gt;       &lt;td valign="top" width="36"&gt;4&lt;/td&gt;        &lt;td valign="top" width="266"&gt;Milliseconds are rounded to increments of .000, .003, or .007 seconds            &lt;br /&gt;Ex &lt;br /&gt;
&lt;span style="font-family: Consolas; font-size: small;"&gt;declare @datetime datetime              &lt;br /&gt;select @datetime ='2012-10-19 12:23:45.345'               &lt;br /&gt;select @datetime&lt;/span&gt;&lt;br /&gt;
The result is 2012-10-19 12:23:45.347&lt;/td&gt;        &lt;td valign="top" width="286"&gt;Not rounded until the millisecond precision exceeds 7            &lt;br /&gt;Ex &lt;br /&gt;
&lt;span style="font-family: Consolas; font-size: small;"&gt;declare @sysdatetime datetime2(7)              &lt;br /&gt;select @sysdatetime ='2012-10-19 12:23:45.99999945'               &lt;br /&gt;select @sysdatetime &lt;/span&gt;&lt;br /&gt;
The result is 2012-10-19 12:23:45.9999995&lt;/td&gt;     &lt;/tr&gt;
&lt;tr&gt;       &lt;td valign="top" width="36"&gt;5&lt;/td&gt;        &lt;td valign="top" width="266"&gt;Datatype cannot have variable size&lt;/td&gt;        &lt;td valign="top" width="286"&gt;Datatype can have variable size &lt;br /&gt;
&lt;span style="font-family: Consolas; font-size: small;"&gt;DECLARE @sysdatetime1 datetime2(3),@sysdatetime1 datetime2(5)&lt;/span&gt;&lt;/td&gt;     &lt;/tr&gt;
&lt;tr&gt;       &lt;td valign="top" width="36"&gt;6&lt;/td&gt;        &lt;td valign="top" width="266"&gt;TIME values can be added directly            &lt;br /&gt;Ex &lt;br /&gt;
&lt;span style="font-family: Consolas; font-size: small;"&gt;declare @datetime datetime              &lt;br /&gt;select @datetime ='2012-12-31 12:59:59.99'               &lt;br /&gt;select @datetime+'12:00:00'&lt;/span&gt;&lt;/td&gt;        &lt;td valign="top" width="286"&gt;TIME values cannot be added directly &lt;/td&gt;     &lt;/tr&gt;
&lt;tr&gt;       &lt;td valign="top" width="36"&gt;7&lt;/td&gt;        &lt;td valign="top" width="266"&gt;8 bytes is required to store the value&lt;/td&gt;        &lt;td valign="top" width="286"&gt;Depends on the millisecond precision; 6 to 8 bytes are required to store the value&lt;/td&gt;     &lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/XT0XNpihezw" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/XT0XNpihezw/sql-server-datetime-vs-datetime2.html</link><author>noreply@blogger.com (Madhivanan)</author><thr:total>0</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2012/11/sql-server-datetime-vs-datetime2.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-4837003218445967088</guid><pubDate>Fri, 16 Nov 2012 04:33:00 +0000</pubDate><atom:updated>2012-11-15T20:33:54.245-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Transact SQL T-SQL</category><title>Computed Columns in SQL Server</title><description>Computed columns are derived columns that are bound to values of other columns. However the datatype of these computed columns depends on the nature of the end result. They may be bound to columns of either the same datatype or they could be bound to columns of different datatypes.&lt;br /&gt;
&lt;br /&gt;
Consider the following set of data&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Consolas; font-size: small;"&gt;declare @t table(id int, computed_id as id, computed_date as dateadd(day,id,getdate()))     &lt;br /&gt;insert into @t (id)      &lt;br /&gt;select 5      &lt;br /&gt;select * from @t&lt;/span&gt; &lt;br /&gt;
&lt;br /&gt;The result is&lt;br /&gt;
&lt;br /&gt;
&lt;img alt="SQL Computed Column" border="0" height="83" src="http://lh4.ggpht.com/-4dQt9oHv4uI/UKXBTaxVu-I/AAAAAAAAAZM/ieiODTerCR4/image%25255B2%25255D.png?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="SQL Computed Column" width="281" /&gt;    &lt;br /&gt;
As you can see, the datatype of &lt;em&gt;computed_id&lt;/em&gt; will be same as that of the ID as ID is directly used in the computed column definition. However the datatype of the column &lt;em&gt;computed_date&lt;/em&gt; will be datetime because the expression dateadd(day,id,getdate()) will do an implicit conversion to datetime datatype as getdate() is used in the definition.&lt;br /&gt;
&lt;br /&gt;
Let us consider another set of data&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Consolas; font-size: small;"&gt;declare @t table(id int, computed_id as id/2.0, computed_date as id*300000000000)     &lt;br /&gt;insert into @t (id)      &lt;br /&gt;select 5      &lt;br /&gt;select * from @t&lt;/span&gt; &lt;br /&gt;
&lt;br /&gt;
The output is&lt;br /&gt;
&lt;br /&gt;
&lt;img alt="SQL Computed Column" border="0" height="74" src="http://lh6.ggpht.com/-QuRBOvzwOL8/UKXBUjTe0jI/AAAAAAAAAZU/5QEonGbu9wQ/image%25255B5%25255D.png?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="SQL Computed Column" width="246" /&gt;&lt;br /&gt;
&lt;br /&gt;
As you can see, the datatype of &lt;em&gt;computed_id&lt;/em&gt; will be of decimal type because of the expression id/2.0 which results to decimal number. The datatype of the column &lt;em&gt;computed_numberf&lt;/em&gt; will be BIGINT because the expression id*300000000000 will do an implicit conversion to the BIGINT datatype as the result won't fit into a INT datatype&lt;br /&gt;
&lt;br /&gt;
So the datatpye of computed column differs based on the expression and if you want to update the value returned by a computed column to another table, you need to make sure that the datatypes match each other. &lt;br /&gt;
&lt;br /&gt;
Keep these points in mind while using Computed columns in SQL Server&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/AN1Gw-v0Nbs" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/AN1Gw-v0Nbs/computed-columns-in-sql-server.html</link><author>noreply@blogger.com (Madhivanan)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh4.ggpht.com/-4dQt9oHv4uI/UKXBTaxVu-I/AAAAAAAAAZM/ieiODTerCR4/s72-c/image%25255B2%25255D.png?imgmax=800" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2012/11/computed-columns-in-sql-server.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-2576436907410982334</guid><pubDate>Wed, 07 Nov 2012 09:12:00 +0000</pubDate><atom:updated>2012-11-07T01:12:27.502-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Transact SQL T-SQL</category><title>SET ROWCOUNT VS TOP in SQL Server</title><description>Both SET ROWCOUNT statement and TOP clause are used to limit the number of rows returned. However there are some significant differences between them. They are listed out here&lt;br /&gt;
&lt;br /&gt;
&lt;table border="1" cellpadding="2" cellspacing="0" style="width: 590px;"&gt;&lt;tbody&gt;
&lt;tr&gt;       &lt;td valign="top" width="295"&gt;&lt;strong&gt;SET ROWCOUNT statement&lt;/strong&gt;&lt;/td&gt;        &lt;td valign="top" width="295"&gt;&lt;strong&gt;TOP clause&lt;/strong&gt;&lt;/td&gt;     &lt;/tr&gt;
&lt;tr&gt;       &lt;td valign="top" width="295"&gt;It is specific to a batch. It will affect all DML operations until it is reset to 0&lt;/td&gt;        &lt;td valign="top" width="295"&gt;It has statement level scope and it will not affect other statements until specified each for them&lt;/td&gt;     &lt;/tr&gt;
&lt;tr&gt;       &lt;td valign="top" width="295"&gt;Variable can be used in all version. Ex SET ROWCOUNT @var&lt;/td&gt;        &lt;td valign="top" width="295"&gt;Variable can be used only from version 2005 onwards like TOP (@var)&lt;/td&gt;     &lt;/tr&gt;
&lt;tr&gt;       &lt;td valign="top" width="295"&gt;Not possible to set percentage&lt;/td&gt;        &lt;td valign="top" width="295"&gt;Possible to set percentage option. Ex &lt;span style="font-family: Courier New; font-size: small;"&gt;SELECT TOP 20 percent * FROM TABLE&lt;/span&gt;&lt;/td&gt;     &lt;/tr&gt;
&lt;tr&gt;       &lt;td valign="top" width="295"&gt;Not possible to specify decimal value&lt;/td&gt;        &lt;td valign="top" width="295"&gt;Possible to specify decimal values along with PERCENT option.&lt;/td&gt;     &lt;/tr&gt;
&lt;tr&gt;       &lt;td valign="top" width="295"&gt;It is executed outside of actual DML and its value is not part of query plan&lt;/td&gt;        &lt;td valign="top" width="295"&gt;The expression used in TOP clause will be considered as part of query plan.&lt;/td&gt;     &lt;/tr&gt;
&lt;tr&gt;       &lt;td valign="top" width="295"&gt;Multiple SET ROWCOUNT statements are allowed in a single batch. However the lastly available before the statements will be used.&lt;br /&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;SET ROWCOUNT 10              &lt;br /&gt;SET ROWCOUNT 100               &lt;br /&gt;SELECT * FROM SYS.OBJECTS               &lt;br /&gt;SET ROWCOUNT 0&lt;/span&gt;&lt;br /&gt;
The count&amp;nbsp; 100 will be considered for execution&lt;/td&gt;        &lt;td valign="top" width="295"&gt;Multiple TOP is not allowed however they can be nested.&lt;br /&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;SELECT TOP 10 * FROM              &lt;br /&gt;(               &lt;br /&gt;SELECT TOP 100 * FROM SYS.OBJECTS               &lt;br /&gt;) AS T&lt;/span&gt;&lt;br /&gt;
The final result will have maximum of 10 rows&lt;/td&gt;     &lt;/tr&gt;
&lt;tr&gt;       &lt;td valign="top" width="295"&gt;As this is executed as a seperate statement It can not be part of VIEW definition&lt;/td&gt;        &lt;td valign="top" width="295"&gt;It can be part of VIEW definition.&lt;/td&gt;     &lt;/tr&gt;
&lt;tr&gt;       &lt;td valign="top" width="295"&gt;This is marked as Deprecated. Avoid using this&lt;/td&gt;        &lt;td valign="top" width="295"&gt;Always available in all versions&lt;/td&gt;     &lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/u4jO0fWd0mg" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/u4jO0fWd0mg/set-rowcount-vs-top-in-sql-server.html</link><author>noreply@blogger.com (Madhivanan)</author><thr:total>0</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2012/11/set-rowcount-vs-top-in-sql-server.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-355923437433944918</guid><pubDate>Fri, 02 Nov 2012 07:32:00 +0000</pubDate><atom:updated>2012-11-02T00:32:38.016-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Transact SQL T-SQL</category><category domain="http://www.blogger.com/atom/ns#">MySQL</category><title>Scope of variables in Dynamic SQL - SQL Server Vs MySQL‏</title><description>I have already posted about how Dynamic SQL works in SQL Server and MySQL at &lt;a href="http://www.sqlservercurry.com/2012/08/dynamic-sql-sql-server-vs-mysql.html"&gt;http://www.sqlservercurry.com/2012/08/dynamic-sql-sql-server-vs-mysql.html&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
There is a significant difference between SQL Server and MySQL as far as the scope of variables is concerned. The variable declared and accessed in Dynamic SQL can be accessed out of Dynamic SQL in MySQL, whereas this is not possible in SQL Server&lt;br /&gt;
&lt;br /&gt;
Consider the following set of data&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;create table testing(id int, names varchar(100))&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;insert into testing(id,names)     &lt;br /&gt;select 1,'test1' union all      &lt;br /&gt;select 2,'test2' union all      &lt;br /&gt;select 3,'test3'&lt;/span&gt; &lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;MySQL&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
The purpose is to assign a value to a variable in dynamic sql and access the same variable out of dynamic sql&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;set @sql:='set @count:=(select count(*) from testing);';     &lt;br /&gt;prepare stmt from @sql;      &lt;br /&gt;execute stmt ;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;select @count;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Courier New;"&gt;&lt;/span&gt;&lt;br /&gt;
In the above code, the variable @count is declared and assigned in dynamic sql. But after dynamic sql is executed using the prepare statement, the variable is still accessible. The statement &lt;span style="font-family: Courier New;"&gt;select @count&lt;/span&gt; returns the value 3&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;SQL Server&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
Create the same table testing in SQL Server. Now execute the following code&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;declare @sql varchar(8000)     &lt;br /&gt;set @sql='      &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; declare @count int       &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; set @count=(select count(*) from testing)      &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; '      &lt;br /&gt;execute (@sql)&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;select @count&lt;/span&gt; &lt;br /&gt;
&lt;br /&gt;
You will get the following error&lt;br /&gt;
&lt;br /&gt;
&lt;span style="color: red;"&gt;Msg 137, Level 15, State 2, Line 9     &lt;br /&gt;Must declare the scalar variable "@count".&lt;/span&gt;&lt;br /&gt;
&lt;span style="color: red;"&gt;&lt;/span&gt;&lt;br /&gt;
The code before select @count will get executed correctly. But the variable declared in dynamic sql cannot be accessed out of it. If you access it in same dynamic scope like below, it will work&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;declare @sql varchar(8000)     &lt;br /&gt;set @sql='      &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; declare @count int       &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; set @count=(select count(*) from testing)      &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; select @count       &lt;br /&gt;'      &lt;br /&gt;execute (@sql)&lt;/span&gt;    &lt;br /&gt;&amp;nbsp; &lt;br /&gt;So you need to be aware of this behavior when using Dynamic SQL.&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/ShwcAdODNn0" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/ShwcAdODNn0/scope-of-variables-in-dynamic-sql-sql.html</link><author>noreply@blogger.com (Madhivanan)</author><thr:total>0</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2012/11/scope-of-variables-in-dynamic-sql-sql.html</feedburner:origLink></item></channel></rss>
