<?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: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>Fri, 25 May 2012 07:08:06 +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 Data Services</category><category>SQL Server 2012</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>618</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-3605222503885385536</guid><pubDate>Fri, 25 May 2012 07:08:00 +0000</pubDate><atom:updated>2012-05-25T00:08:06.593-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 LEAD and LAG</title><description>&lt;p&gt;LEAD and LAG are two analytical functions that have been introduced in SQL Server 2012. Let’s quickly see the usage of these functions with an example.&lt;/p&gt;  &lt;p&gt;Sometimes you may need to display the previous or next row values for a column in the same row. In earlier versions, we had to use a Self join to do this. But from SQL Server 2012, we can make use of the analytical functions – LEAD and LAG&lt;/p&gt;  &lt;p&gt;Consider the following set of data. &lt;/p&gt;  &lt;p&gt;&lt;font size="3" face="Courier New"&gt;create table #sales (sales_id int identity(1,1), sales_date datetime, product_id int, qty int, sales_amount decimal(12))     &lt;br /&gt;insert into #sales(sales_date , product_id , qty,sales_amount)      &lt;br /&gt;select '20001111',1,12,48882 union all      &lt;br /&gt;select '20001112',1,33,65544 union all      &lt;br /&gt;select '20001113',1,9, 31289 union all      &lt;br /&gt;select '20001114',1,22,58860&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;Suppose you want to show the sales amount for a day as well as sales amount for the next day. Use the LEAD function as shown below&lt;/p&gt;  &lt;p&gt;&lt;font size="3" face="Courier New"&gt;select&amp;#160; product_id,sales_date,sales_amount, &lt;strong&gt;lead&lt;/strong&gt;(sales_amount) over (order by sales_date) as next_sales_amount from #sales&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="sql-12-lead-function" border="0" alt="sql-12-lead-function" src="http://lh4.ggpht.com/-RpbSaRs3uyA/T78vcciDioI/AAAAAAAAAUs/ILGOmc0z4a4/sql-12-lead-function%25255B2%25255D.png?imgmax=800" width="470" height="126" /&gt;&lt;/p&gt;  &lt;p&gt;Similarly, if you want to show the sales amount for a day as well as sales amount for the previous day, use the LAG function as shown below&lt;/p&gt;  &lt;p&gt;&lt;font size="3" face="Courier New"&gt;select&amp;#160; product_id,sales_date,sales_amount, lag(sales_amount) over (order by sales_date) as previous_sales_amount from #sales&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="sql-12-lag-function" border="0" alt="sql-12-lag-function" src="http://lh3.ggpht.com/-X3Ev6OxxMKg/T78vde3KyoI/AAAAAAAAAUw/kQTlVZgNfR8/sql-12-lag-function%25255B5%25255D.png?imgmax=800" width="491" height="164" /&gt;&lt;/p&gt;  &lt;p&gt;Note that like other windows functions row_number(), rank(), etc, both LEAD and LAG functions need Order by clause. It determines the order of rows so that SQL Server picks previous and next rows easily.&lt;/p&gt;  &lt;p&gt;The following queries show values based on qty column&lt;/p&gt;  &lt;p&gt;&lt;font size="3" face="Courier New"&gt;select&amp;#160; product_id,sales_date,sales_amount, lead(sales_amount) over (order by qty) as next_sales_amount from #sales&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size="3" face="Courier New"&gt;select&amp;#160; product_id,sales_date,sales_amount, lag(sales_amount) over (order by qty) as next_sales_amount from #sales&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;   &lt;br /&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="sql-12-lead-lag" border="0" alt="sql-12-lead-lag" src="http://lh3.ggpht.com/-58chQ5KfZXk/T78veFB3iBI/AAAAAAAAAU8/vkOlT81eayc/sql-12-lead-lag%25255B10%25255D.png?imgmax=800" width="502" height="286" /&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/495177919198719500-3605222503885385536?l=www.sqlservercurry.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/eu-qRq630pk" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/eu-qRq630pk/sql-server-2012-lead-and-lag.html</link><author>noreply@blogger.com (Madhivanan)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh4.ggpht.com/-RpbSaRs3uyA/T78vcciDioI/AAAAAAAAAUs/ILGOmc0z4a4/s72-c/sql-12-lead-function%25255B2%25255D.png?imgmax=800" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2012/05/sql-server-2012-lead-and-lag.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-3146629982733324569</guid><pubDate>Mon, 21 May 2012 15:25:00 +0000</pubDate><atom:updated>2012-05-21T08:26:17.971-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>Using Sequence in SQL Server 2012</title><description>&lt;p align="justify"&gt;Continuing my &lt;a href="http://www.sqlservercurry.com/search/label/SQL%20Server%202012"&gt;series on SQL Server 2012&lt;/a&gt;, today we will explore Sequence. Sequence is an object in SQL Server 2012 which can be used to generate customized sequence numbers. Sequence is an independent object and is not associated with any table by default. When you are inserting data into the table, we can make use of sequence values from the sequence object.&lt;/p&gt;  &lt;p align="justify"&gt;Consider the following Sequence and data&lt;/p&gt;  &lt;p&gt;&lt;font size="3" face="Courier New"&gt;create sequence my_seq     &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; as int      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; start with 1      &lt;br /&gt;&amp;#160;&amp;#160;&amp;#160; increment by 1 &lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size="3" face="Courier New"&gt;GO&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;The above creates the sequence named my_seq which has initial value of 1 and will increment by 1&lt;/p&gt;  &lt;p&gt;&lt;font size="3" face="Courier New"&gt;create table sales(sales_id int, product_id int,qty int,sales_amount decimal(12,2))&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size="3" face="Courier New"&gt;GO&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;Let us add some data to this table generating sales_id value using sequence&lt;/p&gt;  &lt;p&gt;&lt;font size="3" face="Courier New"&gt;insert into sales(sales_id,product_id,qty,sales_amount)     &lt;br /&gt;select next value for my_seq,10001,3,1200 union all      &lt;br /&gt;select next value for my_seq,10002,2,600 union all      &lt;br /&gt;select next value for my_seq,10003,10,200 union all      &lt;br /&gt;select next value for my_seq,10001,200,1200 union all      &lt;br /&gt;select next value for my_seq,10001,10,1200&lt;/font&gt; &lt;/p&gt;  &lt;p&gt;In the above insert statement, values are taken from sequence for sales_id column and every time a value is selected from the sequence, its value gets incremented by 1&lt;/p&gt;  &lt;p&gt;Now run this select statement and see the result&lt;/p&gt;  &lt;p&gt;&lt;font size="3" face="Courier New"&gt;select * from sales&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;OUTPUT&lt;/p&gt;  &lt;p&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="sequence-sql-2012" border="0" alt="sequence-sql-2012" src="http://lh6.ggpht.com/-RnY-S9hjY6I/T7peOLxfnzI/AAAAAAAAAUg/Rlc2O_YarkA/sequence-sql-2012%25255B30%25255D.png?imgmax=800" width="279" height="125" /&gt;&lt;/p&gt;  &lt;p&gt;If you want to restart the sequence values, you can use ALTER sequence statement&lt;/p&gt;  &lt;p&gt;&lt;font size="3" face="Courier New"&gt;alter sequence my_seq restart with 1&lt;/font&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/495177919198719500-3146629982733324569?l=www.sqlservercurry.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/kyO6cC1ObLY" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/kyO6cC1ObLY/using-sequence-in-sql-server-2012.html</link><author>noreply@blogger.com (Madhivanan)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh6.ggpht.com/-RnY-S9hjY6I/T7peOLxfnzI/AAAAAAAAAUg/Rlc2O_YarkA/s72-c/sequence-sql-2012%25255B30%25255D.png?imgmax=800" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2012/05/using-sequence-in-sql-server-2012.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-9216677556367605965</guid><pubDate>Thu, 17 May 2012 07:20:00 +0000</pubDate><atom:updated>2012-05-17T00:21:00.656-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">SQL Server 2012</category><title>Adventure Works Database for SQL Server 2012</title><description>SQL Server 2012 introduces many new technologies and features. One of the best ways to learn about these features is to try them out on a database. Luckily for us, we have the AdventureWorks database available since the year 2000. The best part is that the SQL Team has been regularly maintaining and updating the AdventureWorks sample database for all new versions of SQL Server, as they are released.&lt;br /&gt;
&lt;br /&gt;
To download versions of Adventure Works databases for SQL Server 2012, go to &lt;a href="http://msftdbprodsamples.codeplex.com/releases/view/55330" target="_blank" title="Download AdventureWorks for SQL Server 2012"&gt;&lt;strong&gt;AdventureWorks for SQL Server 2012&lt;/strong&gt;&lt;/a&gt; on CodePlex. &lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;AdventureWorks Community Samples Databases for SQL Azure&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
SQL Azure is the relational data store for the Azure platform. The SQL team has also released a AdventureWorks2012 Full database on CodePlex containing the full version of the AdventureWorks2012 database along with an AdventureWorks samples database that is scaled-out using SQL Azure Federations.&lt;br /&gt;
&lt;br /&gt;
&lt;a href="http://msftdbprodsamples.codeplex.com/releases/view/37304#DownloadId=342357"&gt;AdventureWorks2012Full_SQLAzure&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
There are some other downloads available for OLTP, Multidimensional models, Datawarehouse etc. Do check them out!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/495177919198719500-9216677556367605965?l=www.sqlservercurry.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/m3JELjc9m6s" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/m3JELjc9m6s/adventure-works-database-sql-server.html</link><author>noreply@blogger.com (Suprotim Agarwal)</author><thr:total>0</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2012/05/adventure-works-database-sql-server.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-8847065422018417241</guid><pubDate>Fri, 11 May 2012 02:51:00 +0000</pubDate><atom:updated>2012-05-21T08:19:07.827-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 Format Function</title><description>&lt;p align="justify"&gt;SQL Server 2012 has introduced many new functions. We will see some of them in this series.&lt;/p&gt;  &lt;p align="justify"&gt;One of the most common challenge for a SQL Developer is to format a date. In earlier versions, we had to use the CONVERT function with format style number. In SQL Server 2012, we have a new function named FORMAT which can format dates in various formats.&lt;/p&gt;  &lt;p&gt;The following codes are self explanatory&lt;/p&gt;  &lt;p&gt;&lt;font size="3" face="Courier New"&gt;declare @d datetime     &lt;br /&gt;set @d='20110119 12:33:22'&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size="3" face="Courier New"&gt;--Format date in dd-mm-yyyy format     &lt;br /&gt;select format(@d,'dd-MM-yyyy') as [dd_mm_yyyy]&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size="3" face="Courier New"&gt;--Format date in mm/dd/yyyy format     &lt;br /&gt;select format(@d,'MM/dd/yyyy') as [mm/dd/yyyy]&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size="3" face="Courier New"&gt;--Format date in mmm-yyyy format     &lt;br /&gt;select format(@d,'MMM-yyyy') as [MMM-yyyy]&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size="3" face="Courier New"&gt;--Format date in MMM dd,yyyy format     &lt;br /&gt;select format(@d,'MMM dd,yyyy') as [MMM dd,yyyy]&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size="3" face="Courier New"&gt;--Format date in HH:MM:SS format     &lt;br /&gt;select format(@d,'HH:mm:ss') as [HH:mm:ss]&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;&lt;font size="3" face="Courier New"&gt;--Format date in long format     &lt;br /&gt;select format(@d,'dddd, dd MMMM yyyy') as [long format]&lt;/font&gt;&lt;/p&gt;  &lt;p&gt;   &lt;br /&gt;All you have to do is to give the format in a specific way you want. Here’s the output&lt;/p&gt;  &lt;p&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="sql-2012-format" border="0" alt="sql-2012-format" src="http://lh4.ggpht.com/-7wILsy5MgFE/T6x-gCRv_CI/AAAAAAAAAUU/tcbXJQckmig/sql-2012-format%25255B8%25255D.png?imgmax=800" width="272" height="332" /&gt;&lt;/p&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/495177919198719500-8847065422018417241?l=www.sqlservercurry.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/T6Gn-DqGeFo" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/T6Gn-DqGeFo/sql-server-2012-format-function.html</link><author>noreply@blogger.com (Madhivanan)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh4.ggpht.com/-7wILsy5MgFE/T6x-gCRv_CI/AAAAAAAAAUU/tcbXJQckmig/s72-c/sql-2012-format%25255B8%25255D.png?imgmax=800" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2012/05/sql-server-2012-format-function.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-722185655537538742</guid><pubDate>Tue, 08 May 2012 05:58:00 +0000</pubDate><atom:updated>2012-05-07T22:58:23.668-07:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Transact SQL T-SQL</category><title>Retrieve Column Names From Temporary Table 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;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/495177919198719500-722185655537538742?l=www.sqlservercurry.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/hMDevbtEAho" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/hMDevbtEAho/retrieve-column-names-from-temporary.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/2012/05/retrieve-column-names-from-temporary.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-3754718981861915082</guid><pubDate>Thu, 26 Apr 2012 08:36:00 +0000</pubDate><atom:updated>2012-05-02T04:43:50.200-07:00</atom:updated><title>Information Schema - SQL Server vs MySQL</title><description>&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/search/label/MySQL"&gt;Continuing my series&lt;/a&gt; on how same things can be done differently in SQL Server and MySQL, in this post, we will see how Information Schema is used in SQL Server vs MySQL.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
To view information about a column for a table&amp;nbsp; or list of tables available in the database or primary and foreign key information etc. etc. we can use information_schema views like - information_schema.tables, information_schema.columns and so on. This option is available in both SQL Server and MySQL. However there are some differences in their usage.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;strong&gt;In SQL Server&lt;/strong&gt; &lt;/div&gt;
&lt;ol&gt;
&lt;li&gt;     &lt;div align="justify"&gt;
information_schema is a schema and available in all databases by default. &lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;     &lt;div align="justify"&gt;
In views like information_schema.columns, the column table_catalog will show database name and column table_schema will show the schema name. &lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;     &lt;div align="justify"&gt;
By default, it shows the result for the current database. If you want to show results for a different database, you need to qualify a database name like db_name.information_schema.columns&lt;/div&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;div align="justify"&gt;
&lt;strong&gt;In MySQL&lt;/strong&gt;&lt;/div&gt;
&lt;ol&gt;
&lt;li&gt;     &lt;div align="justify"&gt;
information_schema is a database that has information for all databases&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;     &lt;div align="justify"&gt;
In views like information_schema.columns, the column table_catalog will be NULL and column table_schema will show the database_name.&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;     &lt;div align="justify"&gt;
By default it shows the result for all the databases. If you want to show results for a specific database, you need to filter&amp;nbsp; on        &lt;br /&gt;
the column table_catalog ex WHERE table_catalog='db_name'        &lt;/div&gt;
&lt;/li&gt;
&lt;/ol&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/495177919198719500-3754718981861915082?l=www.sqlservercurry.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/b28bfp47BtQ" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/b28bfp47BtQ/information-schema-sql-server-vs-mysql.html</link><author>noreply@blogger.com (Madhivanan)</author><thr:total>1</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2012/04/information-schema-sql-server-vs-mysql.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-4486299629216043074</guid><pubDate>Thu, 19 Apr 2012 03:39:00 +0000</pubDate><atom:updated>2012-04-18T20:39:45.058-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>Bulk Insert - SQL Server vs MySQL</title><description>&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/search/label/MySQL"&gt;Continuing my series&lt;/a&gt; on how same things can be done differently in SQL Server and MySQL, in this post, we will see how bulk insert can be done in SQL Server vs MySQL.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
We often need to import data from a text file to the server. Consider that the file D:\test.txt has data for three columns and&amp;nbsp; you want to import this data into a table&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
In SQL Server we can use the BULK INSERT command&lt;/div&gt;
&lt;img alt="sqlserver-bulkinsert" border="0" height="148" src="http://lh6.ggpht.com/-m0xVBbmYZ_Q/T4-D9ALeBXI/AAAAAAAAAUM/0JX10zo2v5w/sqlserver-bulkinsert%25255B6%25255D.jpg?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="sqlserver-bulkinsert" width="265" /&gt;&lt;br /&gt;
&lt;br /&gt;
In MySQL we can user Load Data Command&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;load data local infile 'D:/test.txt' into table test      &lt;br /&gt;fields&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; escaped by '\\'       &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; terminated by '\,'       &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; lines terminated by '\n'&lt;/span&gt; &lt;br /&gt;
&lt;br /&gt;
&lt;div align="justify"&gt;
Here both of them consider comma as a field separator and new line as the line separator&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;strong&gt;Note :&lt;/strong&gt; By Default SQL Server looks for the existence of the file in Server's directory. In MySQL, we need to specify keyword local to instruct that file is available in the local system&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/495177919198719500-4486299629216043074?l=www.sqlservercurry.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/7WeOdPZcnmo" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/7WeOdPZcnmo/bulk-insert-sql-server-vs-mysql.html</link><author>noreply@blogger.com (Madhivanan)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh6.ggpht.com/-m0xVBbmYZ_Q/T4-D9ALeBXI/AAAAAAAAAUM/0JX10zo2v5w/s72-c/sqlserver-bulkinsert%25255B6%25255D.jpg?imgmax=800" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2012/04/bulk-insert-sql-server-vs-mysql.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-1764960127785732190</guid><pubDate>Thu, 12 Apr 2012 14:32:00 +0000</pubDate><atom:updated>2012-04-12T07:32:55.473-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>SET and SELECT - SQL Server Vs MySQL‏</title><description>&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/search/label/MySQL"&gt;Continuing my series&lt;/a&gt; on how same things can be done differently in SQL Server and MySQL, in this post, we will see the usage of SET and SELECT commands in SQL Server vs MySQL.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
SET and SELECT commands can be used to assign values to the variables. But the usage is different in SQL Server and MySQL. &lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
In SQL Server, SET can be used to assign a value to single variable only. SELECT command can be used to assign values to multiple variables.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
Consider the following examples&lt;/div&gt;
&lt;br /&gt;&lt;span style="font-family: Courier New; font-size: small;"&gt;Declare @a int, @b int     &lt;br /&gt;set @a=1      &lt;br /&gt;set @b=2      &lt;br /&gt;select @a,@b&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
The following will also work&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;Declare @a int, @b int     &lt;br /&gt;select @a=1,@b=2      &lt;br /&gt;select @a,@b&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-size: x-small;"&gt;OUTPUT&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;img alt="mysql-sqlserver-set-select" border="0" height="61" src="http://lh4.ggpht.com/-8oY1ZZg34cI/T4bnPxbmqyI/AAAAAAAAATc/UihL37pOpw0/mysql-sqlserver-set-select%25255B13%25255D.jpg?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="mysql-sqlserver-set-select" width="147" /&gt;&lt;br /&gt;
&lt;br /&gt;
In MySQL, declaration is not needed. Any number of variables can be assigned using a single SET command&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;set @a:=1, @b:=2;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Courier New; font-size: medium;"&gt;select @a,@b&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div align="justify"&gt;
The SELECT command can be used to assign values and select the values too. The following is equal to the previous code&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;select @a:=1, @b:=2;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
The above command assigns values to the variables and also returns values assigned&lt;br /&gt;
&lt;br /&gt;
OUTPUT&lt;br /&gt;
&lt;br /&gt;
&lt;img alt="mysql-sqlserver-set-select" border="0" height="70" src="http://lh5.ggpht.com/-GPq2_02f1gQ/T4bnQxNjw2I/AAAAAAAAATk/WcY8Dhipx7A/mysql-sqlserver-set-select%25255B6%25255D.jpg?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="mysql-sqlserver-set-select" width="157" /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/495177919198719500-1764960127785732190?l=www.sqlservercurry.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/UWxrEHohXnA" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/UWxrEHohXnA/set-and-select-sql-server-vs-mysql.html</link><author>noreply@blogger.com (Madhivanan)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh4.ggpht.com/-8oY1ZZg34cI/T4bnPxbmqyI/AAAAAAAAATc/UihL37pOpw0/s72-c/mysql-sqlserver-set-select%25255B13%25255D.jpg?imgmax=800" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2012/04/set-and-select-sql-server-vs-mysql.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-305924183329018710</guid><pubDate>Mon, 09 Apr 2012 04:02:00 +0000</pubDate><atom:updated>2012-04-08T21:02:56.834-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>Temporary Tables - SQL Server vs MySQL</title><description>&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/search/label/MySQL"&gt;Continuing my series&lt;/a&gt; on how same things can be done differently in SQL Server and MySQL, in this post, we will see temporary table support in SQL Server vs MySQL.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
We may often need to create a temporary table while processing data to provide a workspace for storing intermediate results. Both SQL Server and MySQL support temporary tables.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
In SQL Server, all temporary tables should be prefixed by the # sign&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
Consider this table&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;create table #test &lt;br /&gt;( &lt;br /&gt;id int, &lt;br /&gt;names varchar(100) &lt;br /&gt;)&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;insert into #test(id, names) &lt;br /&gt;select 1,'test'&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;select * from #test&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
We can drop this table by using a DROP command&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;DROP table #test&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
In MySQL, we have to use the keyword 'temporary' when creating a temporary table&lt;br /&gt;
&lt;br /&gt;
Consider the following code&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;create temporary table if not exists test &lt;br /&gt;( &lt;br /&gt;id int, &lt;br /&gt;names varchar(100) &lt;br /&gt;)&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;&lt;span style="font-family: Courier New; font-size: small;"&gt;insert into test(id, names) &lt;br /&gt;select 1,'test'&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;select * from test&lt;/span&gt;&lt;br /&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;The above creates a temporary table called test in the current session if it is not already available. To drop a temporary table in MySQL, we can use the following code&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;drop temporary table test &lt;/span&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/495177919198719500-305924183329018710?l=www.sqlservercurry.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/ewFPuhD-lZ4" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/ewFPuhD-lZ4/temporary-tables-sql-server-vs-mysql.html</link><author>noreply@blogger.com (Madhivanan)</author><thr:total>0</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2012/04/temporary-tables-sql-server-vs-mysql.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-5739435064876402821</guid><pubDate>Thu, 05 Apr 2012 11:53:00 +0000</pubDate><atom:updated>2012-04-05T04:53:33.539-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>Dense Rank - MySQL vs SQL Server</title><description>&lt;div align="justify"&gt;
&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;&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;&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;&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;&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; &lt;br /&gt;&lt;a href="http://www.sqlservercurry.com/search/label/MySQL" title="SQL Server vs MySQL"&gt;Continuing my series&lt;/a&gt; on how same things can be done differently in SQL Server and MySQL, in this post, we will see how to implement Dense Rank in SQL Server vs MySQL.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
Generating a dense_rank is a common requirement when showing resultsets. In SQL Server, starting from version 2005, we can make use of the dense_rank() function. Dense_rank() will generate the serial number for each set of values and keep the same number if the value is duplicated &lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
Consider the following set of data&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 test(names varchar(100))&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;insert into test &lt;br /&gt;select 'Suresh' union all &lt;br /&gt;select 'Ramesh' union all &lt;br /&gt;select 'Kant' union all &lt;br /&gt;select 'Jerald' union all &lt;br /&gt;select 'Clara' union all &lt;br /&gt;select 'Ramesh' union all &lt;br /&gt;select 'Kant' union all &lt;br /&gt;select 'Jerald' union all &lt;br /&gt;select 'John'&lt;/span&gt; &lt;br /&gt;
&lt;br /&gt;
&lt;img alt="dense-rank-data" border="0" height="225" src="http://lh5.ggpht.com/-VjQwNdybGLs/T32HdBzLyJI/AAAAAAAAATM/o12a_sI-x8U/dense-rank-data%25255B15%25255D.jpg?imgmax=800" 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="dense-rank-data" width="311" /&gt;&lt;br /&gt;
&lt;br /&gt;&lt;strong&gt;SQL Server&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div align="justify"&gt;
Using the dense_rank()&amp;nbsp; function, we can generate a serial number and reset for each name&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;select dense_rank() over (order by names) as sno,names from test&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;&lt;strong&gt;MySQL&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div align="justify"&gt;
Using a variable, we can generate the serial number, and use another variable that keeps same value for duplicates&lt;/div&gt;
&lt;br /&gt;&lt;span style="font-family: Courier New; font-size: small;"&gt;set @sno:=0; &lt;br /&gt;set @names:=''; &lt;br /&gt;select @sno:=case when @names=names then @sno else @sno+1 end as sno,@names:=names as names from test &lt;br /&gt;order by names;&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div align="justify"&gt;
In the above example, variable @sno gets incremented by 1 for each set of values thus keeping the same value for duplicates.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;img alt="result" border="0" height="222" src="http://lh6.ggpht.com/-cuQizFxUkwY/T32Hd1t5zDI/AAAAAAAAATU/fhY9gcumI7c/result%25255B13%25255D.jpg?imgmax=800" 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="result" width="185" /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/495177919198719500-5739435064876402821?l=www.sqlservercurry.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/wj0G9W2DFy4" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/wj0G9W2DFy4/dense-rank-mysql-vs-sql-server.html</link><author>noreply@blogger.com (Madhivanan)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh5.ggpht.com/-VjQwNdybGLs/T32HdBzLyJI/AAAAAAAAATM/o12a_sI-x8U/s72-c/dense-rank-data%25255B15%25255D.jpg?imgmax=800" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2012/04/dense-rank-mysql-vs-sql-server.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-6409065776131617848</guid><pubDate>Mon, 26 Mar 2012 06:51:00 +0000</pubDate><atom:updated>2012-03-25T23:51:45.292-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>Reset Row Number For Each Group - SQL Server Vs MySQL</title><description>&lt;div align="justify"&gt;
Continuing on my &lt;a href="http://www.sqlservercurry.com/2012/03/generate-row-number-sql-server-vs-mysql.html" target="_blank"&gt;SQL Server vs MySQL series&lt;/a&gt;, we will see how same things can be done differently in SQL Server and MySQL&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
Generating a row number&amp;nbsp; or a serial number and resetting it on each group is a common requirement when showing result sets. In SQL Server, starting from version 2005, we can make use of the row_number() function with the partition clause&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
Consider the following set of data&lt;/div&gt;
&lt;img alt="sql-data" border="0" height="222" src="http://lh4.ggpht.com/-l48JRzIsScw/T3AOUBYz2RI/AAAAAAAAAS8/94nYooLZprA/sql-data%25255B10%25255D.jpg?imgmax=800" 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="sql-data" width="343" /&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;SQL Server&lt;/strong&gt;&lt;br /&gt;
&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;br /&gt;
Using the row_number() function, we can generate the serial number and reset for each names &lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: 'Courier New'; font-size: small;"&gt;select row_number() over (partition by names order by names) as sno,names from test&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New'; font-size: small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;strong&gt;MySQL&lt;/strong&gt;&lt;br /&gt;
&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;br /&gt;
Using a variable, we can generate the serial number, and use another variable that resets first variable for each group&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: 'Courier New'; font-size: small;"&gt;set @sno:=0;     &lt;br /&gt;set @names:='';&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New'; font-size: small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New'; font-size: small;"&gt;OUTPUT&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New'; font-size: small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
&lt;img alt="result" border="0" height="225" src="http://lh4.ggpht.com/-cEmFtClNXhs/T3AOVANbBwI/AAAAAAAAATE/ExQ3vV4ysOI/result%25255B6%25255D.jpg?imgmax=800" 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="result" width="178" /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/495177919198719500-6409065776131617848?l=www.sqlservercurry.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/4XEx5wu2HIE" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/4XEx5wu2HIE/reset-row-number-for-each-group-sql.html</link><author>noreply@blogger.com (Madhivanan)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh4.ggpht.com/-l48JRzIsScw/T3AOUBYz2RI/AAAAAAAAAS8/94nYooLZprA/s72-c/sql-data%25255B10%25255D.jpg?imgmax=800" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2012/03/reset-row-number-for-each-group-sql.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-8877033451730744771</guid><pubDate>Mon, 12 Mar 2012 10:10:00 +0000</pubDate><atom:updated>2012-03-25T23:52:04.081-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>Generate Row Number – SQL Server vs MySQL</title><description>&lt;div align="justify"&gt;
In this series, we will see how same SQL tasks can be achieved differently in SQL Server and MySQL. Generating a row number&amp;nbsp; or a serial number is a common requirement when showing the resultsets. &lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
In SQL Server, starting from version 2005, we can make use of the row_number() function&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
Consider the following set of data&lt;/div&gt;
&lt;img alt="sql-row-number-data" border="0" height="168" src="http://lh5.ggpht.com/-D0o_xNkpJEg/T13LW9fYGRI/AAAAAAAAAS0/-iC83ytBqE0/sql-row-number-data%25255B8%25255D.jpg?imgmax=800" 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="sql-row-number-data" width="334" /&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;SQL Server&lt;/strong&gt;&lt;br /&gt;
&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;br /&gt;
Using the row_number() function we can generate a serial number as follows:&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: 'Courier New'; font-size: small;"&gt;select row_number() over (order by names) as sno,names from test&lt;/span&gt; &lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;MySQL&lt;/strong&gt;&lt;br /&gt;
&lt;strong&gt;&lt;br /&gt;&lt;/strong&gt;&lt;br /&gt;
Using a variable in MySQL, we can generate a serial number as follows:&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: 'Courier New'; font-size: small;"&gt;set @sno:=0;     &lt;br /&gt;select @sno:=@sno+1 as sno,names from test       &lt;br /&gt;order by names;&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: 'Courier New'; font-size: small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;br /&gt;
In the above example, variable @sno gets incremented by 1 for each row.&lt;br /&gt;
&lt;br /&gt;
Stay tuned for more on MySQL vs SQL Server posts.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/495177919198719500-8877033451730744771?l=www.sqlservercurry.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/Z4OoLM9Sa-I" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/Z4OoLM9Sa-I/generate-row-number-sql-server-vs-mysql.html</link><author>noreply@blogger.com (Madhivanan)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh5.ggpht.com/-D0o_xNkpJEg/T13LW9fYGRI/AAAAAAAAAS0/-iC83ytBqE0/s72-c/sql-row-number-data%25255B8%25255D.jpg?imgmax=800" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2012/03/generate-row-number-sql-server-vs-mysql.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-5530988409542286880</guid><pubDate>Wed, 07 Mar 2012 10:32:00 +0000</pubDate><atom:updated>2012-03-07T11:54:27.511-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">SQL Server Administration</category><category domain="http://www.blogger.com/atom/ns#">SQL Server Training</category><title>Common Myths Around SQL Server Best Practices</title><description>&lt;div align="justify"&gt;
Idera has a bunch of &lt;a href="http://bit.ly/x3R2yc" target="_blank" title="Free SQL Server Webcasts"&gt;&lt;strong&gt;Free SQL Server Webcasts&lt;/strong&gt;&lt;/a&gt; that you can watch and learn from. One that particularly caught my attention was the webcast about &lt;em&gt;SQL Server Myths Debunked…Or Are They?&lt;/em&gt;&amp;nbsp;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
Numerous SQL Server experts have done a great job of debunking many of the common myths that crop up around SQL Server best practices and usage. However in some cases, the debunking of those myths creates unintended consequences – we ignore the underlying truth which started the myth in the first place.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
SQL Server expert Michael K. Campbell does a survey of misperceptions that have arisen from commonly debunked SQL Server myths. This free webcast shows you how shrinking and using multiple files can be a best practice, what AWE and locking files in memory really means, along with other best practices that you may be missing out on because of misconceptions.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
In addition to this webcast, I recommend the following webcasts:&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;     &lt;div align="justify"&gt;
What Are You Waiting For?
&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;     &lt;div align="justify"&gt;
Putting a Better SQL Server in Production&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;     &lt;div align="justify"&gt;
Under The Hood with SQL Server Fundamentals&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;     &lt;div align="justify"&gt;
Learning to Hate the SMO: A PowerShell Love Story&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;     &lt;div align="justify"&gt;
TempDB: Performance and Manageability&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;     &lt;div align="justify"&gt;
5 Common High-Availability Mistakes&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;     &lt;div align="justify"&gt;
Common SQL Server Security Mistakes&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;     &lt;div align="justify"&gt;
Top 10 SQL Server Backup Mistakes and How to Avoid Them&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;div align="justify"&gt;
Check out all these &lt;a href="http://bit.ly/x3R2yc" target="_blank" title="Free SQL Server Webcasts"&gt;Free SQL Server Webcasts Over Here&lt;/a&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/495177919198719500-5530988409542286880?l=www.sqlservercurry.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/icOlXboqPIQ" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/icOlXboqPIQ/common-myths-around-sql-server-best.html</link><author>noreply@blogger.com (Suprotim Agarwal)</author><thr:total>0</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2012/03/common-myths-around-sql-server-best.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-8072376990208454364</guid><pubDate>Thu, 01 Mar 2012 01:48:00 +0000</pubDate><atom:updated>2012-02-29T17:48:49.143-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>Find out Long Running query using sp_who2 with Dynamic Management Views‏ (DMV)</title><description>&lt;div align="justify"&gt;
There are many ways to find out why &lt;a href="http://www.sqlservercurry.com/2011/03/sql-server-identify-memory-and.html" title="SQL Server Memory and Performance Issues"&gt;your SQL server is performing slow&lt;/a&gt;. Suppose you know that your server is responding slowly while executing queries and you want to find out the query that takes a lot of time to execute. If you want &lt;a href="http://www.sqlservercurry.com/2010/07/find-most-time-consuming-code-in-your.html" title="Most Time Consuming Code in your SQL Server Database"&gt;to find out slow queries, you can use dynamic management views&lt;/a&gt; from version 2005 onwards. However you can still use the old system stored procedure sp_who2 along with management views, to find this info.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
Run the following code&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;span style="font-family: 'Courier New'; font-size: small;"&gt;EXEC sp_who2&lt;/span&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;span style="font-family: 'Courier New'; font-size: small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
and see the result shown below. Find out rows with runnable status with highest CPUTime.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;img alt="long_run1" border="0" height="294" src="http://lh5.ggpht.com/-cyAUoM12i1g/T07U9PxWBvI/AAAAAAAAASs/kKAXBk1l-4o/long_run1%25255B12%25255D.jpg?imgmax=800" 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="long_run1" width="590" /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
Sp_who2 will not display the text of the query, so we need to find out the text using &lt;span style="font-family: 'Courier New'; font-size: small;"&gt;dm_exec_requests&lt;/span&gt; and &lt;span style="font-family: 'Courier New'; font-size: small;"&gt;dm_exec_sql_text&lt;/span&gt; management views. The view &lt;span style="font-family: 'Courier New'; font-size: small;"&gt;dm_exec_requests &lt;/span&gt;will show the text of the query. All we need to do is to find out spid from the result of &lt;span style="font-family: 'Courier New'; font-size: small;"&gt;sp_who2&lt;/span&gt; and filter it in &lt;span style="font-family: 'Courier New'; font-size: small;"&gt;dm_exec_requests&lt;/span&gt; using session_id column. The following code displays the text of the query.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;span style="font-family: 'Courier New'; font-size: small;"&gt;SELECT      &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sql.text AS statement_text      &lt;br /&gt;FROM       &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sys.dm_exec_requests&amp;nbsp; AS req      &lt;br /&gt;CROSS APPLY sys.dm_exec_sql_text(req.sql_handle) as sql      &lt;br /&gt;WHERE       &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; req.session_id=52&lt;/span&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;span style="font-family: 'Courier New'; font-size: small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
Similarly you can also use dynamic management views to find out various root causes for performance issues.   &lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/495177919198719500-8072376990208454364?l=www.sqlservercurry.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/GcbBt8G40e4" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/GcbBt8G40e4/find-out-long-running-query-using.html</link><author>noreply@blogger.com (Madhivanan)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh5.ggpht.com/-cyAUoM12i1g/T07U9PxWBvI/AAAAAAAAASs/kKAXBk1l-4o/s72-c/long_run1%25255B12%25255D.jpg?imgmax=800" height="72" width="72" /><thr:total>1</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2012/02/find-out-long-running-query-using.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-7573357691522800741</guid><pubDate>Wed, 15 Feb 2012 05:17:00 +0000</pubDate><atom:updated>2012-02-14T21:17:46.524-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">SQL Server Administration</category><title>Using sys.dm_os_performance_counters to measure Transactions Per Second</title><description>&lt;div align="justify"&gt;
Performance Monitor tool in SQL Server represents aspects of system performance, such as CPU Usage % , Memory Paging etc. The sys.dm_os_performance_counters Dynamic Management View exposes data of all performance counters for the particular instance of SQL Server. This view is extremely useful when you have to present the performance data of your database in your dashboard.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;em&gt;Note: If you are handling multiple databases, check out the handy &lt;a href="https://www.idera.com/SQL-Server/SQL-diagnostic-manager/?s=BN300_curry" target="_blank" title="SQL Diagnostics"&gt;&lt;strong&gt;SQL Diagnostic Manager tool&lt;/strong&gt;&lt;/a&gt; from Idera which monitors and manage your entire SQL Server environment from a single console&lt;/em&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;em&gt;&lt;br /&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
We have touched upon the usefulness of the &lt;code&gt;sys.dm_os_performance_counters &lt;/code&gt;in my previous post &lt;a href="http://www.sqlservercurry.com/2011/09/sql-server-removing-deprecated-code-and.html"&gt;Removing Deprecated Code and Future Proofing your Queries&lt;/a&gt;. In this post, we will see another use of the sys.dm_os_performance_counters view to measure SQL transactions per second for a database. Here’s a very handy query written by Joe Stefanelli&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;span style="font-family: 'Courier New'; font-size: small;"&gt;DECLARE @ctr bigint&lt;/span&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;span style="font-family: 'Courier New'; font-size: small;"&gt;SELECT @ctr = ctr     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; FROM sys.dm_os_performance_counters      &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; WHERE counter_name = 'transactions/sec'      &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; AND object_name = 'SQLServer:Databases'      &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; AND instance_name = 'SomeDBName'&lt;/span&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;span style="font-family: 'Courier New'; font-size: small;"&gt;WAITFOR DELAY '00:00:01'&lt;/span&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;span style="font-family: 'Courier New'; font-size: small;"&gt;SELECT ctr - @ctr     &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; FROM sys.dm_os_performance_counters      &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; WHERE counter_name = 'transactions/sec'      &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; AND object_name = 'SQLServer:Databases'      &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; AND instance_name = 'DotNetCurry2'&lt;/span&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;span style="font-family: 'Courier New'; font-size: small;"&gt;&lt;br /&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
As you can see, were taking the difference of two values for a delay of one second to get the number of transactions per second. In future posts, we will see some more queries using this useful DMV.&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/495177919198719500-7573357691522800741?l=www.sqlservercurry.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/eZ0g_eBIuNQ" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/eZ0g_eBIuNQ/using-sysdmosperformancecounters-to.html</link><author>noreply@blogger.com (Suprotim Agarwal)</author><thr:total>0</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2012/02/using-sysdmosperformancecounters-to.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-2314436367757864281</guid><pubDate>Tue, 07 Feb 2012 04:38:00 +0000</pubDate><atom:updated>2012-02-06T20:38:35.806-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">SQL Server Training</category><title>SQL Server 2012 Free Cook Book and Training</title><description>Microsoft has created some free content designed to get started with SQL Server 2012.&amp;nbsp; You can use the SQL Server 2012 Early Adoption Cook Book Wiki at &lt;a href="http://social.technet.microsoft.com/wiki/contents/articles/6967.sql-server-2012-early-adoption-cook-book.aspx"&gt;http://social.technet.microsoft.com/wiki/contents/articles/6967.sql-server-2012-early-adoption-cook-book.aspx&lt;/a&gt;. You can bookmark this link as Microsoft to update this wiki with the latest SQL Server 2012 information as and when it is available.&lt;br /&gt;
&lt;br /&gt;
Microsoft also released the SQL Server 2012 Developer Training Kit which includes technical content including labs, demos and presentations designed to help you learn how to develop SQL Server 2012 database and BI solutions.&lt;br /&gt;
&lt;br /&gt;
You can download &lt;a href="http://www.microsoft.com/download/en/details.aspx?id=27721" target="_blank" title="SQL Server 2012 Training Kit"&gt;SQL Server 2012 Training Kit over here&lt;/a&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/495177919198719500-2314436367757864281?l=www.sqlservercurry.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/4Rwpryg7VIg" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/4Rwpryg7VIg/sql-server-2012-free-cook-book-and.html</link><author>noreply@blogger.com (Suprotim Agarwal)</author><thr:total>1</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2012/02/sql-server-2012-free-cook-book-and.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-3030232659417967469</guid><pubDate>Mon, 23 Jan 2012 07:51:00 +0000</pubDate><atom:updated>2012-01-25T09:53:27.539-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Transact SQL T-SQL</category><title>Declaring Length for VARCHAR and NVARCHAR datatypes IS Necessary</title><description>&lt;div align="justify"&gt;
I have seen some new SQL Server developers declaring Varchar and Nvarchar data types without specifying a length, since it is optional. Although this works in some other programming languages like C#, SQL Server behaves differently and this is a bad practice overall. Let us see with an example of what’s wrong with not declaring a length for the Char or Varchar data type.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
Consider the following t-sql code&lt;br /&gt;
&lt;br /&gt;
&lt;img alt="varchar-length" border="0" height="163" src="http://lh6.ggpht.com/-ChoWCkj9twU/Tx0Qgfxf-ZI/AAAAAAAACZA/eMb7KmtFdxs/varchar-length%25255B2%25255D.jpg?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="varchar-length" width="454" /&gt;&lt;br /&gt;
&lt;br /&gt;
OUTPUT&lt;br /&gt;
&lt;br /&gt;
&lt;img alt="image" border="0" height="75" src="http://lh3.ggpht.com/-AK1PKuO6axI/Tx0QhyZWZXI/AAAAAAAACZI/WY9lQaatW2s/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="266" /&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div align="justify"&gt;
Surprised seeing the output? When a length is not specified while declaring a variable, the default length is 1 and when a length is not specified when using the CAST and CONVERT functions, the default length is 30. That’s the reason you get the output you see above.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;strong&gt;Note:&lt;/strong&gt; Try doing the same when you create a table with a CHAR column, without specifying the length.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
To get the desired results, rewrite the t-sql code in the following manner:&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;DECLARE @v1 AS char(30)     &lt;br /&gt;DECLARE @v2 AS varchar(60)      &lt;br /&gt;SET @v1 = 'abcdefghijkl'      &lt;br /&gt;SET @v2 = 'The quick brown fox jumped over the lazy dog'&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;SELECT      &lt;br /&gt;DATALENGTH(@v2),      &lt;br /&gt;DATALENGTH(CAST(@v2 as varchar(60)))      &lt;br /&gt;GO&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
Now you get the desired results&lt;br /&gt;
&lt;br /&gt;
&lt;img alt="image" border="0" height="79" src="http://lh3.ggpht.com/-LsDuF-kR4ns/Tx0QjEN9soI/AAAAAAAACZQ/lxq5XaXNS7g/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="image" width="280" /&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div align="justify"&gt;
Another place where you got to be careful is while creating stored procedures with parameters. If you have created a stored procedure that accepts a parameter with a VARCHAR datatype with no length, you will be in for a surprise to learn that SQL Server silently truncates the string and adds some leading characters.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
Overall, &lt;strong&gt;always&lt;/strong&gt; specify a length for the CHAR, VARCHAR, NVARCHAR and similar data types. It’s a good practice!&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/495177919198719500-3030232659417967469?l=www.sqlservercurry.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/hg4BVXW7Qs0" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/hg4BVXW7Qs0/declaring-length-for-varchar-and.html</link><author>noreply@blogger.com (Suprotim Agarwal)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh6.ggpht.com/-ChoWCkj9twU/Tx0Qgfxf-ZI/AAAAAAAACZA/eMb7KmtFdxs/s72-c/varchar-length%25255B2%25255D.jpg?imgmax=800" height="72" width="72" /><thr:total>3</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2012/01/declaring-length-for-varchar-and.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-5356567576078500699</guid><pubDate>Mon, 09 Jan 2012 11:33:00 +0000</pubDate><atom:updated>2012-01-09T03:36:59.519-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">SQL Server Administration</category><title>Restoring SQL Server Database: Points to Consider</title><description>&lt;div style="text-align: justify;"&gt;
Here are some important points to consider while restoring a database backup. Let us first create a sample database named test using the following code&lt;/div&gt;
&lt;br /&gt;
&lt;img alt="sql-restore" border="0" height="90" src="http://lh5.ggpht.com/-mgZZihAmCjE/TwrQnjwI24I/AAAAAAAAASk/W-944TfD-14/sql-restore%25255B8%25255D.jpg?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="sql-restore" width="438" /&gt;&lt;br /&gt;
&lt;br /&gt;
A new database named &lt;em&gt;test&lt;/em&gt; will be created in your server and data and log files are created&amp;nbsp;in the H: drive.&lt;br /&gt;
&lt;br /&gt;
Let us assume that you regularly take backup of this database using the below code:&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;backup database test to disk='h:\test.bak'&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div style="text-align: justify;"&gt;
Now if you want to create another database or restore this &lt;em&gt;test&lt;/em&gt; database to a new database named &lt;em&gt;testing, y&lt;/em&gt;ou can use the following code&lt;/div&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;restore database testing from disk='h:\test.bak'&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
However executing the code above will give you the following error&lt;br /&gt;
&lt;br /&gt;
&lt;span style="color: red;"&gt;Msg 1834, Level 16, State 1, Line 2     &lt;br /&gt;The file 'h:\test_dat.mdf' cannot be overwritten.&amp;nbsp; It is being used by database 'test'.      &lt;br /&gt;Msg 3156, Level 16, State 4, Line 2      &lt;br /&gt;File 'test_dat' cannot be restored to 'h:\test_dat.mdf'. Use WITH MOVE to identify a valid location for the file.      &lt;br /&gt;Msg 1834, Level 16, State 1, Line 2      &lt;br /&gt;The file 'h:\test_log.ldf' cannot be overwritten.&amp;nbsp; It is being used by database 'test'.      &lt;br /&gt;Msg 3156, Level 16, State 4, Line 2      &lt;br /&gt;File 'test_log' cannot be restored to 'h:\test_log.ldf'. Use WITH MOVE to identify a valid location for the file.      &lt;br /&gt;Msg 3119, Level 16, State 1, Line 2      &lt;br /&gt;Problems were identified while planning for the RESTORE statement. Previous messages provide details.      &lt;br /&gt;Msg 3013, Level 16, State 1, Line 2      &lt;br /&gt;RESTORE DATABASE is terminating abnormally.&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div style="text-align: justify;"&gt;
This error occurs because the data and log files are currently being used by the database &lt;em&gt;test. &lt;/em&gt;So you need to give different names for those files while restoring, as shown below&lt;/div&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;restore database testing from disk='h:\test.bak'     &lt;br /&gt;with       &lt;br /&gt;move 'test_dat' to 'h:\testing.mdf',      &lt;br /&gt;move 'test_log' to 'h:\testing.ldf'&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
The above code will work fine and new database will be created with the name testing.&lt;br /&gt;
&lt;br /&gt;
Just be aware of this point while restoring a backup of existing database!&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/495177919198719500-5356567576078500699?l=www.sqlservercurry.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/OH27OZ_u6NA" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/OH27OZ_u6NA/restoring-sql-server-database-points-to.html</link><author>noreply@blogger.com (Madhivanan)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh5.ggpht.com/-mgZZihAmCjE/TwrQnjwI24I/AAAAAAAAASk/W-944TfD-14/s72-c/sql-restore%25255B8%25255D.jpg?imgmax=800" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2012/01/restoring-sql-server-database-points-to.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-4953101097210098380</guid><pubDate>Thu, 29 Dec 2011 17:50:00 +0000</pubDate><atom:updated>2011-12-29T09:52:26.522-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><category domain="http://www.blogger.com/atom/ns#">Link List</category><title>Most Popular SQL Server Articles in 2011</title><description>&lt;div align="justify"&gt;
With 2012 fast approaching and 2011 drawing to an end, we've put together our list of the Most Popular SQL Server articles on SQLServerCurry.com this year. &lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
I would like to thank each one of you who has visited my blog or contributed to it by submitting a Guest post, &lt;a href="http://feeds2.feedburner.com/sqlservercurry/blog" title="Subscribe to RSS"&gt;Subscribing to RSS Feed&lt;/a&gt;, by &lt;a href="http://twitter.com/suprotimagarwal"&gt;joining me on Twitter&lt;/a&gt;, retweeting posts or promoting the articles and giving regular feedbacks via rating, comments or Emails. A special note of thanks to Madhivannan and Pravin Kumar for their contributions.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
Here are some articles that were liked the most by readers like you. Have a very Happy New Year 2012!&lt;/div&gt;
&lt;h4 align="justify"&gt;
SQL Server Administration Articles&lt;/h4&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/12/why-is-raid-important-for-databases.html"&gt;Why is RAID So Important for Databases?&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/09/sql-server-removing-deprecated-code-and.html"&gt;SQL Server: Removing Deprecated Code and Future Proofing your Queries&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/08/net-developer-database-knowledge.html"&gt;Every .NET Developer Should Know About the Database they are working with&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/08/sql-server-2008-move-data-to-different.html"&gt;SQL Server: Move Data to a Different Table using OUTPUT clause&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/08/localdb-denali-new-version-sql-express.html"&gt;LocalDB Denali: New version of SQL Express&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/07/sql-server-top-10-cached-queries.html"&gt;SQL Server: Top 10 Cached Queries&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/05/troubleshoot-deadlocks-using-sql-server.html"&gt;Troubleshoot Deadlocks using SQL Server Profiler 2005/2008&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/04/monitor-running-processes-in-sql-server.html"&gt;Monitor Running Processes in SQL Server 2005/2008&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/03/sql-server-move-table-to-new-file-group.html"&gt;SQL Server: Move Table to a new File Group&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/03/sql-server-identify-memory-and.html"&gt;SQL Server: Identify Memory and Performance Issues in T-SQL Queries and Fix them&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/03/repair-sql-server-database-marked-as.html"&gt;Repair SQL Server Database marked as Suspect or Corrupted&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/02/fastest-way-to-update-rows-in-large.html"&gt;Fastest Way to Update Rows in a Large Table in SQL Server&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/01/sql-server-export-table-to-csv.html"&gt;SQL Server: Export Table to CSV&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/01/load-comma-delimited-file-csv-in-sql.html"&gt;Load Comma Delimited file (csv) in SQL Server&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/01/backup-compression-in-sql-server-2008.html"&gt;Backup Compression in SQL Server 2008&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h4 align="justify"&gt;
T-SQL Articles&lt;/h4&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/07/sql-server-bit-data-type-and-how-it.html"&gt;SQL Server Bit Data Type and how it Stores Values&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/07/sql-server-datetime-vs-datetime2.html"&gt;SQL Server: DateTime vs DateTime2&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/06/sql-server-tutorials-on-date-time.html"&gt;SQL Server Tutorials on Date Time&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/06/sql-server-row-summary-column-summary.html"&gt;SQL Server: Calculate Summary and Column Summary&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/06/concatenate-strings-in-sql-server.html"&gt;Concatenate Strings in SQL Server - Different ways&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/05/sql-server-clr-user-defined-function.html"&gt;SQL Server CLR User Defined Function using Visual Studio 2010&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/05/sql-queries-beyond-true-and-false.html"&gt;SQL Queries – beyond TRUE and FALSE&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/05/sql-clr-stored-procedure-using-visual.html"&gt;SQL CLR Stored Procedure using Visual Studio 2010&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/04/sql-server-search-similar-string-in.html"&gt;SQL Server: Search Similar String in a Table&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/03/sql-server-count-based-on-condition.html"&gt;SQL Server: Count based on Condition&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/03/sql-server-insert-date-and-time-in.html"&gt;SQL Server: Insert Date and Time in Separate Columns&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/03/sql-server-combine-multiple-rows-into.html"&gt;SQL Server: Combine Multiple Rows Into One Column with CSV output&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/02/sql-server-first-and-last-sunday-of.html"&gt;SQL Server: First and Last Sunday of Each Month&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/02/sql-server-group-by-year-month-and-day.html"&gt;SQL Server: Group By Year, Month and Day&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/02/sql-server-return-multiple-values-from.html"&gt;SQL Server: Return Multiple Values from a Function&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/01/unpivot-example-in-sql-server.html"&gt;UNPIVOT example in SQL Server&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/01/date-difference-in-sql-server-in-days.html"&gt;Date Difference in SQL Server in Days, Hours, Minutes and Seconds&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/01/minus-keyword-in-sql-server.html"&gt;MINUS Keyword in SQL Server – Alternatives&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/01/rollback-nested-transactions-in-stored.html"&gt;Rollback Nested Transactions in Stored Procedure - SQL Server&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/01/rollback-transaction-in-sql-server.html"&gt;Rollback Transaction in SQL Server&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.sqlservercurry.com/2011/01/select-top-with-ties-in-sql-server.html"&gt;Select TOP With TIES in SQL Server&lt;/a&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/495177919198719500-4953101097210098380?l=www.sqlservercurry.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/EMI4TEFMTNI" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/EMI4TEFMTNI/most-popular-sql-server-articles-in.html</link><author>noreply@blogger.com (Suprotim Agarwal)</author><thr:total>0</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2011/12/most-popular-sql-server-articles-in.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-804256898466864769</guid><pubDate>Sat, 24 Dec 2011 09:47:00 +0000</pubDate><atom:updated>2011-12-24T01:51:16.406-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Transact SQL T-SQL</category><title>Assign Result of Dynamic SQL to a Variable in SQL Server</title><description>&lt;div align="justify"&gt;
Suppose you have dynamic sql that returns a single value and you want to copy it to a variable. For eg: you want the total count of the table copied to a variable. This is possible in SQL Server using the following methods:&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;strong&gt;Method 1 : Use sp_executesql system stored procedure&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;declare @counting int&lt;/span&gt;&lt;br /&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;execute sp_executesql      &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; N'select @count=count(*) from sys.objects',      &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; N'@count int output',      &lt;br /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; @count =@counting output;      &lt;br /&gt;select @counting as counting&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
In the above method, count is assigned to the variable @count which outputs to @counting&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;Method 2 : Use table variable&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;declare @t table(counting int)     &lt;br /&gt;declare @counting int      &lt;br /&gt;insert into @t       &lt;br /&gt;exec('select count(*) from sys.objects')      &lt;br /&gt;select @counting=counting from @t       &lt;br /&gt;select @counting as counting&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div align="justify"&gt;
The above t-sql code copiesthe&amp;nbsp; resultset to table variable @t and the count is copied to variable @counting&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
OUTPUT   &lt;br /&gt;
&lt;br /&gt;
&lt;img alt="dynamic-sql-variable" border="0" height="96" src="http://lh4.ggpht.com/-ou7bA6mAYNU/TvWftKqSh7I/AAAAAAAAASc/2kBn58s_yV0/dynamic-sql-variable%25255B6%25255D.jpg?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="dynamic-sql-variable" width="217" /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/495177919198719500-804256898466864769?l=www.sqlservercurry.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/dFSSpLVssUs" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/dFSSpLVssUs/sql-server-dynamic-sql-variable.html</link><author>noreply@blogger.com (Madhivanan)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh4.ggpht.com/-ou7bA6mAYNU/TvWftKqSh7I/AAAAAAAAASc/2kBn58s_yV0/s72-c/dynamic-sql-variable%25255B6%25255D.jpg?imgmax=800" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2011/12/sql-server-dynamic-sql-variable.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-420890892425818004</guid><pubDate>Mon, 19 Dec 2011 08:08:00 +0000</pubDate><atom:updated>2011-12-19T00:24:42.381-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">SQL Server Administration</category><title>Why is RAID So Important for Databases?</title><description>&lt;div align="justify"&gt;
A good server design has no, or very few, single points of failure. One of the most common server component that fails, are disks. So data redundancy becomes essential to recoverability. &lt;em&gt;Redundant Array of Independent/Inexpensive Disks (RAID)&lt;/em&gt; is a disk system that provides better fault-tolerance by making use of redundancy of disk(s).&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;em&gt;RAID is a disk system that contains multiple disk drives, called an array, to provide greater performance, fault tolerance, storage capacity, at a moderate cost. While configuring your server system, you typically have to make a choice between hardware RAID and software RAID for the server’s internal disk drives&lt;/em&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;img alt="RAID System" border="0" height="311" src="http://lh6.ggpht.com/-MTz5YJrfO3I/Tu7w-cbKrWI/AAAAAAAACYE/oay_6w3Wujc/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="RAID System" width="393" /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
RAID systems are widely used as storage solutions to get the best I/O performance, depending&amp;nbsp;whether the application is write intensive or read intensive. DBA RayRankins mentions in his book that for database-related applications, in order to minimize disk head movement and maximize I/O performance, it’s a good practice to spread random I/O’s (data changes) and sequential I/O’s (for the transaction log) across different disk subsystems. I agree and endorse his view, as SQL Server, or for that matter any other database, is very much an I/O intensive system.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;em&gt;&lt;strong&gt;I will be using SQL Server as an example to explain the importance of RAID in databases, however you can implement these concepts in your choice of database. The concepts more or less, remain the same&lt;/strong&gt;&lt;/em&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
Although RAID is not a part of a database like SQL Server, implementing RAID can directly affect the way SQL Server performs.There are many RAID arrays available such as RAID 0, RAID 1, RAID 3, RAID 4, RAID 5, RAID 6, RAID 10 and RAID 01. In this article, we will discuss the ones you will likely encounter as a SQL Server DBA&amp;nbsp;i.e. RAID levels 0, 1, 5 and 10 and also&amp;nbsp; discuss their advantages and disadvantages from a fault tolerance and performance perspective&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;em&gt;Note: RAID is not a replacement for backups. Backups are very essential for any system.&lt;/em&gt;&lt;/div&gt;
&lt;h4 align="justify"&gt;

