<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-5334232245085456076</id><updated>2026-06-08T23:37:46.639-07:00</updated><category term="p"/><title type='text'>Dimant DataBase Solutions</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><link rel='next' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default?start-index=26&amp;max-results=25'/><author><name>Uri Dimant</name><uri>http://www.blogger.com/profile/17252545378139162559</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>122</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-6769001584875734959</id><published>2020-07-13T01:13:00.002-07:00</published><updated>2020-07-13T01:13:57.777-07:00</updated><title type='text'>Calendar table , again...</title><content type='html'>Hello friends.&lt;br /&gt;
I am back to start writing posts about all things that related to SQL Server. There are lots of examples over a internet about how create a calendar table in T-SQL.&amp;nbsp;&lt;br /&gt;
I would like to show you a little bit different technique to create a calendar table which based on the time interval. Let say we need to create a table with one hour interval, I have done it recently for our BI team to create Power BI reports.&lt;br /&gt;
Simple we are going to use a recursivequery to create such table.&lt;br /&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;with&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt; tmp&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;plant_date&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;as&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;color: grey; font-family: Consolas; font-size: 9.5pt;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;select&lt;/span&gt; &lt;span style=&quot;color: magenta;&quot;&gt;CAST&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: red;&quot;&gt;&#39;20200101&#39;&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;as&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;datetime&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;)&amp;nbsp; --- Start date&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;union&lt;/span&gt; &lt;span style=&quot;color: grey;&quot;&gt;all&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;select&lt;/span&gt;
&lt;span style=&quot;color: magenta;&quot;&gt;DATEADD&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;hour&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; 1&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; plant_date&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;from&lt;/span&gt;
tmp&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;where&lt;/span&gt; &lt;span style=&quot;color: magenta;&quot;&gt;DATEADD&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;hour&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; 1&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; plant_date&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt;&amp;nbsp;&amp;nbsp;
&lt;span style=&quot;color: grey;&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;&#39;20281231&#39;&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;color: grey; font-family: Consolas; font-size: 9.5pt;&quot;&gt;)/*After we create a simple table&amp;nbsp; you can extract an year , month, a day and etc.*/&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;select&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt; plant_date&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;cast&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;plant_date &lt;span style=&quot;color: blue;&quot;&gt;as&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;date&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;) date,&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;YEAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;plant_date&lt;span style=&quot;color: grey;&quot;&gt;) Year,&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;CASE&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt; &lt;span style=&quot;color: blue;&quot;&gt;WHEN&lt;/span&gt; &lt;span style=&quot;color: magenta;&quot;&gt;DATEPART&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;MONTH&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; plant_date&lt;span style=&quot;color: grey;&quot;&gt;)&amp;lt;&lt;/span&gt;10 &lt;span style=&quot;color: blue;&quot;&gt;THEN&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;&#39;0&#39;&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&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;
&lt;span style=&quot;color: grey;&quot;&gt;+&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&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;
&lt;span style=&quot;color: magenta;&quot;&gt;CAST&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;DATEPART&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;MONTH&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; plant_date&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;AS&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;VARCHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;2&lt;span style=&quot;color: grey;&quot;&gt;))&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&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;
&lt;span style=&quot;color: blue;&quot;&gt;ELSE&lt;/span&gt; &lt;span style=&quot;color: magenta;&quot;&gt;CAST&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;DATEPART&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;MONTH&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; plant_date&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;AS&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;VARCHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;2&lt;span style=&quot;color: grey;&quot;&gt;))&lt;/span&gt; &lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&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;
&lt;span style=&quot;color: blue;&quot;&gt;END Month&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;&lt;span style=&quot;color: magenta;&quot;&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; DATENAME&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;MONTH&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;plant_date&lt;span style=&quot;color: grey;&quot;&gt;),&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&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;
&lt;span style=&quot;color: blue;&quot;&gt;CASE&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;WHEN&lt;/span&gt; &lt;span style=&quot;color: magenta;&quot;&gt;DATEPART&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;day&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; plant_date&lt;span style=&quot;color: grey;&quot;&gt;)&amp;lt;&lt;/span&gt;10 &lt;span style=&quot;color: blue;&quot;&gt;THEN&lt;/span&gt; &lt;span style=&quot;color: red;&quot;&gt;&#39;0&#39;&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&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;
&lt;span style=&quot;color: grey;&quot;&gt;+&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&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;
&lt;span style=&quot;color: magenta;&quot;&gt;CAST&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;DATEPART&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;day&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; plant_date&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;AS&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;VARCHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;2&lt;span style=&quot;color: grey;&quot;&gt;))&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&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;
&lt;span style=&quot;color: blue;&quot;&gt;ELSE&lt;/span&gt; &lt;span style=&quot;color: magenta;&quot;&gt;CAST&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;DATEPART&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;day&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt; plant_date&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;AS&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;VARCHAR&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;2&lt;span style=&quot;color: grey;&quot;&gt;))&lt;/span&gt; &lt;span style=&quot;color: blue;&quot;&gt;END day&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&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;
&lt;span style=&quot;color: magenta;&quot;&gt;DATEPART&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: magenta;&quot;&gt;hour&lt;/span&gt;&lt;span style=&quot;color: grey;&quot;&gt;,&lt;/span&gt;plant_date&lt;span style=&quot;color: grey;&quot;&gt;) hour&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;&amp;nbsp; &lt;span style=&quot;color: blue;&quot;&gt;from&lt;/span&gt;&amp;nbsp; tmp&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;span style=&quot;color: blue; font-family: Consolas; font-size: 9.5pt;&quot;&gt;option &lt;/span&gt;&lt;span style=&quot;color: grey; font-family: Consolas; font-size: 9.5pt;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;font-family: Consolas; font-size: 9.5pt;&quot;&gt;maxrecursion 0&lt;span style=&quot;color: grey;&quot;&gt;)&lt;/span&gt;&lt;o:p&gt;&lt;/o:p&gt;&lt;/span&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
I hope the above script helps&amp;nbsp; you.&lt;/div&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;br /&gt;
&lt;div class=&quot;MsoNormal&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;br /&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/6769001584875734959/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/5334232245085456076/6769001584875734959' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/6769001584875734959'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/6769001584875734959'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2020/07/calendar-table-again.html' title='Calendar table , again...'/><author><name>Uri Dimant</name><uri>http://www.blogger.com/profile/17252545378139162559</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-5938634201180210378</id><published>2014-05-14T00:10:00.001-07:00</published><updated>2014-05-14T00:10:39.223-07:00</updated><title type='text'>Make sure that you write/create optimized SSIS Packages</title><content type='html'>Recently I worked on the package that loops many text files and insert the data into a staging table and then from the staging into original table which has &amp;nbsp;five columns only defined as VARCHAR(50) in the database. Pretty simple , not? Text files have many data and the destination table has many columns (90+) and very wide data tape client defines (VARCHAR(3000)). Actually he needs to get only few columns but &amp;nbsp;who cares, right? We have observed that the memory during the package execution grow &amp;nbsp;rapidly and very quickly MSSQL Server service was become unavailabe. You can imagine that during this spike nobody can work and a local DBA started getting complains from the end users. I would like to say that the server is very strong (4CPU+ 28RAM)...So what&#39;s problem?&lt;br /&gt;
&lt;br /&gt;
At the source component , the estimated sized of a row is determined by the maximum columns sizes of all columns returned by the query (remember we have 90+ columns defined as VARCHAR(3000)). This is where &amp;nbsp;the performance problem resides.&lt;br /&gt;
&lt;br /&gt;
After sometime investigations we dropped the wide stage table and created a stage table with exactly same structure as original table has.&lt;br /&gt;
That means columns have VARCHAR(50) and we also changed the job schedule to run it frequently to deal with small data sets. &amp;nbsp;So performance was drastically improved. We have not seen any more memory growing and not reducing, there is no more compliance from the end user.&lt;br /&gt;
&lt;br /&gt;
Please read this article if you deal with packages especially if it works with large data sets.&lt;br /&gt;
http://technet.microsoft.com/en-us/library/cc966529.aspx</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/5938634201180210378/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/5334232245085456076/5938634201180210378' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/5938634201180210378'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/5938634201180210378'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2014/05/make-sure-that-you-writecreate.html' title='Make sure that you write/create optimized SSIS Packages'/><author><name>Uri Dimant</name><uri>http://www.blogger.com/profile/17252545378139162559</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-2435563643535095585</id><published>2014-01-01T00:53:00.002-08:00</published><updated>2014-01-01T00:54:43.378-08:00</updated><title type='text'>How to fix the fragmentation on heaps? </title><content type='html'>&lt;br /&gt;
We know that heaps are the tables without clustered index on. It may have many non clustered index &amp;nbsp;(NCI) but still it is considered a heap. So consider a huge table (heap) that is fragmented. How would &amp;nbsp;you fix it? Technically, you cannot defrag a heap table. &amp;nbsp;But remember we have ALTER table REBUILD command which works very well on the heaps. But there is one big But. If you have many NCI on the table this command will rebuild all of them at once. What does it mean? It can produce transaction-log bloat which hits the over all performance. Think about the process the scan the log or perhaps Log Shipping job that needs to move the log file to the remote server. All this affects performance.&lt;br /&gt;
&lt;br /&gt;
So, to answer the question ,we need to create a clustered index on that table..That is simple answer to the question.&lt;br /&gt;
&lt;br /&gt;
Happy New Year to all!&lt;br /&gt;
&lt;br /&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/2435563643535095585/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/5334232245085456076/2435563643535095585' title='14 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/2435563643535095585'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/2435563643535095585'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2014/01/how-to-fix-fragmentation-on-heaps.html' title='How to fix the fragmentation on heaps? '/><author><name>Uri Dimant</name><uri>http://www.blogger.com/profile/17252545378139162559</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>14</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-5963024695663118206</id><published>2013-05-10T01:03:00.000-07:00</published><updated>2013-05-10T01:03:23.849-07:00</updated><title type='text'>One more method of using UNPIVOT command </title><content type='html'>&lt;br /&gt;
Hi friends&lt;br /&gt;
I had recently a client who has a table with more than 90&#39;s column and his requirement was to return the all columns that IS NOT NULL. So starting with sample T-SQL you will need to filter each column to check for NULLs, like WHERE col1 IS NOT NULL or col2 IS NOT NULL... or perhaps even using an aggregation &amp;nbsp;to&amp;nbsp;eliminate NULLs. But I found pretty nice solution, so take a look at below DDL.&amp;nbsp;We have a table with number of columns (Dayn) to be checked for NULLs. I used simple UNPIVOT output of multiple rows into multiple columns in a single row.&lt;br /&gt;
&lt;br /&gt;
DECLARE @Table TABLE&lt;br /&gt;
(&lt;br /&gt;
UserId INT,&lt;br /&gt;
Day1 INT NULL,&lt;br /&gt;
Day2 INT NULL,&lt;br /&gt;
Day3 INT NULL,&lt;br /&gt;
Day4 INt NULL,&lt;br /&gt;
Day5 INT NULL,&lt;br /&gt;
Day6 INT NULL,&lt;br /&gt;
Day7 INT NULL,&lt;br /&gt;
Day8 INt NULL,&lt;br /&gt;
Day9 INT NULL,&lt;br /&gt;
Day10 INt NULL&lt;br /&gt;
);&lt;br /&gt;
&lt;br /&gt;
INSERT INTO @Table(UserId,&lt;br /&gt;
&amp;nbsp;Day1, Day2, Day3, Day4, Day5, Day6,Day7, Day8,Day9, Day10)&lt;br /&gt;
VALUES(1,null,null,20,3,null,null,null,null,null,null);&lt;br /&gt;
INSERT INTO @Table(UserId,&lt;br /&gt;
&amp;nbsp;Day1, Day2, &amp;nbsp;Day3, Day4,Day5, Day6,Day7, Day8,Day9, Day10)&lt;br /&gt;
VALUES(2,50,25,15,5,null,null,null,null,null,null);&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
SELECT UserId, Col_NotNull FROM @Table&lt;br /&gt;
UNPIVOT (Col_NotNull FOR DayNumber&lt;br /&gt;
IN (Day1, Day2, &amp;nbsp;Day3, Day4,Day5, Day6,Day7, Day8,Day9, Day10)) AS c&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
UserId&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;Col_NotNull&lt;br /&gt;
1&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;20&lt;br /&gt;
1&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;3&lt;br /&gt;
2&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;50&lt;br /&gt;
2&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;25&lt;br /&gt;
2&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;15&lt;br /&gt;
2&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;5&lt;br /&gt;
&lt;br /&gt;
&lt;div&gt;
&lt;br /&gt;&lt;/div&gt;
</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/5963024695663118206/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/5334232245085456076/5963024695663118206' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/5963024695663118206'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/5963024695663118206'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2013/05/one-more-method-of-using-unpivot-command.html' title='One more method of using UNPIVOT command '/><author><name>Uri Dimant</name><uri>http://www.blogger.com/profile/17252545378139162559</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-5608357847987113199</id><published>2012-10-31T06:01:00.001-07:00</published><updated>2012-10-31T06:05:51.381-07:00</updated><title type='text'>SQL Server 2012 IIF function may replace CASE expression?</title><content type='html'>&lt;br /&gt;
Hi&lt;br /&gt;
Did you install already SQL Server 2012? Well, see a new T-SQL function named IIF you can use sometimes instead of CASE expression.&lt;br /&gt;
&lt;a href=&quot;http://msdn.microsoft.com/en-us/library/hh213574.aspx&quot;&gt;http://msdn.microsoft.com/en-us/library/hh213574.aspx&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
CREATE TABLE #t (id INT)&lt;br /&gt;
&lt;br /&gt;
INSERT INTO #t VALUES (1),(2),(3)&lt;br /&gt;
&lt;br /&gt;
DECLARE @a int = 3;&lt;br /&gt;
DECLARE @b int = 2;&lt;br /&gt;
&lt;br /&gt;
SELECT * FROM #t WHERE id= IIF ( @a &amp;gt; @b, @a, @b ) &lt;br /&gt;
&lt;br /&gt;
Returns Id=3.&lt;br /&gt;
&lt;br /&gt;
You can even using nested IIF commands.&lt;br /&gt;
SELECT * FROM #t WHERE id= IIF ( @a &amp;gt; @b, IIF(@a&amp;gt;0,@b,0), @b )&lt;br /&gt;
&lt;br /&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/5608357847987113199/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/5334232245085456076/5608357847987113199' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/5608357847987113199'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/5608357847987113199'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2012/10/sql-server-2012-iif-function-may.html' title='SQL Server 2012 IIF function may replace CASE expression?'/><author><name>Uri Dimant</name><uri>http://www.blogger.com/profile/17252545378139162559</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-7080431250230142518</id><published>2012-06-18T02:21:00.001-07:00</published><updated>2012-06-18T02:21:31.485-07:00</updated><title type='text'>Identity property remains gaps</title><content type='html'>Let say you have a table and use identity property. It is ok, but I really hope that you are aware of the bug that remains gaps in identity if you restart MS SQL Server service. Please vote&amp;nbsp;&lt;a href=&quot;http://connect.microsoft.com/SQLServer/feedback/details/739013/alwayson-fail&quot; style=&quot;background-color: white;&quot;&gt;http://connect.microsoft.com/SQLServer/feedback/details/739013/alwayson-fail&lt;/a&gt;&lt;br /&gt;
&lt;div class=&quot;MsoPlainText&quot;&gt;
&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoPlainText&quot;&gt;
over-results-in-reseed-of-identity&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoPlainText&quot;&gt;
Thanks&lt;/div&gt;
&lt;div class=&quot;MsoPlainText&quot;&gt;
&lt;br /&gt;&lt;/div&gt;
&lt;div class=&quot;MsoPlainText&quot;&gt;
&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;
&lt;div class=&quot;MsoPlainText&quot;&gt;
&lt;o:p&gt;&lt;/o:p&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/7080431250230142518/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/5334232245085456076/7080431250230142518' title='7 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/7080431250230142518'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/7080431250230142518'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2012/06/identity-property-remains-gaps.html' title='Identity property remains gaps'/><author><name>Uri Dimant</name><uri>http://www.blogger.com/profile/17252545378139162559</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>7</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-5675408999993121263</id><published>2012-04-23T00:28:00.002-07:00</published><updated>2012-04-23T00:39:18.992-07:00</updated><title type='text'>Successful upgrade to SQL Server 2012</title><content type='html'>Hi everyone. Today I successfully upgraded our production database to the new version -SQL Server 2012. Actually everything went ok, and after running Upgrade Advisor and restored the database into a new server. The &quot;challenge&quot; was to upgrade existing SSRS reports and SSIS packages. What I would recommend is to open a new project in SQL Data Tools (yes BIDS is gone) and adding report by report to the project. SQL Server automatically upgraded  them for first time I  run them. Another thing is that now you can much easily to configure SSRS and even if you specified not to &quot;configure&quot; during  the installation. I have not noticed any performance degradation since we moved from SQL Server 2005.So lets enjoy new features that were introduced and happy working with SQL Server 2012.</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/5675408999993121263/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/5334232245085456076/5675408999993121263' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/5675408999993121263'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/5675408999993121263'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2012/04/successful-upgrade-to-sql-server-2012.html' title='Successful upgrade to SQL Server 2012'/><author><name>Uri Dimant</name><uri>http://www.blogger.com/profile/17252545378139162559</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-7079386517888166553</id><published>2012-03-06T21:46:00.000-08:00</published><updated>2012-03-06T21:48:10.536-08:00</updated><title type='text'>SQL Server 2012 has released to manufacturing (RTM)</title><content type='html'>Microsoft announced that SQL Server 2012 has released to manufacturing (RTM). Customers and partners can download an evaluation of the product today (http://www.microsoft.com/sqlserver/en/us/default.aspx) and can expect general availability to begin on April 1.</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/7079386517888166553/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/5334232245085456076/7079386517888166553' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/7079386517888166553'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/7079386517888166553'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2012/03/sql-server-2012-has-released-to.html' title='SQL Server 2012 has released to manufacturing (RTM)'/><author><name>Uri Dimant</name><uri>http://www.blogger.com/profile/17252545378139162559</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>6</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-547263869542147978</id><published>2012-02-01T04:20:00.001-08:00</published><updated>2012-02-27T04:53:52.655-08:00</updated><title type='text'>LIKE operator &quot;issue&quot;</title><content type='html'>Hi&lt;br /&gt;Please consider the below script&lt;br /&gt;&lt;br /&gt;create table #t ( col varchar(40))&lt;br /&gt;&lt;br /&gt;insert into #t values (&#39;[Untitled].pdf&#39;)&lt;br /&gt;insert into #t values (&#39;Untitled].pdf&#39;)&lt;br /&gt;insert into #t values (&#39;^Untitled].pdf&#39;)&lt;br /&gt;insert into #t values (&#39;|Untitled].pdf&#39;)&lt;br /&gt;&lt;br /&gt;Now the client runs the below SELECT statement&lt;br /&gt;&lt;br /&gt;select * from #t where col like col&lt;br /&gt;&lt;br /&gt;Ok, you ask why col LIKE col and not col=col. Well I really simplified the the query because it takes a parameter and needs to search sub string within a col base on CASE Expression.So as you can see the above script does not return the first row [Untitled].pdf. After some investigation it turns out than we cannot get rid of &lt;span style=&quot;font-weight:bold;&quot;&gt;left bracket&lt;/span&gt;. The only real solution is the below script (thanks to Peter Larsson)&lt;br /&gt;&lt;br /&gt;select * from #t where col like replace(col, &#39;[&#39;, &#39;[[]&#39;)</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/547263869542147978/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/5334232245085456076/547263869542147978' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/547263869542147978'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/547263869542147978'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2012/02/like-operator-issue.html' title='LIKE operator &quot;issue&quot;'/><author><name>Uri Dimant</name><uri>http://www.blogger.com/profile/17252545378139162559</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-3543275487993090868</id><published>2012-01-10T20:57:00.000-08:00</published><updated>2012-01-10T21:01:24.314-08:00</updated><title type='text'>Must read that blog</title><content type='html'>Happy New Year to everyone!&lt;br /&gt; &lt;br /&gt;Just found great web site about SQL Server maintained by Remus Rusanu (MS Employee)&lt;br /&gt;It contains great info not only about published releases (SQL Server 2005/2008/2008R2) but also new features,command about SQL Server 2012&lt;br /&gt;&lt;br /&gt;http://rusanu.com/2011/08/05/</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/3543275487993090868/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/5334232245085456076/3543275487993090868' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/3543275487993090868'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/3543275487993090868'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2012/01/must-read-that-blog.html' title='Must read that blog'/><author><name>Uri Dimant</name><uri>http://www.blogger.com/profile/17252545378139162559</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-545912967486325930</id><published>2011-12-07T02:02:00.000-08:00</published><updated>2011-12-07T02:11:07.160-08:00</updated><title type='text'>Implicit conversions , sometimes it is hidden</title><content type='html'>Consider simple table with one column defined as REAL datatype&lt;br /&gt;CREATE TABLE #t (c REAL)&lt;br /&gt;INSERT INTO #t VALUES (0)&lt;br /&gt;&lt;br /&gt;SELECT COUNT(*) FROM #t WHERE c=&#39;&#39; &lt;br /&gt;&lt;br /&gt;In above statement you expect getting 0 rows to be returns as we filter out for all nonempty rows..But it returns 1 and the answer  you find looking at execution plan.&lt;br /&gt;&lt;br /&gt;&lt;a onblur=&quot;try {parent.deselectBloggerImageGracefully();} catch(e) {}&quot; href=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjvOp_EdVBD1PaFFuxVgGMCsveVx_r_IzbTLlAFtm-_Hhet_Gmk70dB14mm7IpoyLci6Y0bI8zLFaebsqqw1yF4xM_gqXqUBTpwqXz7V7AWJvKGTlZPqdcejAKhl9M5htmwnkjO3ofQvrz4/s1600/1.jpg&quot;&gt;&lt;img style=&quot;display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 278px; height: 320px;&quot; src=&quot;https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjvOp_EdVBD1PaFFuxVgGMCsveVx_r_IzbTLlAFtm-_Hhet_Gmk70dB14mm7IpoyLci6Y0bI8zLFaebsqqw1yF4xM_gqXqUBTpwqXz7V7AWJvKGTlZPqdcejAKhl9M5htmwnkjO3ofQvrz4/s320/1.jpg&quot; border=&quot;0&quot; alt=&quot;&quot;id=&quot;BLOGGER_PHOTO_ID_5683326429901134482&quot; /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;SQL Server will implicitly convert &#39;&#39; to REAL datatype with 0 and a result is 1 row to be return.</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/545912967486325930/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/5334232245085456076/545912967486325930' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/545912967486325930'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/545912967486325930'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2011/12/implicit-conversions-sometimes-it-is.html' title='Implicit conversions , sometimes it is hidden'/><author><name>Uri Dimant</name><uri>http://www.blogger.com/profile/17252545378139162559</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjvOp_EdVBD1PaFFuxVgGMCsveVx_r_IzbTLlAFtm-_Hhet_Gmk70dB14mm7IpoyLci6Y0bI8zLFaebsqqw1yF4xM_gqXqUBTpwqXz7V7AWJvKGTlZPqdcejAKhl9M5htmwnkjO3ofQvrz4/s72-c/1.jpg" height="72" width="72"/><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-3194956118279632280</id><published>2011-11-06T06:13:00.000-08:00</published><updated>2011-11-06T06:24:26.464-08:00</updated><title type='text'>Filter out characters---conversion error?</title><content type='html'>Just help out may colleague to write a query where we needed to filter out all data that contains characters and because a column is defined as VARCHAR we CAST it to INTEGER in order to implement range searching. Please see simplified demo script.&lt;br /&gt;&lt;br /&gt;CREATE TABLE #t (c varchar(50))&lt;br /&gt;INSERT INTO #t VALUES (&#39;122&#39;)&lt;br /&gt;INSERT INTO #t VALUES (&#39;4545&#39;)&lt;br /&gt;INSERT INTO #t VALUES (&#39;4545/454&#39;)&lt;br /&gt;INSERT INTO #t VALUES (&#39;4899&#39;)&lt;br /&gt;&lt;br /&gt;----Failed&lt;br /&gt;SELECT * FROM &lt;br /&gt;(&lt;br /&gt;SELECT c FROM #t WHERE c NOT LIKE &#39;%[/]%&#39; &lt;br /&gt;) AS d WHERE CAST(c AS INT)&gt;10&lt;br /&gt;&lt;br /&gt;----Succeed&lt;br /&gt;SELECT * FROM #t&lt;br /&gt;WHERE CASE WHEN c  LIKE &#39;%[^0-9]%&#39; THEN 0&lt;br /&gt;           WHEN CAST(c AS int) BETWEEN 1 AND 1000 THEN 1&lt;br /&gt;           ELSE 0 END = 1&lt;br /&gt;&lt;br /&gt;The answer why the first attemp is failed we found looking at execution plan. &lt;br /&gt;&lt;span style=&quot;font-weight:bold;&quot;&gt;Predicate&lt;/span&gt; &lt;br /&gt;CONVERT(int,[tempdb].[dbo].[#t].[c],0)&gt;(10) AND NOT [tempdb].[dbo].[#t].[c] like &#39;%[/]%&#39;&lt;br /&gt;&lt;br /&gt;Thinking that we filter out all &quot;bad&quot; rows and can CAST the rest is wrong because as we see above predicate is applied for the whole table.&lt;br /&gt;&lt;br /&gt;As opposite the second query we used CASE expression to filter out &quot;bad&quot; rows  CASE...=1 we see that SQL Server really filters out &quot;bad&quot; rows and now CAST is working.&lt;br /&gt;&lt;span style=&quot;font-weight:bold;&quot;&gt;Predicate&lt;/span&gt;&lt;br /&gt;CASE WHEN [tempdb].[dbo].[#t].[c] like &#39;%[^0-9]%&#39; THEN (0) ELSE CASE WHEN CONVERT(int,[tempdb].[dbo].[#t].[c],0)&gt;=(1) AND CONVERT(int,[tempdb].[dbo].[#t].[c],0)&lt;=(1000) THEN (1) ELSE (0) END END=(1)</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/3194956118279632280/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/5334232245085456076/3194956118279632280' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/3194956118279632280'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/3194956118279632280'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2011/11/filter-out-characters-conversion-error.html' title='Filter out characters---conversion error?'/><author><name>Uri Dimant</name><uri>http://www.blogger.com/profile/17252545378139162559</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-1273225245675312867</id><published>2011-11-03T01:00:00.000-07:00</published><updated>2011-11-03T01:10:12.915-07:00</updated><title type='text'>Why all my stored procedures are saved in master database under System stored procedure folder?</title><content type='html'>Just having a discussion with a colleague , she made some changes in configuration and now when she creates a simple (not a system) stored procedure in &lt;span style=&quot;font-weight:bold;&quot;&gt;master &lt;/span&gt; database it saves under Programmability --Stored Procedures--System Stored Procedures. Is it comfortable? No,right? After some investigation I found that we need to return &#39;allow updates&#39; to 0 , see below script&lt;br /&gt;&lt;br /&gt;EXEC sp_configure &#39;allow updates&#39;,0&lt;br /&gt;reconfigure&lt;br /&gt;&lt;br /&gt;Now everything got back to work in the &#39;right&#39; place.</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/1273225245675312867/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/5334232245085456076/1273225245675312867' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/1273225245675312867'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/1273225245675312867'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2011/11/why-all-my-stored-procedures-are-saved.html' title='Why all my stored procedures are saved in master database under System stored procedure folder?'/><author><name>Uri Dimant</name><uri>http://www.blogger.com/profile/17252545378139162559</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-4863169538282858902</id><published>2011-09-05T02:04:00.000-07:00</published><updated>2011-09-05T02:08:14.655-07:00</updated><title type='text'>Default Schema for Windows Group</title><content type='html'>Remember if you connect to SQL Server using Windows group, you cannot define a default schema. It is greyed out. This means that if you want everyone to be in a specific schema, you can&#39;t do it by default.&lt;br /&gt;&lt;br /&gt;See reference &lt;br /&gt;http://connect.microsoft.com/SQLServer/feedback/details/328585/default-schema-for-windows-group.&lt;br /&gt;&lt;br /&gt;I hope finally MS will be addressing this issue in the next release of SQL Server named Denali (CTP 4) &lt;br /&gt;&lt;br /&gt;/*&lt;br /&gt;I&#39;m happy to report that the next CTP for SQL Server Denali will allow default schemas to be assigned to Windows groups.&lt;br /&gt;&lt;br /&gt;I&#39;d like to thank everyone for their votes and attention to this issue. It made a different in being able to appropriately prioritize and make this improvement happen.&lt;br /&gt;&lt;br /&gt;Il-Sung.&lt;br /&gt;*/</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/4863169538282858902/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/5334232245085456076/4863169538282858902' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/4863169538282858902'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/4863169538282858902'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2011/09/default-schema-for-windows-group.html' title='Default Schema for Windows Group'/><author><name>Uri Dimant</name><uri>http://www.blogger.com/profile/17252545378139162559</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-6429587960079129040</id><published>2011-07-12T20:58:00.000-07:00</published><updated>2011-07-12T21:02:30.868-07:00</updated><title type='text'>SQL Server 2008 R2 Service Pack 1 has been released</title><content type='html'>MS has just released SQL Server 2008 R2 Service Pack 1.  Both the Service Pack and Feature Pack updates are available for download on the Microsoft Download Center.  SQL Server 2008 R2 SP1 contains cumulative updates for SQL Server 2008 R2 and fixes to issues that have been reported through our customer feedback platforms.&lt;br /&gt; &lt;br /&gt;To download the Service Pack packages please visit:&lt;br /&gt;&lt;br /&gt;http://www.microsoft.com/download/en/details.aspx?displaylang=en&amp;id=26727 ---SQL Server 2008 R2 SP1&lt;br /&gt;http://www.microsoft.com/download/en/details.aspx?displaylang=en&amp;id=26729 ---SQL Server 2008 R2 SP1 Express&lt;br /&gt;http://www.microsoft.com/download/en/details.aspx?displaylang=en&amp;id=26728 ---SQL Server 2008 R2 SP1 Feature Packs&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;To learn more about SQL Server 2008 R2 SP1 please visit Knowledge Base Article For Microsoft SQL Server 2008 R2 SP1.&lt;br /&gt;http://support.microsoft.com/kb/2528583</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/6429587960079129040/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/5334232245085456076/6429587960079129040' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/6429587960079129040'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/6429587960079129040'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2011/07/sql-server-2008-r2-service-pack-1-has.html' title='SQL Server 2008 R2 Service Pack 1 has been released'/><author><name>Uri Dimant</name><uri>http://www.blogger.com/profile/17252545378139162559</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-9166013831370234889</id><published>2011-06-13T02:15:00.000-07:00</published><updated>2011-06-13T02:30:28.217-07:00</updated><title type='text'>Partitioning experience</title><content type='html'>As you know since SQL Server 2005 MS introduced partitioning feature. I have been using it frequently with my clients but yesterday ,answering question on MSDN forum about partition I learned something new. Let say we have a partition table and staging table with the data and check constraint. See the below script.&lt;br /&gt;&lt;br /&gt;create database db1&lt;br /&gt;go&lt;br /&gt;use db1&lt;br /&gt;go&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;create partition function pf1(int) as range left for values (2,4)&lt;br /&gt;create partition scheme ps1 as partition pf1 all to ([primary])&lt;br /&gt;&lt;br /&gt;create table db1.dbo.t1 (id int ,descr varchar(2)) on ps1(id)&lt;br /&gt;create table db1.dbo.t2 (id int,descr varchar(2))&lt;br /&gt;alter table db1.dbo.t2 add check (id &gt; 9)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;create nonclustered index i1 on db1.dbo.t1(id)&lt;br /&gt;create nonclustered index i2 on db1.dbo.t2(id)&lt;br /&gt;&lt;br /&gt;insert into db1.dbo.t1 values (1,&#39;n1&#39;)&lt;br /&gt;insert into db1.dbo.t1 values(2,&#39;n2&#39;)&lt;br /&gt;insert into db1.dbo.t1 values(3,&#39;N3&#39;)&lt;br /&gt;insert into db1.dbo.t2 values (10,&#39;n1&#39;)&lt;br /&gt;insert into db1.dbo.t2 values(15,&#39;n2&#39;)&lt;br /&gt;&lt;br /&gt;---Add new range&lt;br /&gt;alter partition function pf1() split range (9)&lt;br /&gt;&lt;br /&gt;---Moving the data from t2 to t1 into a new added range&lt;br /&gt;alter table db1.dbo.t2 switch to db1.dbo.t1 partition 4&lt;br /&gt;&lt;br /&gt;Msg 4972, Level 16, State 1, Line 2&lt;br /&gt;ALTER TABLE SWITCH statement failed. Check constraints or partition function of source table &#39;db1.dbo.t2&#39; allows values that are not allowed by check constraints or partition function on target table &#39;db1.dbo.t1&#39;.&lt;br /&gt;&lt;br /&gt;I got the error, but everything should be ok. Both tables have the same datatypes,indexes,lengths.. What is going on here. I spend some time and found the below explanation in the BOL&lt;br /&gt;&lt;br /&gt;/*&lt;br /&gt;We recommend that you specify NOT NULL on the partitioning column of partitioned tables, and also nonpartitioned tables that are sources or targets for ALTER TABLE...SWITCH operations. By doing this, you make sure that any CHECK constraints on partitioning columns do not have to check for null values. Null values are ordinarily placed in the leftmost partition of a partitioned table. When you are switching any partition other than the leftmost partition and when the ANSI_NULLS database option is set to ON, the absence of a NOT NULL constraint on the source and target tables might interfere with any CHECK constraints that are also defined on the partitioning column. &lt;br /&gt;*/&lt;br /&gt;So altering  CHECK constraint with id IS NOT NULL solved the problem..&lt;br /&gt;&lt;br /&gt;alter table db1.dbo.t2 add check (id &gt; 9 AND id is not null)</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/9166013831370234889/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/5334232245085456076/9166013831370234889' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/9166013831370234889'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/9166013831370234889'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2011/06/partitioning-experience.html' title='Partitioning experience'/><author><name>Uri Dimant</name><uri>http://www.blogger.com/profile/17252545378139162559</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-7575017443541660253</id><published>2011-05-03T02:52:00.001-07:00</published><updated>2011-05-03T02:52:39.635-07:00</updated><title type='text'>SQL Server 2005 Maintenance Plan won&#39;t delete bakup files created in SQL Server 2008</title><content type='html'>I have been asked recently to help to one of my friends with &quot;strange behaviour&quot; as he described...He has SQL Server 2008 where sql job copies .BAK files (database backup) into his local disk drive with requirement to keep those file only for one month. So as he has SQL Server 2005 (SP3) installed on his machine the first idea was to create Maintenance Clean Up task to delete the files. But as you imagine that did not work. Surely, when I came to help, I did not know that those files are created in SQL Server 2008 and has been copied to his local machine and after cheching all possibilities I launch up SQL Server Profiler to see what is going on... I saw the below command to be executed for each file to be deleted .&lt;br /&gt;exec sp_executesql N&#39;RESTORE LABELONLY FROM DISK=@P1&#39;,N&#39;@P1 nchar(27)&#39;,N&#39;X:\GBDB\Log\log080411PM.bak&#39;&lt;br /&gt;Well, I put that statement in Query window and obviously got the error which leads me to the root of  the problem.&lt;br /&gt;Msg 3241, Level 16, State 7, Line 1&lt;br /&gt;The media family on device &#39;X:\GBDB\Log\log040411PM.bak&#39; is incorrectly formed. SQL Server cannot process this media family.&lt;br /&gt;Msg 3013, Level 16, State 1, Line 1&lt;br /&gt;RESTORE LABELONLY is terminating abnormally.&lt;br /&gt;&lt;br /&gt;What do you think guys? Does SQL Server need to check out the version of the files need to be deleted or just delete files with .BAK extension regardless on version they were created ?</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/7575017443541660253/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/5334232245085456076/7575017443541660253' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/7575017443541660253'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/7575017443541660253'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2011/05/sql-server-2005-maintenance-plan-wont.html' title='SQL Server 2005 Maintenance Plan won&#39;t delete bakup files created in SQL Server 2008'/><author><name>Uri Dimant</name><uri>http://www.blogger.com/profile/17252545378139162559</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-2721912155539005737</id><published>2011-04-06T00:20:00.001-07:00</published><updated>2011-04-06T00:36:20.012-07:00</updated><title type='text'>Name of CTE and referenced table should be different</title><content type='html'>Just got the below example from the public forum. &lt;br /&gt;&lt;br /&gt;CREATE TABLE Batch (Batch CHAR(1),Status INT)&lt;br /&gt;&lt;br /&gt;INSERT INTO Batch VALUES (&#39;A&#39;,1)&lt;br /&gt;INSERT INTO Batch VALUES (&#39;B&#39;,2)&lt;br /&gt;INSERT INTO Batch VALUES (&#39;C&#39;,3)&lt;br /&gt;&lt;br /&gt;WITH Batch AS&lt;br /&gt;(&lt;br /&gt; SELECT *, ROW_NUMBER() OVER(ORDER BY Batch, Status) AS RowNum&lt;br /&gt; FROM Batch&lt;br /&gt;)&lt;br /&gt;DELETE FROM Batch&lt;br /&gt;WHERE RowNum=1&lt;br /&gt;&lt;br /&gt;As you can see , running the query SQL Server throws the error.&lt;br /&gt;Msg 252, Level 16, State 1, Line 1&lt;br /&gt;Recursive common table expression &#39;Batch&#39; does not contain a top-level UNION ALL operator.&lt;br /&gt;&lt;br /&gt;SQL Server &quot;thinks&quot; that the CTE referenced to itself but there is no UNION ALL clause.In the blow example CTE named EmpCTE referenced within to itself to join with Employees table.&lt;br /&gt;&lt;br /&gt;WITH EmpCTE(empid, empname, mgrid, lvl)&lt;br /&gt;AS&lt;br /&gt;( &lt;br /&gt;&lt;br /&gt;  -- Anchor Member (AM)&lt;br /&gt;  SELECT empid, empname, mgrid, 0&lt;br /&gt;  FROM Employees&lt;br /&gt;  WHERE empid = 7&lt;br /&gt;  UNION ALL&lt;br /&gt;  &lt;br /&gt;  -- Recursive Member (RM)&lt;br /&gt;  SELECT E.empid, E.empname, E.mgrid, M.lvl+1&lt;br /&gt;  FROM Employees AS E&lt;br /&gt;    JOIN EmpCTE AS M&lt;br /&gt;      ON E.mgrid = M.empid&lt;br /&gt;)&lt;br /&gt;SELECT * FROM EmpCTE&lt;br /&gt;&lt;br /&gt;Finally,in order to resolve the problem you need that CTE and user table have different names. Something like that&lt;br /&gt;&lt;br /&gt;WITH Batch_cte AS&lt;br /&gt;(&lt;br /&gt; SELECT *, ROW_NUMBER() OVER(ORDER BY Batch, Status) AS RowNum&lt;br /&gt; FROM Batch&lt;br /&gt;)&lt;br /&gt;DELETE FROM Batch_cte&lt;br /&gt;WHERE RowNum=1</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/2721912155539005737/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/5334232245085456076/2721912155539005737' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/2721912155539005737'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/2721912155539005737'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2011/04/do-not-assing-same-name-to-cte.html' title='Name of CTE and referenced table should be different'/><author><name>Uri Dimant</name><uri>http://www.blogger.com/profile/17252545378139162559</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-4266367723522025519</id><published>2011-04-04T01:55:00.000-07:00</published><updated>2011-04-04T01:59:49.622-07:00</updated><title type='text'>MVP Award for 2011 year.</title><content type='html'>I just got my MVP renewal email from MS, so this is my 5th award for SQL MVP since 2007. &lt;br /&gt;I would say thanks for all support and good communication with all MVP around the world and hopefully it could make me more motivated to give more contribution for the community</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/4266367723522025519/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/5334232245085456076/4266367723522025519' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/4266367723522025519'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/4266367723522025519'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2011/04/mvp-award-for-2011-year.html' title='MVP Award for 2011 year.'/><author><name>Uri Dimant</name><uri>http://www.blogger.com/profile/17252545378139162559</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-1608254304153815669</id><published>2011-03-30T03:27:00.001-07:00</published><updated>2011-03-30T03:30:49.985-07:00</updated><title type='text'>To someone who specializes in SQL Server performance tuning</title><content type='html'>Must read Conor&#39;s blog&lt;br /&gt;http://blogs.msdn.com/b/conor_cunningham_msft/</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/1608254304153815669/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/5334232245085456076/1608254304153815669' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/1608254304153815669'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/1608254304153815669'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2011/03/to-someone-who-specializes-in-sql.html' title='To someone who specializes in SQL Server performance tuning'/><author><name>Uri Dimant</name><uri>http://www.blogger.com/profile/17252545378139162559</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-7154174935546051608</id><published>2011-02-06T00:39:00.000-08:00</published><updated>2011-02-06T00:40:19.001-08:00</updated><title type='text'>Alias issue in T-SQL or defensive programming</title><content type='html'>Recently I have talked to our developer who wanted to delete TOP x rows from the table. I pointed him to the below artcile http://blogs.msdn.com/b/sqlcat/archive/2009/05/21/fast-ordered-delete.aspx where a tip – a view with ORDER BY.&lt;br /&gt;&lt;br /&gt;As alternative he wanted using a derived table but cannot understand why all rows are deleted from the table instead of TOP(x). See the below demo.&lt;br /&gt;&lt;br /&gt;create table #t (c int)&lt;br /&gt;insert into #t values (1)&lt;br /&gt;insert into #t values (1)&lt;br /&gt;insert into #t values (2)&lt;br /&gt;insert into #t values (3)&lt;br /&gt;insert into #t values (3)&lt;br /&gt;&lt;br /&gt;delete #t from (select top (2) c&lt;br /&gt;        from t order by c) t&lt;br /&gt;&lt;br /&gt;How does  DELETE extension in T-SQL work?. The FROM clause after the DELETE specifies the target table to delete. The second optional FROM clause specifies the qualifying rows. But if I change &#39;t&#39; alias to &#39;#t&#39; as original name  of the temporary table that would work...&lt;br /&gt;&lt;br /&gt;delete #t from (select top (2) c&lt;br /&gt;        from t order by c) #t&lt;br /&gt;&lt;br /&gt;Now, SQL Server &#39;sees&#39; that derived table has the same name as a target and thus deletes only TOP(x) rows&lt;br /&gt;&lt;br /&gt;There is no goal of this post to get into a discussion about how to write correlated subquery to perform such operations, I just wanted you to pay attention on if you choose using derived tables to perform deletion please make sure  that alias you specify for derived table is the same as a target table..&lt;br /&gt;&lt;br /&gt;PS. If you are testing and not sure about the result please use BEGIN TRAN... before executing the script.If you see that rows affected by the script is too many issue ROLLBACK TRAN to back  to original data.</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/7154174935546051608/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/5334232245085456076/7154174935546051608' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/7154174935546051608'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/7154174935546051608'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2011/02/alias-issue-in-t-sql-or-defensive.html' title='Alias issue in T-SQL or defensive programming'/><author><name>Uri Dimant</name><uri>http://www.blogger.com/profile/17252545378139162559</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-1911603259663861776</id><published>2011-01-11T22:57:00.000-08:00</published><updated>2011-01-11T23:48:22.216-08:00</updated><title type='text'>Dedup on huge table</title><content type='html'>Hi friends&lt;br /&gt;At this time I would like to share with you my experience to delete duplicates in very large table(800 million of rows). &lt;br /&gt;A very general method is to use ROW_NUMBER function to PARTITION ON desired columns and then filter out only unique data.&lt;br /&gt;&lt;br /&gt;WITH cte&lt;br /&gt;AS&lt;br /&gt;(&lt;br /&gt;SELECT &lt;columns&gt;,ROW_NUMBER() OVER (PARTITION BY &lt;columns&gt; ORDER BY &lt;columns&gt;) rn&lt;br /&gt;FROM tbl&lt;br /&gt;) SELECT * FROM cte WHERE rn=1&lt;br /&gt;&lt;br /&gt;As you imagine on huge table it will take too long. In order to optimize that query I used batch processing (divided  that transaction into small chunks)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;DECLARE @x INT&lt;br /&gt;SET @x = 1&lt;br /&gt;WHILE @x &lt; 44,000,000  -- Set appropriately&lt;br /&gt;BEGIN&lt;br /&gt;&lt;br /&gt;    ;WITH cte&lt;br /&gt;    AS&lt;br /&gt;    (&lt;br /&gt;     SELECT &lt;columns&gt;,ROW_NUMBER() OVER (PARTITION BY &lt;columns&gt; ORDER BY &lt;columns&gt;) rn&lt;br /&gt;     FROM tbl WHERE ID BETWEEN @x AND @x + 10000&lt;br /&gt;    )SELECT * FROM cte WHERE rn=1 &lt;br /&gt;&lt;br /&gt;    SET @x = @x + 10000&lt;br /&gt;END&lt;br /&gt;Ok ,it worked much faster as we have a clustered index on ID column such as SQL Server uses it to get the data based on defined range.However, we have another problem that data we are getting back is not actually unique.You see that for specific range ,say (from 1 to 10000) I get the data based on required partition and filter out for rn=1, BUT it is possible that the same row will occur in the next chunk (from 10001 to 20000) and we will also get it back because SQL Server does not recognize it as duplicate we have already got from the first chunk.&lt;br /&gt;&lt;br /&gt;More reliable solution is checking on entire table and not to base on ranges.&lt;br /&gt;SELECT &lt;columns&gt;,COUNT(*) rn FROM &lt;br /&gt;tbl GROUP BY &lt;columns&gt; &lt;br /&gt;HAVING COUNT(*)&gt;1&lt;br /&gt;&lt;br /&gt;As you can see it could take long time ,so I also tried to create a Dedupt table with a key IGNORE_DUP_KEY option (thanks to Hugo) but insert into the table was pretty slow as well. Peter Larsson a fellow MVP has suggested  the below technique that worked pretty well&lt;br /&gt;&lt;br /&gt;CREATE TABLEe #unique (id primary key clustered)&lt;br /&gt;INSERT INTO #temp (id) SELECT  MIN(ID) AS ID&lt;br /&gt;FROM  tbl&lt;br /&gt;GROUP BY       Col1, col2, col3... (here you decide the uniqueness)&lt;br /&gt;&lt;br /&gt;--And then insert into batches for &lt;br /&gt;&lt;br /&gt;SET @id = 0&lt;br /&gt;WHILE @id &lt; (800 million or more)&lt;br /&gt;BEGIN &lt;br /&gt;        SELECT  t1.ID,   t1.Col&lt;br /&gt;        FROM    dbo.Table1 AS T1&lt;br /&gt;        INNER JOIN #unique as u ONu.id = t1.id&lt;br /&gt;        AND u.id BETWEEN @id AND @id +99999&lt;br /&gt;&lt;br /&gt;        SET @id += 100000&lt;br /&gt;END&lt;br /&gt;&lt;br /&gt;It would be great it put back here testing results.</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/1911603259663861776/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/5334232245085456076/1911603259663861776' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/1911603259663861776'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/1911603259663861776'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2011/01/dedup-on-huge-table.html' title='Dedup on huge table'/><author><name>Uri Dimant</name><uri>http://www.blogger.com/profile/17252545378139162559</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-7753055902271366703</id><published>2010-12-16T02:28:00.001-08:00</published><updated>2010-12-16T02:31:07.486-08:00</updated><title type='text'>What business says does not mean what businees wants</title><content type='html'>This great sentence I learned form my exerience being a consultant &lt;br /&gt;&lt;br /&gt;&lt;strong&gt;What business says does not mean what businees wants&lt;/strong&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/7753055902271366703/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/5334232245085456076/7753055902271366703' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/7753055902271366703'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/7753055902271366703'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2010/12/what-business-says-does-not-mean-what.html' title='What business says does not mean what businees wants'/><author><name>Uri Dimant</name><uri>http://www.blogger.com/profile/17252545378139162559</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-3964748768084419921</id><published>2010-11-23T01:19:00.001-08:00</published><updated>2010-11-23T01:23:32.274-08:00</updated><title type='text'>TechEd 2010 in Eilat</title><content type='html'>Hi&lt;br /&gt;I am going to attend TechEd 2010 in Eilat next week. It is great opportunity to learn new things , meet new and old friends.It is my second TechEd and I am will be focusing on Data Platform direction and BI. Hope to see you there.&lt;br /&gt;&lt;br /&gt;http://www.microsoft.com/israel/TechEd2010/Tracks/BI.aspx</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/3964748768084419921/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/5334232245085456076/3964748768084419921' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/3964748768084419921'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/3964748768084419921'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2010/11/teched-2010-in-eilat.html' title='TechEd 2010 in Eilat'/><author><name>Uri Dimant</name><uri>http://www.blogger.com/profile/17252545378139162559</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-4309931188795275903</id><published>2010-11-08T23:26:00.001-08:00</published><updated>2010-11-09T00:01:06.185-08:00</updated><title type='text'>Blobs and covering indexes</title><content type='html'>Nowadays it is common to store/save pictures,documents in the databbase. Since SQL Server 2005 we use VARBINARY(MAX) datatype to store such data. The &#39;problem&#39; I have seen recently at the client database is SELECT statement on very huge table that contain BLOB data  and that data needs to be return to the client works pretty slowly. Run the below &lt;br /&gt;SET STATISTICS IO ON &lt;br /&gt;&lt;br /&gt;SELECT col1,col2, blobdata FROM tbl WHERE.... &lt;br /&gt;&lt;br /&gt;Table &#39;tbl&#39;. Scan count 1, logical reads 8125, physical reads 0, read-ahead reads 0, lob logical reads 261521, lob physical reads 0, lob read-ahead reads 0.&lt;br /&gt;CPU time = 1252 ms, elapsed time = 12632 ms.&lt;br /&gt;&lt;br /&gt;We see high number of reads to return the data from index/blob pages.&lt;br /&gt;&lt;br /&gt;Execution plan shows that there is Bookmark to return the BLOB data from Clustered Index Key because of our NCI(NonClustered Index) does not cover all columns in SELECT statement. &lt;br /&gt;Ok, at first glance  you would re-create NCI to INCLUDE  blobdata column to &#39;cover&#39; SELECT ...Well, I noticed that recreating NCI index takes long long time and LOG file was grown dramatically. I see that after INCLUDE blobdata column I reduced logical reads as shown below&lt;br /&gt;&lt;br /&gt;Table &#39;tbl&#39;. Scan count 1, logical reads 14, physical reads 0, read-ahead reads 0, lob logical reads 421521, lob physical reads 0, lob read-ahead reads 0.&lt;br /&gt;CPU time = 1622 ms, elapsed time = 16512 ms.&lt;br /&gt;&lt;br /&gt;But what happened? BLOB page reads were done almost 1.8 times more with covering index and actually I have not seen much improvment in performance of the query.&lt;br /&gt;So finally, if you create an index or INCLUDE the BLOBs SQL Server create copy of that index for every blob column and thus it takes time to create index and reading the data from index page. Well, you were able to save some IO by covering BLOB column but internally SQL Server works much hardly to return the data and maintain the index.</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/4309931188795275903/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/5334232245085456076/4309931188795275903' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/4309931188795275903'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/4309931188795275903'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2010/11/blobs-and-covering-indexes.html' title='Blobs and covering indexes'/><author><name>Uri Dimant</name><uri>http://www.blogger.com/profile/17252545378139162559</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry></feed>