Different RAID Levels (Advantages and Disadvantages)&lt;/h4&gt;
&lt;div align="justify"&gt;
We will discussing only RAID 0, 1, 5 and 10 (database perspective)&amp;nbsp;. &lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;strong&gt;RAID 0 – &lt;/strong&gt;Also known as Disk Striping, RAID 0 does not provide redundancy or fault tolerance but instead writes data to two drives, in an alternating fashion. This provides the best read write I/O performance. If you had 8 chunks of data, for example, chunk 1, 3, 5, and 7 would be written to the first drive, and chunk 2, 4, 6, and 8 would be written to the second drive, but all in a fixed (sequential) order. RAID 0 has a simple design, easier to implement and no overheads for parity. The drawback is that any piece of data is on only one disk, so if one disk fails, data stored within those disks are lost.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;strong&gt;RAID 1&lt;/strong&gt;- Also known as Disk Mirroring, RAID 1 provides a redundant, identical copy of a selected disk and thus provides good fault tolerance. It can be implemented with 2 drives. The disadvantage is that it has a big storage overhead and a high cost/capacity ratio&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;strong&gt;RAID 5&lt;/strong&gt; - Also known as Disk Striping with Parity, stripes data across multiple drives and writes parity bits across drives. Data redundancy is provided by the parity information. It can be implemented with 3 or more disks and is a popular choice amongst DBA’s. Since data and parity information are arranged on the disk array, two types of information are always on different disks. If one disk fails, just replace it with a new disk and the array rebuilds itself. RAID 5 has a higher read rate and makes good use of capacity. The drawback of RAID 5 is slower write rates and slow rebuild times.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;strong&gt;RAID 10&lt;/strong&gt; - Also known as mirroring with striping, RAID 10 is a combination of RAID1 + RAID0. RAID 10 uses a striped array of disks that are then mirrored to another identical set of striped disks. This array level uses at least four hard disks and additional disks must be added in even numbers. The data is first placed into mirrored pairs at the lower level. Next, the controller selects a member from each mirrored pair and stripes the data into a new logical volume. Since RAID 10 writes in a random fashion, it provides best performance with a write-intensive application (like video editing). The drawback is that it is expensive.&lt;/div&gt;
&lt;h4 align="justify"&gt;

Which RAID is Suitable for my Database?&lt;/h4&gt;
&lt;div align="justify"&gt;
Now that you have an overview of RAID levels, let’s look at which RAID is suitable for a database. The answer to this question depends on a variety of factors. Do you want availability, performance or cost? What are your requirements for fault tolerance and performance? Here’s a quick snapshot of the performance and fault tolerance provided by RAID&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;strong&gt;&lt;img alt="sqlserver-raid-performance" border="0" height="125" src="http://lh3.ggpht.com/-fVgEGyw3YsE/Tu7w_m--jzI/AAAAAAAACYM/T1nfXm2n9do/sqlserver-raid-performance%25255B4%25255D.png?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="sqlserver-raid-performance" width="554" /&gt;&lt;/strong&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
When it comes to a database like SQL Server, no one RAID level will suit your need. In most cases, SQL Server performs large reads and small writes. So for databases, where write operations are more, RAID 5 is not a good choice. On the contrary, RAID 10 is a good option for databases with more write operations. &lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
Here are some points and best practices to keep in mind while deciding the RAID system for your database. &lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;     &lt;div align="justify"&gt;
Your Data, Logs, tempdb, and backups should be on separate physical drives or a set of disks (array).&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;     &lt;div align="justify"&gt;
RAID1 is often chosen to store operating systems, binaries, index file groups and database transaction log files. Critical to log and index performance is fault tolerance and a good write speed. &lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;     &lt;div align="justify"&gt;
Since log file are written sequentially and are read only for recovery operations, recommended RAID for Log Files is RAID 1 or 10. If your RAID 1 is at a 100% usage, choose RAID 10 for better performance.&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;     &lt;div align="justify"&gt;
For data files with random access and read heavy data volumes, striping is important. So recommended RAID is 5 or 10. &lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;     &lt;div align="justify"&gt;
For data files which require good write performance, RAID 10 is recommended. Use a battery backed caching RAID controller for better write performance&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;     &lt;div align="justify"&gt;
For tempdb files with good read/write performance RAID 0, 1 or 10 is recommended. Although tempdb contains temporary data and DBA’s often go in for RAID 0 for tempdb, keep in mind that SQL Server requires tempdb to carry out many of its activities. So if you want your system to be always available, rethink RAID 0.&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;     &lt;div align="justify"&gt;
Putting log files or tempdb on a RAID 5 array is not recommended, since RAID 5 does not perform well for write operations. DBA's although have conflicting opinions over this point.&lt;/div&gt;
&lt;/li&gt;
&lt;li&gt;     &lt;div align="justify"&gt;
Choose small fast drives, over large slow drives.&lt;/div&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;div align="justify"&gt;
Make sure you do your own research, so you can make an informed decision! In a real scenario, you may not have the luxury of deciding your server configurations due to the cost associated with it. So keeping these points in mind, may help you in such scenarios.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
Please use the comments section and share your opinions!&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/495177919198719500-420890892425818004?l=www.sqlservercurry.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/kjdS3uoPZus" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/kjdS3uoPZus/why-is-raid-important-for-databases.html</link><author>noreply@blogger.com (Suprotim Agarwal)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh6.ggpht.com/-MTz5YJrfO3I/Tu7w-cbKrWI/AAAAAAAACYE/oay_6w3Wujc/s72-c/image%25255B2%25255D.png?imgmax=800" height="72" width="72" /><thr:total>1</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2011/12/why-is-raid-important-for-databases.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-8474565477060713963</guid><pubDate>Wed, 14 Dec 2011 09:58:00 +0000</pubDate><atom:updated>2011-12-14T01:58:58.981-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">Transact SQL T-SQL</category><title>Deleting Leading Zeros in a String (SQL)</title><description>A common issue while importing data from different source into SQL Server is often numbers get prefixed with zeroes. If you want to delete these leading zeroes in a string, you can use the following methods:&lt;br /&gt;
&lt;br /&gt;&lt;strong&gt;Method 1 : CAST to Bigint&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;declare @s varchar(100)     &lt;br /&gt;set @s ='0000004007340007402100'       &lt;br /&gt;select cast(@s as bigint)&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
CASTing the string to Bigint will automatically delete the leading zeroes&lt;br /&gt;
&lt;br /&gt;
&lt;img alt="sql-delete-zeroes" border="0" height="88" src="http://lh4.ggpht.com/-VtppH5HkDxc/TuhzJ-0QS3I/AAAAAAAAASM/mvSzBym1bJ4/sql-delete-zeroes%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-delete-zeroes" width="202" /&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;Method 2 : Use Replace function&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;span style="font-family: Courier New; font-size: small;"&gt;declare @s varchar(100)     &lt;br /&gt;set @s ='0000004007340007402100'       &lt;br /&gt;select replace(ltrim(replace(@s,'0',' ')),' ','0')&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
The T-SQL code shown above first replaces all zeroes to a single space. The LTRIM function deletes all leading spaces and the second replace function, replaces all spaces to zeroes, so that all leading zeroes are deleted.&lt;br /&gt;
&lt;br /&gt;
OUTPUT&lt;br /&gt;
&lt;br /&gt;
&lt;img alt="sql-delete-zeroes" border="0" height="88" src="http://lh5.ggpht.com/-vdS3KdIXhjY/TuhzKpPCW_I/AAAAAAAAASU/BFIweufVYOg/sql-delete-zeroes%25255B12%25255D.png?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="sql-delete-zeroes" width="202" /&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/495177919198719500-8474565477060713963?l=www.sqlservercurry.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/sGp_vh9J7xk" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/sGp_vh9J7xk/deleting-leading-zeros-in-string-sql.html</link><author>noreply@blogger.com (Madhivanan)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh4.ggpht.com/-VtppH5HkDxc/TuhzJ-0QS3I/AAAAAAAAASM/mvSzBym1bJ4/s72-c/sql-delete-zeroes%25255B8%25255D.png?imgmax=800" height="72" width="72" /><thr:total>2</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2011/12/deleting-leading-zeros-in-string-sql.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-4542080520258343430</guid><pubDate>Fri, 09 Dec 2011 09:00:00 +0000</pubDate><atom:updated>2011-12-09T01:15:42.795-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>Find Last Run Query in SQL Server</title><description>&lt;div align="justify"&gt;
Have you ever wondered what SQL query was last executed by your users across all SQL Server databases on your server? I have seen some solutions on the internet that use the sysprocesses view to retrieve this information. In this post, I will show you how this information can be retrieved better using &lt;a href="http://www.sqlservercurry.com/2010/12/list-dynamic-management-views-dmv-by.html"&gt;Dynamic Management Views&lt;/a&gt;.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
Please use this query:&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;pre class="code"&gt;&lt;span style="color: blue;"&gt;SELECT &lt;/span&gt;conn&lt;span style="color: grey;"&gt;.&lt;/span&gt;session_id&lt;span style="color: grey;"&gt;, &lt;/span&gt;sson&lt;span style="color: grey;"&gt;.&lt;/span&gt;&lt;span style="color: magenta;"&gt;host_name&lt;/span&gt;&lt;span style="color: grey;"&gt;, &lt;/span&gt;sson&lt;span style="color: grey;"&gt;.&lt;/span&gt;login_name&lt;span style="color: grey;"&gt;, 
 &lt;/span&gt;sqltxt&lt;span style="color: grey;"&gt;.&lt;/span&gt;&lt;span style="color: blue;"&gt;text&lt;/span&gt;&lt;span style="color: grey;"&gt;, &lt;/span&gt;sson&lt;span style="color: grey;"&gt;.&lt;/span&gt;login_time&lt;span style="color: grey;"&gt;,  &lt;/span&gt;sson&lt;span style="color: grey;"&gt;.&lt;/span&gt;&lt;span style="color: blue;"&gt;status
FROM &lt;/span&gt;&lt;span style="color: green;"&gt;sys&lt;/span&gt;&lt;span style="color: grey;"&gt;.&lt;/span&gt;&lt;span style="color: green;"&gt;dm_exec_connections &lt;/span&gt;conn
&lt;span style="color: grey;"&gt;INNER JOIN &lt;/span&gt;&lt;span style="color: green;"&gt;sys&lt;/span&gt;&lt;span style="color: grey;"&gt;.&lt;/span&gt;&lt;span style="color: green;"&gt;dm_exec_sessions &lt;/span&gt;sson 
&lt;span style="color: blue;"&gt;ON &lt;/span&gt;conn&lt;span style="color: grey;"&gt;.&lt;/span&gt;session_id &lt;span style="color: grey;"&gt;= &lt;/span&gt;sson&lt;span style="color: grey;"&gt;.&lt;/span&gt;session_id
&lt;span style="color: grey;"&gt;CROSS APPLY &lt;/span&gt;&lt;span style="color: green;"&gt;sys&lt;/span&gt;&lt;span style="color: grey;"&gt;.&lt;/span&gt;&lt;span style="color: green;"&gt;dm_exec_sql_text&lt;/span&gt;&lt;span style="color: grey;"&gt;(&lt;/span&gt;most_recent_sql_handle&lt;span style="color: grey;"&gt;) &lt;/span&gt;&lt;span style="color: blue;"&gt;AS &lt;/span&gt;sqltxt
&lt;span style="color: blue;"&gt;ORDER BY &lt;/span&gt;conn&lt;span style="color: grey;"&gt;.&lt;/span&gt;session_id&lt;/pre&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;
Here I&amp;nbsp;have utilized the &lt;a href="http://msdn.microsoft.com/en-us/library/ms181509.aspx" target="_blank"&gt;sys.dm_exec_connections&lt;/a&gt; Dynamic Management View, in conjunction with the &lt;a href="http://msdn.microsoft.com/en-us/library/ms176013.aspx" target="_blank" title="sys.dm_exec_sessions"&gt;sys.dm_exec_sessions&lt;/a&gt; DMV and &lt;a href="http://msdn.microsoft.com/en-us/library/ms181929.aspx" target="_blank"&gt;sys.dm_exec_sql_text&lt;/a&gt; Dynamic Management Function (DMF) to return the last query executed against all SQL Server databases, in that server.&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;
Here’s a quick overview of what these DMV’s and DMF do&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;strong&gt;sys.dm_exec_connections&lt;/strong&gt; - Returns information about the connections established to this instance of SQL Server and the details of each connection&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;strong&gt;sys.dm_exec_sessions&lt;/strong&gt; - Returns one row per authenticated session on SQL Server. sys.dm_exec_sessions is a server-scope view that shows information about all active user connections and internal tasks. This information includes client version, client program name, client login time, login user, current session setting, and more&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;strong&gt;sys.dm_exec_sql_text&lt;/strong&gt; - Returns the text of the SQL batch that is identified by the specified sql_handle&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;
In the last statement, we are passing the value contained in the most_recent_sql_handle column of this DMV to the sys.dm_exec_sql_text DMF.&amp;nbsp; The DMF returns the text of the sql query, whose sql_handle we passed to it.&amp;nbsp; This sql_handle that we passed, uniquely identifies the query.&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;
Here’s the output&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;img alt="sql-last-run-query" border="0" height="147" src="http://lh6.ggpht.com/-XA5QuXjo4UY/TuGspwvRUgI/AAAAAAAACXY/fmB_2-J-Nzk/sql-last-run-query%25255B3%25255D.png?imgmax=800" style="background-image: none; border: 0px currentColor; display: inline; padding-left: 0px; padding-right: 0px; padding-top: 0px;" title="sql-last-run-query" width="590" /&gt;&lt;/div&gt;
&lt;div style="text-align: justify;"&gt;
&lt;br /&gt;
Also check out &lt;a href="http://www.sqlservercurry.com/2010/07/find-most-time-consuming-code-in-your.html"&gt;&lt;strong&gt;Find the Most Time Consuming Code in your SQL Server Database&lt;/strong&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/495177919198719500-4542080520258343430?l=www.sqlservercurry.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/x0fz8Ds-d3s" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/x0fz8Ds-d3s/last-run-query-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/-XA5QuXjo4UY/TuGspwvRUgI/AAAAAAAACXY/fmB_2-J-Nzk/s72-c/sql-last-run-query%25255B3%25255D.png?imgmax=800" height="72" width="72" /><thr:total>1</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2011/12/last-run-query-in-sql-server.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-2227272651772067701</guid><pubDate>Sun, 04 Dec 2011 12:39:00 +0000</pubDate><atom:updated>2011-12-04T04:48:05.805-08: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;br /&gt;
&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;
&lt;/div&gt;
&lt;/dt&gt;
&lt;div align="justify"&gt;
&lt;/div&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;
&lt;/div&gt;
&lt;/dt&gt;
&lt;div align="justify"&gt;
&lt;/div&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;
&lt;/div&gt;
&lt;/dt&gt;
&lt;div align="justify"&gt;
&lt;/div&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;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/495177919198719500-2227272651772067701?l=www.sqlservercurry.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/ldN2NIJaytc" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/ldN2NIJaytc/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/2011/12/activity-monitor-in-sql-server.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-495177919198719500.post-6955157414696007489</guid><pubDate>Wed, 30 Nov 2011 04:47:00 +0000</pubDate><atom:updated>2011-11-29T20:50:10.214-08:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">SQL Server General</category><title>SQL Server Download Links All Editions</title><description>&lt;div align="justify"&gt;
With SQL Server 2012 RC0 recently announced and a plethora of previous editions already available, it can get tedious to keep track of all these downloads. In this post, I will attempt to share the download links of all SQL Server versions - SQL Server 2012, SQL Server 2008 &amp;amp; R2 and SQL Server 2005. I will update this post and the links as and when applicable.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h2 align="justify"&gt;
SQL Server 2012 Download Links&lt;/h2&gt;
&lt;div align="justify"&gt;
&lt;strong&gt;&lt;/strong&gt;Microsoft SQL Server 2012 RC0 enables a cloud-ready information platform.&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.microsoft.com/download/en/details.aspx?id=28145" rel="nofollow" target="_blank" title="Download SQL Server 2012 Release Candidate"&gt;SQL Server 2012 Release Candidate 0 (RC0)&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.microsoft.com/download/en/details.aspx?id=28151" rel="nofollow" target="_blank" title="Download SQL Server 2012 Express RC0"&gt;SQL Server 2012 Express RC0&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.microsoft.com/download/en/details.aspx?id=28150" rel="nofollow" target="_blank" title="Download SQL Server 2012 PowerPivot"&gt;SQL Server 2012 PowerPivot for Microsoft Excel 2010 Release Candidate 0 (RC0)&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.microsoft.com/download/en/details.aspx?id=28149" rel="nofollow" target="_blank" title="Download SQL Server 2012 RC0 Master Data Services"&gt;SQL Server 2012 RC0 Master Data Services Add-in For Microsoft Excel&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.microsoft.com/download/en/details.aspx?id=28147" rel="nofollow" target="_blank" title="Download  SQL Server 2012 Release Candidate 0 (RC0) Manageability Tool Kit"&gt;SQL Server 2012 Release Candidate 0 (RC0) Manageability Tool Kit&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://msdn.microsoft.com/en-us/data/hh297027" rel="nofollow" target="_blank" title="Download SQL Server Data Tools CTP4"&gt;SQL Server Data Tools CTP4&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.microsoft.com/download/en/details.aspx?id=27721" rel="nofollow" target="_blank" title="Download SQL Server 2012 Developer Training Kit"&gt;SQL Server 2012 Developer Training Kit Web Installer Preview&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h2 align="justify"&gt;
SQL Server 2008 R2 Download Links&lt;/h2&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.microsoft.com/download/en/details.aspx?displaylang=en&amp;amp;id=6362" rel="nofollow" target="_blank" title="Download SQL Server 2008 R2 Evaluation"&gt;SQL Server 2008 R2 Evaluation&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.microsoft.com/download/en/details.aspx?id=26727" rel="nofollow" target="_blank" title="Download SQL Server 2008 R2 SP1"&gt;SQL Server 2008 R2 Service Pack 1&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.microsoft.com/download/en/details.aspx?displaylang=en&amp;amp;id=3743" rel="nofollow" target="_blank" title="Download SQL Server 2008 R2 Express"&gt;SQL Server 2008 R2 RTM – Express&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.microsoft.com/download/en/details.aspx?id=26729" rel="nofollow" target="_blank" title="SQL Server 2008 R2 SP1 - Express Edition"&gt;SQL Server 2008 R2 SP1 - Express Edition&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.microsoft.com/download/en/details.aspx?id=22985" rel="nofollow" target="_blank" title="Download SQL Server 2008 R2 Express SSMS"&gt;SQL Server 2008 R2 RTM - Management Studio Express&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.microsoft.com/download/en/details.aspx?id=23650" rel="nofollow" target="_blank" title="SQL Server 2008 R2 RTM - Express with Management Tools"&gt;SQL Server 2008 R2 RTM - Express with Management Tools&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.microsoft.com/download/en/details.aspx?id=25174" rel="nofollow" target="_blank" title="SQL Server2008 R2 RTM - Express with Advanced Services"&gt;SQL Server 2008 R2 RTM - Express with Advanced Services&lt;/a&gt;&lt;/div&gt;
&lt;a href="http://www.sqlservercurry.com/2011/06/sql-server-2008-r2-vhd-available-for.html" title="SQL Server 2008 R2 VHD"&gt;SQL Server 2008 R2 VHD&lt;/a&gt;&lt;br /&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h2 align="justify"&gt;
SQL Server 2008 Download Links&lt;/h2&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.microsoft.com/download/en/details.aspx?id=8850" rel="nofollow" target="_blank" title="Download SQL Server 2008 Trial"&gt;SQL Server 2008 Trial&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.microsoft.com/download/en/details.aspx?id=27594" rel="nofollow" target="_blank"&gt;SQL Server 2008 Service Pack 3&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.microsoft.com/download/en/details.aspx?id=1695" rel="nofollow" target="_blank"&gt;SQL Server 2008 Express&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.microsoft.com/download/en/details.aspx?id=27597" rel="nofollow" target="_blank" title="Download SQL Server 2008 Express SP3"&gt;SQL Server 2008 Express Edition Service Pack 3&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.microsoft.com/download/en/details.aspx?id=7593" rel="nofollow" target="_blank"&gt;SQL Server 2008 Management Studio Express&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.microsoft.com/download/en/details.aspx?id=22973" rel="nofollow" target="_blank"&gt;SQL Server 2008 Express with Tools&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h2 align="justify"&gt;
SQL Server 2005 Download Links&lt;/h2&gt;
&lt;div align="justify"&gt;
SQL Server 2005 Evaluation is no longer available. You can use the Express Edition&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=B953E84F-9307-405E-BCEB-47BD345BAECE&amp;amp;displaylang=en" rel="nofollow" target="_blank"&gt;SQL Server 2005 SP4 RTM&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=220549B5-0B07-4448-8848-DCC397514B41&amp;amp;displaylang=en" rel="nofollow" target="_blank"&gt;SQL Server 2005 Express Edition&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.microsoft.com/download/en/details.aspx?id=15291" rel="nofollow" target="_blank"&gt;Microsoft SQL Server 2005 Express Edition Service Pack 3&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;a href="http://www.microsoft.com/download/en/details.aspx?id=8961" rel="nofollow" target="_blank" title="Download SQL Server 2005 SSMS Express"&gt;SQL Server Management Studio Express&lt;/a&gt;&lt;/div&gt;
&lt;div align="justify"&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;h2 align="justify"&gt;
Other SQL Server Download Links&lt;/h2&gt;
&lt;a href="http://www.sqlservercurry.com/2011/07/sql-server-migration-assistant-for.html"&gt;SQL Server Migration Assistant for Oracle v5.1&lt;/a&gt;&lt;br /&gt;
&lt;a href="http://www.sqlservercurry.com/2011/06/sql-server-driver-20-for-php-5.html"&gt;SQL Server Driver 2.0 for PHP 5&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div align="justify"&gt;
Any important links that I missed out? Share it in the comments section and I will update the post!&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/495177919198719500-6955157414696007489?l=www.sqlservercurry.com' alt='' /&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/sqlservercurry/blog/~4/sIjpjSweohU" height="1" width="1"/&gt;</description><link>http://feedproxy.google.com/~r/sqlservercurry/blog/~3/sIjpjSweohU/sql-server-download-links-all-editions.html</link><author>noreply@blogger.com (Suprotim Agarwal)</author><thr:total>1</thr:total><feedburner:origLink>http://www.sqlservercurry.com/2011/11/sql-server-download-links-all-editions.html</feedburner:origLink></item></channel></rss>

