<?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: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>2012-01-24T18:21:37.826-08: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?max-results=100'/><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=101&amp;max-results=100'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>113</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><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/&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-3543275487993090868?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</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.g?blogID=5334232245085456076&amp;postID=3543275487993090868' title='0 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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</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='' &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="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://4.bp.blogspot.com/-19jkWJ-OQ5s/Tt87RsTYmpI/AAAAAAAAAAQ/OX5dTQYffdQ/s1600/1.jpg"&gt;&lt;img style="display:block; margin:0px auto 10px; text-align:center;cursor:pointer; cursor:hand;width: 278px; height: 320px;" src="http://4.bp.blogspot.com/-19jkWJ-OQ5s/Tt87RsTYmpI/AAAAAAAAAAQ/OX5dTQYffdQ/s320/1.jpg" border="0" alt=""id="BLOGGER_PHOTO_ID_5683326429901134482" /&gt;&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;SQL Server will implicitly convert '' to REAL datatype with 0 and a result is 1 row to be return.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-545912967486325930?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</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.g?blogID=5334232245085456076&amp;postID=545912967486325930' title='0 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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://4.bp.blogspot.com/-19jkWJ-OQ5s/Tt87RsTYmpI/AAAAAAAAAAQ/OX5dTQYffdQ/s72-c/1.jpg' height='72' width='72'/><thr:total>0</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 ('122')&lt;br /&gt;INSERT INTO #t VALUES ('4545')&lt;br /&gt;INSERT INTO #t VALUES ('4545/454')&lt;br /&gt;INSERT INTO #t VALUES ('4899')&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 '%[/]%' &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 '%[^0-9]%' 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="font-weight:bold;"&gt;Predicate&lt;/span&gt; &lt;br /&gt;CONVERT(int,[tempdb].[dbo].[#t].[c],0)&gt;(10) AND NOT [tempdb].[dbo].[#t].[c] like '%[/]%'&lt;br /&gt;&lt;br /&gt;Thinking that we filter out all "bad" 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 "bad" rows  CASE...=1 we see that SQL Server really filters out "bad" rows and now CAST is working.&lt;br /&gt;&lt;span style="font-weight:bold;"&gt;Predicate&lt;/span&gt;&lt;br /&gt;CASE WHEN [tempdb].[dbo].[#t].[c] like '%[^0-9]%' 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)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-3194956118279632280?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</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.g?blogID=5334232245085456076&amp;postID=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='http://img2.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="font-weight:bold;"&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 'allow updates' to 0 , see below script&lt;br /&gt;&lt;br /&gt;EXEC sp_configure 'allow updates',0&lt;br /&gt;reconfigure&lt;br /&gt;&lt;br /&gt;Now everything got back to work in the 'right' place.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-1273225245675312867?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</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.g?blogID=5334232245085456076&amp;postID=1273225245675312867' title='1 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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</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'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'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'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;*/&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-4863169538282858902?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&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.g?blogID=5334232245085456076&amp;postID=4863169538282858902' title='0 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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</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&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-6429587960079129040?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</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.g?blogID=5334232245085456076&amp;postID=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='http://img2.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,'n1')&lt;br /&gt;insert into db1.dbo.t1 values(2,'n2')&lt;br /&gt;insert into db1.dbo.t1 values(3,'N3')&lt;br /&gt;insert into db1.dbo.t2 values (10,'n1')&lt;br /&gt;insert into db1.dbo.t2 values(15,'n2')&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 'db1.dbo.t2' allows values that are not allowed by check constraints or partition function on target table 'db1.dbo.t1'.&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)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-9166013831370234889?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</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.g?blogID=5334232245085456076&amp;postID=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='http://img2.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'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 "strange behaviour" 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'RESTORE LABELONLY FROM DISK=@P1',N'@P1 nchar(27)',N'X:\GBDB\Log\log080411PM.bak'&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 'X:\GBDB\Log\log040411PM.bak' 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 ?&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-7575017443541660253?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</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.g?blogID=5334232245085456076&amp;postID=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&apos;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='http://img2.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 ('A',1)&lt;br /&gt;INSERT INTO Batch VALUES ('B',2)&lt;br /&gt;INSERT INTO Batch VALUES ('C',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 'Batch' does not contain a top-level UNION ALL operator.&lt;br /&gt;&lt;br /&gt;SQL Server "thinks" 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&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-2721912155539005737?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</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.g?blogID=5334232245085456076&amp;postID=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='http://img2.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&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-4266367723522025519?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</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.g?blogID=5334232245085456076&amp;postID=4266367723522025519' title='1 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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</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's blog&lt;br /&gt;http://blogs.msdn.com/b/conor_cunningham_msft/&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-1608254304153815669?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</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.g?blogID=5334232245085456076&amp;postID=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='http://img2.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 't' alias to '#t' 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 'sees' 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.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-7154174935546051608?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</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.g?blogID=5334232245085456076&amp;postID=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='http://img2.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.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-1911603259663861776?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</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.g?blogID=5334232245085456076&amp;postID=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='http://img2.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;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-7753055902271366703?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&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.g?blogID=5334232245085456076&amp;postID=7753055902271366703' title='0 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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</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&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-3964748768084419921?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</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.g?blogID=5334232245085456076&amp;postID=3964748768084419921' title='0 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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</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 'problem' 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 'tbl'. 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 'cover' 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 'tbl'. 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.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-4309931188795275903?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</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.g?blogID=5334232245085456076&amp;postID=4309931188795275903' title='2 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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-3230489593929885855</id><published>2010-10-03T05:39:00.001-07:00</published><updated>2010-10-03T06:06:56.881-07:00</updated><title type='text'>Sometimes it is important to choose right collation for the server</title><content type='html'>The title looks like a joke, am I right? Sometimes we just take care of collation for specific database. Well that may lead you to unpleasant situation and moreover getting wrong or no data at all. Recently I helped my friend to solve the problem for his new software. He created a database with "right" collation let say Hebrew_CI_AS and the program searches for the data that contains hebrew characters. Suddenly the program does not return the data at all. He showed me that the data he is looking for are presented in the table and point me to the stored procedure that performs the searching. You have already guessed what was the problem, right? The problem was because withing a stored procedure he used a temporary table where the programm stores the data for final result  (VARCHAR(n) for search string) for some logic. As you know tempdb is a system database which inherits a server's collation (SQL_Latin1_General_CP1_CI_AS) and hence no data were returned. In that specific case we changed the data type to NVARCHAR(n) and there is no need to reinstall the entire server, but think about the program(I have seen many of such) that such technique (store unicode character in temporary tables) are used in hundred of stored procedures and change the datatype for all of them is very big headache.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-3230489593929885855?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/3230489593929885855/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=3230489593929885855' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/3230489593929885855'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/3230489593929885855'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2010/10/sometimes-it-is-important-to-choose.html' title='Sometimes it is important to choose right collation for the server'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-99129461350087114</id><published>2010-09-19T02:36:00.001-07:00</published><updated>2010-09-19T02:41:34.845-07:00</updated><title type='text'>Window Functions (OVER Clause) – Help Make a Difference</title><content type='html'>Hello friends&lt;br /&gt;&lt;br /&gt;As you probably know those days SQL Server 2011 is being developed. Itzik Ben-Gan believes (me too) that having those new window functions and enhance existing ones will benefit all of us SQL Server developers in the future more complete support for the standard window functions in SQL Server. Please read and vote..&lt;br /&gt;&lt;br /&gt;http://www.sqlmag.com/blogs/puzzled-by-t-sql/tabid/1023/entryid/13085/Window-Functions-OVER-Clause-Help-Make-a-Difference.aspx&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-99129461350087114?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/99129461350087114/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=99129461350087114' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/99129461350087114'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/99129461350087114'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2010/09/window-functions-over-clause-help-make.html' title='Window Functions (OVER Clause) – Help Make a Difference'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-991642607408457068</id><published>2010-09-02T02:22:00.001-07:00</published><updated>2010-09-02T02:32:56.810-07:00</updated><title type='text'>Be careful to grant dbCreator server role to the user</title><content type='html'>It is common that vendors ask for permission to create databases (or they applications need) on your servers and most of DBAs I have seen immediately grant them dbCreator server role. But they are not aware that members of that role are able to DROP/ALTER any databases on the entire server regardless of whether or not you even have a user account in the database.Did you really want that?? &lt;br /&gt;&lt;br /&gt;The right approach is to grant CREATE ANY DATABASE permission and then the user is able to DROP/ALTER he/she owns.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-991642607408457068?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/991642607408457068/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=991642607408457068' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/991642607408457068'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/991642607408457068'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2010/09/be-careful-to-grant-dbcreator-server.html' title='Be careful to grant dbCreator server role to the user'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-385736612681390385</id><published>2010-08-18T21:20:00.001-07:00</published><updated>2010-08-18T21:27:24.493-07:00</updated><title type='text'>Another way to execute stored procedure</title><content type='html'>Just "discovered" that if you right click on the stored procedure you can clcik "Execute Stored Procedure" option. It gives you friendly GUI to provide parameters,however,as a data professional, I prefer to execute stored procedures in query builder. But it is nice feature for less experienced users.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-385736612681390385?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/385736612681390385/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=385736612681390385' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/385736612681390385'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/385736612681390385'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2010/08/another-way-to-execute-stored-procedure.html' title='Another way to execute stored procedure'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-7969632329284176402</id><published>2010-07-13T04:07:00.001-07:00</published><updated>2010-07-13T04:07:47.735-07:00</updated><title type='text'>Find dependency task again</title><content type='html'>If you have ever dealt with projects where you need to script out the objects and the recreate them on the destination database  you first always have been thinking of sys.sql_dependencies system table (if you are on SQL Server 2005 and onwards) . At our company we have a project to create script  on many client machines that have variuos versions of SQL Server Express Edition from SQL Server 2000  to SQL  Server 2008 R2. The team has automated script that creates all objects on the client  by using old syscomments table. They could not rely on sys.dependencies table because it has some critical bugs as  you are aware of. They did script out all views into a text file and run it for more than once because it is possoble that for the first run it is failed (if you have nested views.).It is very complicated script and in narrow down we can reproduce the problem as the following.&lt;br /&gt;&lt;br /&gt;USE AdventureWorks&lt;br /&gt;GO&lt;br /&gt;CREATE TABLE t1 (c INT)&lt;br /&gt;GO&lt;br /&gt;CREATE VIEW v1&lt;br /&gt;AS&lt;br /&gt;SELECT c FROM v2&lt;br /&gt;GO&lt;br /&gt;/*&lt;br /&gt;&lt;br /&gt;Msg 208, Level 16, State 1, Procedure v1, Line 3&lt;br /&gt;Invalid object name 'v2'&lt;br /&gt;*/&lt;br /&gt;CREATE VIEW v2&lt;br /&gt;AS&lt;br /&gt;SELECT * FROM t1&lt;br /&gt;--Clean up&lt;br /&gt;DROP VIEW v1,v2&lt;br /&gt;DROP TABLE t1&lt;br /&gt;&lt;br /&gt;It worked well for a while but very slowely and they look for improvment. I would like to share with you very reliable and fast option developed by colleague of mine by using VB.NET. Please test it before  running on production server.&lt;br /&gt;&lt;br /&gt;This script reads the view definition, find the level of dependecy, and then set priority to create.&lt;br /&gt;&lt;br /&gt;Var&lt;br /&gt;_dsV – Data table of views and column of “PL” – Place or priority&lt;br /&gt;_dsD  - data table of dependences (view on view ) &lt;br /&gt;_dsC –context of views&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Function&lt;br /&gt;CreateDependence(_dsV.Tables(0), _dsC.Tables(0), _dsD)  - find on context  of view if exist other views&lt;br /&gt;CheckChildViews(_dsV.Tables(0), _dsD, _dsD.Select(), 0) -  recursive  function which set priority of views.&lt;br /&gt;On the start all view with priority 1000 (it’s don’t have dependence) . When view has some dependence then it gets priority = 0  or parent priority + 1. All child views (nested view) get higher among (parent priority +1) or its priority. &lt;br /&gt;GetViews(_dsV.Tables(0), _dsC.Tables(0)) -  get all view by sort of priority descending&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Private Sub CreateViews() &lt;br /&gt;        Dim cldb As clDB ‘  class -  connection  to database&lt;br /&gt;        Dim _dsV As DataSet&lt;br /&gt;        Dim _dsD As New DataTable("DEP")&lt;br /&gt;        Dim _dsC As DataSet&lt;br /&gt;        lblResultSqlScript.Text = ""&lt;br /&gt;        Try&lt;br /&gt;            cldb = New clDB&lt;br /&gt;            cldb.ConnectionString = conectionString&lt;br /&gt;            Dim tmp As String = ""&lt;br /&gt;            'all views&lt;br /&gt;            tmp = " SELECT  ID,NAME,1000 as PL "&lt;br /&gt;            tmp &amp;= " FROM dbo.sysobjects   "&lt;br /&gt;            tmp &amp;= " WHERE xtype = 'V'   and category=0  "&lt;br /&gt;            tmp &amp;= " order by name  " &amp; vbNewLine&lt;br /&gt;            _dsV = cldb.GetDataSet(tmp)&lt;br /&gt;&lt;br /&gt;            'context of views&lt;br /&gt;            tmp = " SELECT text ,COLID,ID "&lt;br /&gt;            tmp &amp;= " FROM syscomments "&lt;br /&gt;            tmp &amp;= " ORDER BY COLID " &amp; vbNewLine&lt;br /&gt;            _dsC = cldb.GetDataSet(tmp)&lt;br /&gt; &lt;br /&gt;            'depandences&lt;br /&gt;            CreateDependence(_dsV.Tables(0), _dsC.Tables(0), _dsD)&lt;br /&gt; &lt;br /&gt;            ClearIndex(_dsV.Tables(0), _dsD)&lt;br /&gt;            CheckChildViews(_dsV.Tables(0), _dsD, _dsD.Select(), 0)&lt;br /&gt;            -----GetViews(_dsV.Tables(0), _dsC.Tables(0))&lt;br /&gt;           RunViews (_dsV.Tables(0), _dsC.Tables(0))&lt;br /&gt;        Catch ex As System.Exception&lt;br /&gt;        Finally&lt;br /&gt;            cldb = Nothing&lt;br /&gt;        End Try&lt;br /&gt;   End Sub&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;    Private Sub CreateDependence(ByVal dsV As DataTable, ByVal dsC As DataTable, ByRef dt As DataTable)&lt;br /&gt;        'create datatable&lt;br /&gt;        Dim cl1 As New DataColumn("PNAME", System.Type.GetType("System.String"))&lt;br /&gt;        Dim cl2 As New DataColumn("PID", System.Type.GetType("System.Int32"))&lt;br /&gt;        Dim cl3 As New DataColumn("CNAME", System.Type.GetType("System.String"))&lt;br /&gt;        Dim cl4 As New DataColumn("CID", System.Type.GetType("System.Int32"))&lt;br /&gt;        dt.Columns.Add(cl1)&lt;br /&gt;        dt.Columns.Add(cl2)&lt;br /&gt;        dt.Columns.Add(cl3)&lt;br /&gt;        dt.Columns.Add(cl4)&lt;br /&gt;        Dim cur As Integer = -1&lt;br /&gt;        Dim tmp As String = ""&lt;br /&gt; &lt;br /&gt;        'load view&lt;br /&gt;        Dim bs As New Text.StringBuilder&lt;br /&gt;        For Each ddr As DataRow In dsV.Select("", "PL DESC")&lt;br /&gt;            Try&lt;br /&gt;                bs.Length = 0&lt;br /&gt;                For Each dr As DataRow In dsC.Select("ID=" &amp; ddr("ID"), "COLID")&lt;br /&gt;                    Try&lt;br /&gt;                        bs.Append(dr("TEXT").ToString)&lt;br /&gt;                    Catch ex As System.Exception&lt;br /&gt;                    End Try&lt;br /&gt;                Next&lt;br /&gt;                'find dependence &lt;br /&gt;                'loop on all view without current&lt;br /&gt;                For Each ddr1 As DataRow In dsV.Select("ID&lt;&gt;" &amp; ddr("ID"), "PL DESC")&lt;br /&gt;                    cur = bs.ToString.IndexOf(ddr1("NAME").ToString)&lt;br /&gt;                    If cur &gt; -1 Then&lt;br /&gt;                        'check if it's real name and not peace of name&lt;br /&gt;                        tmp = bs.ToString.Substring(cur + ddr1("NAME").ToString.Length, 1)&lt;br /&gt;                        If tmp = "" Or tmp = "." Or tmp = " " Or Asc(tmp) = 13 Then&lt;br /&gt;                            'add to dependence&lt;br /&gt;                            Dim newdr As DataRow = dt.NewRow&lt;br /&gt;                            newdr("PNAME") = ddr("NAME")&lt;br /&gt;                            newdr("PID") = ddr("ID")&lt;br /&gt;                            newdr("CNAME") = ddr1("NAME")&lt;br /&gt;                            newdr("CID") = ddr1("ID")&lt;br /&gt;                            dt.Rows.Add(newdr)&lt;br /&gt;                        End If&lt;br /&gt;                    End If&lt;br /&gt;                Next&lt;br /&gt;            Catch ex As System.Exception&lt;br /&gt;            End Try&lt;br /&gt;        Next&lt;br /&gt;    End Sub&lt;br /&gt;    Private Sub GetViews(ByVal dsV As DataTable, ByVal dsC As DataTable)&lt;br /&gt;&lt;br /&gt;        txtSQLScript.Text = ""&lt;br /&gt;        Dim bs As New Text.StringBuilder&lt;br /&gt;        bs.Append(" SET QUOTED_IDENTIFIER OFF    " &amp; vbNewLine)&lt;br /&gt;        bs.Append(" GO " &amp; vbNewLine)&lt;br /&gt;        bs.Append(" SET ANSI_NULLS OFF    " &amp; vbNewLine)&lt;br /&gt;        bs.Append(" GO " &amp; vbNewLine)&lt;br /&gt;        For Each ddr As DataRow In dsV.Select("", "PL DESC")&lt;br /&gt;            bs.Append(" IF EXISTS (select * from dbo.sysobjects where id = object_id(N'" &amp; ddr("NAME") &amp; "') and OBJECTPROPERTY(id, N'IsView') = 1)" &amp; vbNewLine)&lt;br /&gt;            bs.Append(" DROP VIEW " &amp; ddr("NAME") &amp; vbNewLine)&lt;br /&gt;            bs.Append(" GO " &amp; vbNewLine)&lt;br /&gt; &lt;br /&gt;            For Each dr As DataRow In dsC.Select("ID=" &amp; ddr("ID"))&lt;br /&gt;                Try&lt;br /&gt;                    bs.Append(dr("TEXT").ToString)&lt;br /&gt; &lt;br /&gt;                Catch ex As System.Exception&lt;br /&gt;                End Try&lt;br /&gt;            Next&lt;br /&gt;            bs.Append(vbNewLine &amp; " GO " &amp; vbNewLine)&lt;br /&gt;            bs.Append(vbNewLine &amp; "--------------------------------" &amp; vbNewLine)&lt;br /&gt;        Next&lt;br /&gt;        txtSQLScript.Text = bs.ToString&lt;br /&gt;    End Sub&lt;br /&gt;    Private Sub ClearIndex(ByRef dsV As DataTable, ByVal dsd As DataTable)&lt;br /&gt;        For Each ddr As DataRow In dsV.select&lt;br /&gt;            If dsd.Select("PID=" &amp; ddr("ID").ToString).Length &gt; 0 Then&lt;br /&gt;                ddr("PL") = 0&lt;br /&gt;            End If&lt;br /&gt;        Next&lt;br /&gt;    End Sub&lt;br /&gt; &lt;br /&gt;    Private Sub CheckChildViews(ByRef dsV As DataTable, _&lt;br /&gt;                        ByVal dsd As DataTable, _&lt;br /&gt;                        ByVal dr() As DataRow, ByVal index As Integer)&lt;br /&gt;        For Each ddr As DataRow In dr&lt;br /&gt;            Try&lt;br /&gt;                Dim pd As DataRow = dsV.Select("ID=" &amp; ddr("PID").ToString)(0)&lt;br /&gt;                If pd("PL") &lt; index Then pd("PL") = index&lt;br /&gt;                CheckChildViews(dsV, dsd, dsd.Select("PID=" &amp; ddr("CID").ToString), index + 1)&lt;br /&gt;            Catch ex As System.Exception&lt;br /&gt;            End Try&lt;br /&gt;        Next&lt;br /&gt;    End Sub&lt;br /&gt;&lt;br /&gt;Private Sub RunViews(ByVal dsV As DataTable, ByVal dsC As DataTable)&lt;br /&gt;        txtSQLScript.Text = ""&lt;br /&gt;        Dim bs As New Text.StringBuilder&lt;br /&gt;        Dim cldb As New dbAdministration.clDB&lt;br /&gt;        For Each ddr As DataRow In dsV.Select("", "PL DESC")&lt;br /&gt;            bs.Length = 0&lt;br /&gt;            bs.Append(" IF EXISTS (select * from dbo.sysobjects where id = object_id(N'" &amp; ddr("NAME") &amp; "') and OBJECTPROPERTY(id, N'IsView') = 1)" &amp; vbNewLine)&lt;br /&gt;            bs.Append(" DROP VIEW " &amp; ddr("NAME") &amp; vbNewLine)&lt;br /&gt;            cldb.Execute(bs.ToString()) 'run delete if exist view&lt;br /&gt; &lt;br /&gt;            bs.Length = 0&lt;br /&gt;          &lt;br /&gt;           For Each dr As DataRow In dsC.Select("ID=" &amp; ddr("ID"))&lt;br /&gt;                Try&lt;br /&gt;                    bs.Append(dr("TEXT").ToString)&lt;br /&gt;                Catch ex As System.Exception&lt;br /&gt;                End Try&lt;br /&gt;            Next&lt;br /&gt;            cldb.Execute(bs.ToString()) 'run create view&lt;br /&gt;        Next&lt;br /&gt;    End Sub&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-7969632329284176402?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/7969632329284176402/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=7969632329284176402' title='13 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/7969632329284176402'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/7969632329284176402'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2010/07/find-dependency-task-again.html' title='Find dependency task 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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>13</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-1591166282173930243</id><published>2010-06-08T04:52:00.000-07:00</published><updated>2010-06-08T05:01:16.350-07:00</updated><title type='text'>Column level permission</title><content type='html'>Uh,it's so hard to back to work from the vacation:-).Yesterday,for the first time, I have been faced the requirement to implement a column level permission on the table, have you done something before? A little script below demonstrates it.&lt;br /&gt;&lt;br /&gt;USE dbname;&lt;br /&gt;CREATE TABLE demo (c INT, c2 INT)&lt;br /&gt;&lt;br /&gt;GRANT SELECT (c) ON  demo TO test&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;EXECUTE AS USER = 'test';&lt;br /&gt;&lt;br /&gt;SELECT * FROM fn_my_permissions('dbo.demo', 'OBJECT') &lt;br /&gt;    ORDER BY subentity_name, permission_name ;&lt;br /&gt;&lt;br /&gt;REVERT&lt;br /&gt;&lt;br /&gt;PS: subentity_name tells you on what column given permission.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-1591166282173930243?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/1591166282173930243/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=1591166282173930243' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/1591166282173930243'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/1591166282173930243'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2010/06/column-level-permission.html' title='Column level permission'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-315609887485748000</id><published>2010-05-16T05:46:00.001-07:00</published><updated>2010-05-16T05:54:43.878-07:00</updated><title type='text'>It's The Time To Go on Vacation</title><content type='html'>Yep, on 21st May I am going to Moscow to see a World Table Tennis Championship. I love playing table tennis and even took a part of two World Table Tennis Championships in earliest 90. So I hope to meet there may old friends from all around the world, so I am very exited about it.. See you later on.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-315609887485748000?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/315609887485748000/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=315609887485748000' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/315609887485748000'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/315609887485748000'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2010/05/its-time-to-go-on-vacation.html' title='It&apos;s The Time To Go on Vacation'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-5883507320478787128</id><published>2010-05-07T00:53:00.000-07:00</published><updated>2010-05-07T01:07:49.639-07:00</updated><title type='text'>Kalen Delaney's seminar</title><content type='html'>Just backed home from the three days of seminar in Tel-Aviv named "Deep dive into SQL Server Internals with Kalen Delaney".&lt;br /&gt;Two days Kalen explained about SQL Server Query Plans,Tuning and Recompile techniques and the last one  was about SQL Server Data Storage Formats.&lt;br /&gt; &lt;br /&gt;You know , I thought I know a lot about SQL Server performnace tunning and etc, however, attending that seminar I just realized how much I need to learn. There are lot if tips,thoughts how to tune the system, indexes and etc.&lt;br /&gt;&lt;br /&gt;I would also mention about how easy was to understand Kalen (she spoke english at seminar :-)) and her methods to provide courses which is so important for attendants.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-5883507320478787128?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/5883507320478787128/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=5883507320478787128' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/5883507320478787128'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/5883507320478787128'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2010/05/kalen-delaneys-seminar.html' title='Kalen Delaney&apos;s seminar'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-8008225878699993426</id><published>2010-04-12T04:25:00.002-07:00</published><updated>2010-04-13T03:57:31.646-07:00</updated><title type='text'>Don't speak to the DBA while he/she is doing the job.</title><content type='html'>Our DBA was very busy today, helped out our developers top write efficient code,explained to the programmer how to launch the old DTS package on SQL Server 2005 and etc. You know how it is, someone comes in and asking the question and you answer the question at the same time you are doing the work. &lt;br /&gt;So,today answering one such questions our DBA deleted by mistake very ctitical database. Fortunately , we had zero data loss thanks to our backup/recovery strategy.&lt;br /&gt;&lt;br /&gt;I think that is acceptable to say that you are busy right now please ask the question later on, what do you think?&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-8008225878699993426?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/8008225878699993426/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=8008225878699993426' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/8008225878699993426'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/8008225878699993426'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2010/04/dont-speak-to-dba-while-heshe-is-doing.html' title='Don&apos;t speak to the DBA while he/she is doing the job.'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-8883439151751902718</id><published>2010-03-16T00:34:00.002-07:00</published><updated>2010-03-16T00:55:50.057-07:00</updated><title type='text'>Getting column level permision report</title><content type='html'>If you think about giving column level permission , you'll probably create a view with only needed columns to expose to ,then grant SELECT permission on that view and absolutely DENY access to underlaying tables to the user, right? Also , it depends on the policy of the company but you are able to grant SELECT/UPDATE/DELETE/INSERT operations on column level, have you ever used it:-)? So I recently visited our client who does perfom security permission on column level and wanted to know what are the columns and on what tables that specific user has access? I decided to perform the below testing, see if that might help you.&lt;br /&gt;&lt;br /&gt;CREATE TABLE demo (c INT, c2 INT)&lt;br /&gt;&lt;br /&gt;/* Grant SELECT permission on column named c on demo table*/&lt;br /&gt;GRANT SELECT (c) ON  demo TO test&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;EXECUTE AS USER = 'test';&lt;br /&gt;&lt;br /&gt;SELECT * FROM fn_my_permissions('dbo.demo', 'OBJECT') &lt;br /&gt;    ORDER BY subentity_name, permission_name ;&lt;br /&gt;&lt;br /&gt;REVERT&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-8883439151751902718?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/8883439151751902718/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=8883439151751902718' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/8883439151751902718'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/8883439151751902718'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2010/03/getting-column-level-permision-report.html' title='Getting column level permision report'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-2789124668007815164</id><published>2010-03-03T02:33:00.002-08:00</published><updated>2010-03-03T02:44:51.857-08:00</updated><title type='text'>Have you dealt with ENTER key in INSERT table?</title><content type='html'>I have a client who uses the application that allows to insert data but also typing ENTER key in the middle. Well, the question is how do you want to store the data  and later displaying on the client? He was not sure , so I showed him the below technique to display such kind of data....&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;--Create auxiliary table&lt;br /&gt;SELECT TOP 8000 n = IDENTITY(INT)&lt;br /&gt;  INTO Numbers&lt;br /&gt;  FROM syscolumns s1, syscolumns s2 ;&lt;br /&gt;&lt;br /&gt;CREATE TABLE test (col1 varchar(8000))&lt;br /&gt;&lt;br /&gt;INSERT INTO test values ( 'Row----------1&lt;br /&gt;Row--------2&lt;br /&gt;Row--3&lt;br /&gt;Row----4'&lt;br /&gt;)&lt;br /&gt;&lt;br /&gt;---See how it stores the data.&lt;br /&gt;SELECT * FROM TEST&lt;br /&gt;&lt;br /&gt;--Remove ENTER key&lt;br /&gt;SELECT SUBSTRING(col1, n, CHARINDEX(CHAR(13), col1 + CHAR(13), n) - n)&lt;br /&gt;  FROM Numbers, test&lt;br /&gt; WHERE SUBSTRING(CHAR(13) + col1,  n, 1) = CHAR(13)&lt;br /&gt;   AND n &lt; LEN(col1) + 1 ;&lt;br /&gt;&lt;br /&gt;--Cleanup&lt;br /&gt;DROP TABLE Numbers&lt;br /&gt;DROP TABLE test&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-2789124668007815164?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/2789124668007815164/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=2789124668007815164' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/2789124668007815164'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/2789124668007815164'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2010/03/have-you-dealt-with-enter-key-in-insert.html' title='Have you dealt with ENTER key in INSERT 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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-4920192665444600195</id><published>2010-02-03T04:53:00.002-08:00</published><updated>2010-02-03T05:01:53.033-08:00</updated><title type='text'>I was invited to blog on www.sqlblog.com</title><content type='html'>A few days ago Adam Machanic who is the owner of that great web site invited me to join his great company. I am really excited about and proud to be part of such great company of friends who write very good stuff about SQL Server. &lt;br /&gt;I would like to keep bloging here as well , I do not know how long it will take but you can always catch me up on http://sqlblog.com/blogs/uri_dimant/&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-4920192665444600195?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/4920192665444600195/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=4920192665444600195' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/4920192665444600195'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/4920192665444600195'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2010/02/i-was-invited-to-blog-on-wwwsqlblogcom.html' title='I was invited to blog on www.sqlblog.com'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-5267615768046250754</id><published>2010-01-12T03:53:00.000-08:00</published><updated>2010-01-12T04:25:44.037-08:00</updated><title type='text'>Think before unchecking sysadmin rights of BUILTIN\Administrators.</title><content type='html'>I have recently met our client who uchecked the sysadmin rights of BUILTIN\Administrators group before given any permissions to anotgher account. &lt;br /&gt;&lt;br /&gt;That was NOT such problem if the BUILTIN\Administrators group was removed from sysadmin role accidentally/by mistake, then you must login with another sysadmin login. If there is no other sysadmin login, you must login with SQL authentication as sa with the password that was set during setup to sa. Once logged in as a member of sysadmin, you are able to add BUILTIN\Admisnitrators back to sysadmin role.&lt;br /&gt;However everything above does not work for the client. Uhhh,the client also disabled SA accoount as well as DAC connection.&lt;br /&gt;Moreover, there is no domain controller where you can create a sysadmin domain acoount and grant the access to the machine running SQL Server,that was a stand alone computer with single instance installed on.&lt;br /&gt;&lt;br /&gt;The solution we found was to start SQL Server with single user mode. Using the single-user mode, SQL Server 2005 prevents a  Windows Administrator to abuse this privilege to act on behalf of the sysadmin without being noticed. This allows Windows Administrator accounts to perform certain maintenance tasks, such as installing patches. To someone who is not familiar how to start the instance in single user mode  and adding login to the server role being system administrator please read the below link describing step by step the procedure.&lt;br /&gt;&lt;br /&gt;http://blogs.msdn.com/raulga/archive/2007/07/12/disaster-recovery-what-to-do-when-the-sa-account-password-is-lost-in-sql-server-2005.aspx&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-5267615768046250754?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/5267615768046250754/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=5267615768046250754' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/5267615768046250754'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/5267615768046250754'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2010/01/think-before-unchecking-sysadmin-rights.html' title='Think before unchecking sysadmin rights of BUILTIN\Administrators.'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-8484055391364441656</id><published>2009-12-20T07:21:00.000-08:00</published><updated>2009-12-20T07:38:00.493-08:00</updated><title type='text'>SQL_VARIANT_PROPERTY() to find the info about data type?</title><content type='html'>BOL says that this function returns the base data type and other information about a sql_variant value. Have you ever looked what is datatype of GETDATE() or perhaps DB_ID() function  or what is the data type of SYSTEM_USER??&lt;br /&gt;&lt;br /&gt;DECLARE @var sql_variant &lt;br /&gt; SET @var =GETDATE() --SYSTEM_USER&lt;br /&gt;SELECT SQL_VARIANT_PROPERTY(@var , 'BaseType') as BaseType&lt;br /&gt;     , SQL_VARIANT_PROPERTY(@var , 'Precision') as Precision&lt;br /&gt;     , SQL_VARIANT_PROPERTY(@var , 'Scale') as Scale&lt;br /&gt;     , SQL_VARIANT_PROPERTY(@var , 'TotalBytes') as TotalBytes&lt;br /&gt;     , SQL_VARIANT_PROPERTY(@var , 'MaxLength') as MaxLength&lt;br /&gt;     , SQL_VARIANT_PROPERTY(@var , 'Collation') as Collation;&lt;br /&gt;&lt;br /&gt;I found that very usefull as you can easily retun the basic info about even system objects and how SQL Server interpetes it.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-8484055391364441656?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/8484055391364441656/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=8484055391364441656' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/8484055391364441656'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/8484055391364441656'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2009/12/have-you-used-sqlvariantproperty-to.html' title='SQL_VARIANT_PROPERTY() to find the info about data type?'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-6081778728402219588</id><published>2009-12-09T01:14:00.000-08:00</published><updated>2009-12-09T01:29:32.574-08:00</updated><title type='text'>Think before you use ALTER TABLE ....</title><content type='html'>Let me say  you have been told to add a computed column to the very huge table in SQL Server. Well,if you have been working with SQL Server for a while  you are probably aware that  you will be better of using ALTER TABLE.. command instead of using SSMS because under some circumstances SQL Server will copy the entire table into a temporary one and then rename it , but if the users work on the table they will be locked... So far so good. Now , what if you have been told to add a computed column with PERSISTED clause which means SQL Server will store physically the values of the column..Here stop and think a little bit especialy you deal with huge tables. SQL Server is going to update every row in the table with computed value based on the formula you provide with. How long will it take? I have seen that adding a PERSISTED column by using ALTER TABLE command on the table with 230 million rows took 19 hours on very powerful server, can you afford it?. &lt;br /&gt;So what is the solution? The solution is to add permanent column and then running  an UPDATE command , however do not forget to divide the UPDATE into small batches (each one of 10000 rows for example) such as you will control the rows has been updated/affected by the UPDATE and your LOG file will not be blowing up.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-6081778728402219588?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/6081778728402219588/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=6081778728402219588' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/6081778728402219588'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/6081778728402219588'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2009/12/think-before-you-use-alter-table.html' title='Think before you use ALTER 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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-5914336038577470884</id><published>2009-11-08T06:11:00.000-08:00</published><updated>2009-11-09T03:52:50.619-08:00</updated><title type='text'>Consistency checking for a 1.2 TB database</title><content type='html'>I would like to share with you my recent experience on how to build consistency checking for VLDB by using DBCC CHECKDB/CHECKTABLE and etc. I was following Paul's great article&lt;br /&gt;http://www.sqlskills.com/BLOGS/PAUL/post/CHECKDB-From-Every-Angle-Consistency-Checking-Options-for-a-VLDB.aspx&lt;br /&gt;to build the same or almost the same strategy on the client's site. They have 8 huge tables arround 658,245,225 pages each table and I tested the bigest one to run DBCC CHECKTABLE command. It took 17 hours to complete the task but unfortunately it was not acceptable for the client. So my final solution is (thanks to Paul) to restore the database on the dev.machine and perform the checking on.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-5914336038577470884?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/5914336038577470884/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=5914336038577470884' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/5914336038577470884'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/5914336038577470884'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2009/11/consistency-checking-fo-12-tb-database.html' title='Consistency checking for a 1.2 TB database'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>6</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-2009544861275535814</id><published>2009-10-18T05:37:00.000-07:00</published><updated>2009-10-18T06:05:25.515-07:00</updated><title type='text'>Import data from AS400 by using SSIS</title><content type='html'>That was deviation towards the world of BI. I was helping my co-worker to build a SSIS Package to import the data from AS400 into SQL Server.&lt;br /&gt;At first I selected OLE DB Source component for getting the data but started  getting odd errors of driver compatibilty. Then I read that there is a newish driver called DB2OLEDB which unfortunately  works only on Enterprise/Developer Editions ,so I had only Standtard...&lt;br /&gt;&lt;br /&gt;After some investigations I solved the problem by choosing DataReader Source control and selected ODBC driver for Net.Providers. It looks obvious for experienced BI consultants but for me, who rarely use SSIS, I felt great when co-worker was so happy to get all the data:-))&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-2009544861275535814?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/2009544861275535814/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=2009544861275535814' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/2009544861275535814'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/2009544861275535814'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2009/10/import-data-from-as400-by-using-ssis.html' title='Import data from AS400 by using SSIS'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-5757340382756494511</id><published>2009-10-05T03:32:00.000-07:00</published><updated>2009-10-07T02:34:43.007-07:00</updated><title type='text'>Does the fragmentation have impact on performance on Heaps?</title><content type='html'>I have been working on some project that involves using very huge tables. Actually most of tables are heaps and to reduce IO we decided to compress those tables on PAGE level because the database is READ-ONLY. Fortunately in SQL Server 2008 we can issue ALTER TABLE... REBUILD.. to rebild the heaps to remove forwarding pointers ,please read Tibor's article (http://sqlblog.com/blogs/tibor_karaszi/archive/2009/08/28/heaps-forwarding-pointers-alter-table-rebuild-and-non-clustered-indexes.aspx)&lt;br /&gt;&lt;br /&gt;Well ,after running the scripts we ended up with almost 500GB empty space. Usually it is not good to shrink the database as  you gain nothing (it will grow again) and that caused fragmentation which does not help in terms of performance. Shrinking VLDB is another topic but what I did NOT know if your queries are doing INDEX SEEK SQL Server can get to any row easily no matter how fragmented the table is.&lt;br /&gt;In that case fragmenation has no impact on performance.&lt;br /&gt;&lt;br /&gt;If you think about it does perfect sense. Fragmentation is a measure of how closely the logical order matches the physical order. So if you do a scan (following the logical order) on a fragmented table, you can be jumping all over the to get the rows you need,instead of accessing them nice and neat and contiguously.&lt;br /&gt;&lt;br /&gt;Many thanks to Kalen Delaney.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-5757340382756494511?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/5757340382756494511/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=5757340382756494511' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/5757340382756494511'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/5757340382756494511'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2009/10/has-fragmentation-impact-on-performance.html' title='Does the fragmentation have impact on performance 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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-8529237133301566495</id><published>2009-09-02T00:44:00.000-07:00</published><updated>2009-09-02T02:05:21.256-07:00</updated><title type='text'>UNION ALL within a view</title><content type='html'>I have a client who has viewes that contain UNION ALL command for number of very large tables. The logic is to apply a WHERE condition to that view to get the data. Even having indexes on those tables SQL Server applies a WHERE condition to each SELECT statement  within a view that may lead to performance proble. To demonstate it please considetr AdwentureWork data and two tables Sales.SalerDetails and SalesOrderHeader &lt;br /&gt;&lt;br /&gt;CREATE VIEW v1&lt;br /&gt;AS&lt;br /&gt;SELECT S.SalesOrderID&lt;br /&gt;FROM&lt;br /&gt;Sales.SalesOrderDetail S&lt;br /&gt;UNION ALL &lt;br /&gt;SELECT S.SalesOrderID&lt;br /&gt;,S.CreditCardID  FROM &lt;br /&gt;Sales.SalesOrderHeader S &lt;br /&gt;&lt;br /&gt;SET STATISTICS IO ON&lt;br /&gt;SELECT TOP 100 * FROM v1 WHERE SalesOrderDetailID &gt;40000 AND SalesOrderDetailID&lt;45000&lt;br /&gt;&lt;br /&gt;Table 'SalesOrderDetail'. Scan count 3, logical reads 1359, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.&lt;br /&gt;Table 'SalesOrderHeader'. Scan count 1, logical reads 34, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.&lt;br /&gt;&lt;br /&gt;You can see that SQL Server 'touched' those tables by using Clustered Index Scan on &lt;br /&gt;Sales.SalerDetails  and Clustered Index Seek on Sales.SalesOrderHeader. Indeed I realy simplified the logic as in reality it has horrible performance. How to improve?&lt;br /&gt;&lt;br /&gt;I create a Multi-Statement Table-Valued UDF that accepts a parameter &lt;br /&gt;CTREATE FUNCTION dbo.udf1&lt;br /&gt;(&lt;br /&gt; @SalesOrderID INT&lt;br /&gt;)&lt;br /&gt;RETURNS @t TABLE  (c1 INT,c2 INT)&lt;br /&gt;AS&lt;br /&gt;BEGIN&lt;br /&gt;INSERT INTO @t&lt;br /&gt;SELECT S.SalesOrderID&lt;br /&gt;FROM&lt;br /&gt;Sales.SalesOrderDetail S &lt;br /&gt;WHERE SalesOrderID &gt; @SalesOrderID AND SalesOrderID&lt;@SalesOrderID+5000&lt;br /&gt;UNION ALL &lt;br /&gt;SELECT S.SalesOrderID&lt;br /&gt;FROM &lt;br /&gt;Sales.SalesOrderHeader S&lt;br /&gt;WHERE SalesOrderID &gt; @SalesOrderID AND SalesOrderID&lt;@SalesOrderID+5000 &lt;br /&gt;RETURN&lt;br /&gt;END&lt;br /&gt;Table '#74AE54BC'. Scan count 1, logical reads 12, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.&lt;br /&gt;&lt;br /&gt;See , I applied a WHERE condition within an UDF for every statement as UDF must get parameters.This is another argument to use  Table-Valued UDF ...&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-8529237133301566495?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/8529237133301566495/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=8529237133301566495' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/8529237133301566495'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/8529237133301566495'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2009/09/union-all-within-view.html' title='UNION ALL within a view'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-8556589575827581890</id><published>2009-08-09T23:32:00.000-07:00</published><updated>2009-08-09T23:43:48.734-07:00</updated><title type='text'>SQL Server 2005/2008 on Windows 7</title><content type='html'>SQL Server MVP John Paul Cook has already mentioned on www.sqlblog.com that SQL Server 2008 will install on Windows 7 and it works just fine. I would like to add that SQL Server 2005 is also working just fine along with SQL Server 2008.&lt;br /&gt;Having said that I'm told by many people that Reporting Services does not work on W7.Well the phrase 'does not work' does not say too much without additional info however I decided to do some testing and found out that SSRS works just fine as well, so the problem could be because the SSRS/SQL Server account does not have enough credentials to run SSRS. More over I took old reports which were deployed to our dedicated SSRS server (Windows 2003) and redeployed to Windows 7 and that also worked just fine. I'm really happy with Windows 7 which is faster than Vista and more comfortable than XP.....&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-8556589575827581890?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/8556589575827581890/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=8556589575827581890' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/8556589575827581890'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/8556589575827581890'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2009/08/sql-server-20052008-on-windows-7.html' title='SQL Server 2005/2008 on Windows 7'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-8498853640272936749</id><published>2009-07-08T22:56:00.000-07:00</published><updated>2009-07-08T23:31:21.830-07:00</updated><title type='text'>Report on Indexes on FKs</title><content type='html'>This question had been asked in NG yesterday.I think it is a good exercise so I take a script too get the column usage from both sides (parent/child) written by Aaron Bertrand and modified a little bit to return the table/column that does not have an index on.&lt;br /&gt;&lt;br /&gt;WITH fk_no_indexes&lt;br /&gt;AS&lt;br /&gt;(&lt;br /&gt;SELECT&lt;br /&gt;[constraint_name] = f.[name],&lt;br /&gt;[child_table] = OBJECT_NAME(f.parent_object_id),&lt;br /&gt;[child_table_id]=f.parent_object_id,&lt;br /&gt;[child_column] = cc.name,&lt;br /&gt;[child_column_id]=cc.[column_id],&lt;br /&gt;[parent_table] = OBJECT_NAME(f.referenced_object_id),&lt;br /&gt;[parent_column] = pc.name&lt;br /&gt;FROM&lt;br /&gt;sys.foreign_keys f&lt;br /&gt;INNER JOIN&lt;br /&gt;(&lt;br /&gt;SELECTc.[object_id],c.name,c.column_id,ic.index_id&lt;br /&gt;FROM sys.columns c INNER JOIN sys.index_columns ic&lt;br /&gt;ON c.[object_id] = ic.[object_id] AND c.column_id = ic.column_id&lt;br /&gt;) AS pc&lt;br /&gt;ON&lt;br /&gt;f.key_index_id = pc.index_id&lt;br /&gt;INNER JOIN sys.foreign_key_columns fkc&lt;br /&gt;ON f.[object_id] = fkc.constraint_object_id&lt;br /&gt;AND pc.[object_id] = fkc.referenced_object_id&lt;br /&gt;AND fkc.referenced_column_id = pc.column_id&lt;br /&gt;INNER JOIN sys.columns cc&lt;br /&gt;ON fkc.parent_object_id = cc.[object_id]&lt;br /&gt;AND fkc.parent_column_id = cc.column_id&lt;br /&gt;)&lt;br /&gt;SELECT [constraint_name],[child_table],[child_column],[parent_table] FROM &lt;br /&gt;fk_no_indexes WHERE NOT EXISTS&lt;br /&gt;(&lt;br /&gt;SELECT * FROM sys.index_columns i&lt;br /&gt;WHERE i.[object_id]=[child_table_id]&lt;br /&gt;AND [child_column_id]=column_id&lt;br /&gt;)&lt;br /&gt;ORDER BY&lt;br /&gt;constraint_name,&lt;br /&gt;child_table;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-8498853640272936749?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/8498853640272936749/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=8498853640272936749' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/8498853640272936749'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/8498853640272936749'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2009/07/report-on-indexes-on-fks.html' title='Report on Indexes on FKs'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-2573902765789813870</id><published>2009-06-15T05:02:00.000-07:00</published><updated>2009-06-15T05:47:40.039-07:00</updated><title type='text'>Have you applied latest sevice pack to the client tools?</title><content type='html'>Well, I have seen a huge number of developers who have not heard aboit it at all. Yes,I'm serious.Recently, I had helped out tuning SQL Server performance of the database in pretty big and to be held in respect company with more than 10 developers. One day one of them asked me about the query ,which restuns the data without the error on his machine ,however , throws the error on the server.If I remember well it was conversion error.I checked both queries and indeed on the dev.machine it runs without the error. I know there is no magic here..and as you can imagine the dev.machine has installed NO service pack at all,but Production SQL Server 2000 has latest service pack (SP4).That's all story. The 'little' difference could have made a big error witout testing as each developer has installed server and client tools on their machines.The SQL Server query optimizer is free to move/change expressions with new release of service packs or hotfix thus please make sure that you have latest service pack on  your client tools and test the query as well.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-2573902765789813870?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/2573902765789813870/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=2573902765789813870' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/2573902765789813870'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/2573902765789813870'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2009/06/have-you-applied-latest-sevice-pack-to.html' title='Have you applied latest sevice pack to the client tools?'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-6403372532286403890</id><published>2009-05-14T01:24:00.000-07:00</published><updated>2009-05-14T01:42:17.778-07:00</updated><title type='text'>How to upload/modify more than one BLOB column in single statement</title><content type='html'>Recently, I have been visited our client who has one table with two columns defined as VARBINARY(MAX)to store the images. How can we insert/upade these columns with single statements? I have not used blobs too much ,so I  ended up with the following&lt;br /&gt;&lt;br /&gt;CREATE TABLE Blobs (id INT NOT NULL IDENTITY(1,1) PRIMARY KEY ,b1 VARBINARY(MAX),b2 VARBINARY(MAX))&lt;br /&gt;&lt;br /&gt;---Single INSERT statement&lt;br /&gt;&lt;br /&gt;INSERT INTO Blobs (b1,b2)&lt;br /&gt;(SELECT *,(SELECT * FROM &lt;br /&gt; Openrowset(Bulk 'N:\OnePicture.JPG', SINGLE_BLOB) AS d1)&lt;br /&gt; FROM&lt;br /&gt; Openrowset(Bulk 'N:\AnotherPicture.JPG', SINGLE_BLOB) AS d2)&lt;br /&gt;&lt;br /&gt;--Update&lt;br /&gt;&lt;br /&gt;UPDATE Blobs   SET b1=(SELECT * FROM &lt;br /&gt; Openrowset(Bulk 'N:\AnotherPicture.JPG', SINGLE_BLOB) AS d2),&lt;br /&gt;b2=(SELECT * FROM &lt;br /&gt; Openrowset(Bulk 'N:\OnePicture.JPG', SINGLE_BLOB) AS d2)&lt;br /&gt;---WHERE condition put here&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-6403372532286403890?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/6403372532286403890/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=6403372532286403890' title='18 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/6403372532286403890'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/6403372532286403890'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2009/05/how-to-uploadmodify-more-than-one-blob.html' title='How to upload/modify more than one BLOB column in single statement'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>18</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-6077930439337147557</id><published>2009-05-06T00:23:00.000-07:00</published><updated>2009-05-08T06:40:35.057-07:00</updated><title type='text'>My wife gave birth to our third child on 4 May!</title><content type='html'>A son, 3 kg 370 grams! &lt;br /&gt;&lt;br /&gt;We have replaced ourselves and are now done.&lt;blockquote&gt;&lt;/blockquote&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-6077930439337147557?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/6077930439337147557/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=6077930439337147557' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/6077930439337147557'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/6077930439337147557'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2009/05/my-wife-gave-birth-to-our-third-child.html' title='My wife gave birth to our third child on 4 May!'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>6</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-8919608397430246243</id><published>2009-04-19T00:54:00.000-07:00</published><updated>2009-04-19T07:58:24.525-07:00</updated><title type='text'>How to eat  ASYNC_NETWORK_IO ?</title><content type='html'>Last time I was visiting our client the person showed me one process which was in suspended status for a long time. Using Adam's great script(http://sqlblog.com/blogs/adam_machanic/archive/2009/03/30/who-is-active-v8-40-now-with-delta-power.aspx) I identified the SELECT statement which had ASYNC_NETWORK_IO wait type. MS said that this type occurs on network writes when the task is blocked behind the network and we need to verify that the client is processing data from SQL Server. The problem was that SELECT statement retrievs more than 50.000 rows , then the application manipulates (FOR ..NEXT loop) with that data and finally only 200 rows that a client had seen at the end. Well,thanks to SQL Server 2005 we changed the statement to accept a parameter for TOP clause and this wait type has gone. We have not seen a great improvment of the application as we did not expect it but it is another way to identify poor written queries.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-8919608397430246243?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/8919608397430246243/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=8919608397430246243' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/8919608397430246243'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/8919608397430246243'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2009/04/how-to-eat-asyncnetworkio.html' title='How to eat  ASYNC_NETWORK_IO ?'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-2529071843137395365</id><published>2009-04-01T21:07:00.000-07:00</published><updated>2009-04-01T23:52:46.317-07:00</updated><title type='text'>MVP Award for 2009 year.</title><content type='html'>I've been given the MVP award for another year. I hope you have found this blog and my posts in public forums useful and that helped you out to resolve some problems.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-2529071843137395365?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/2529071843137395365/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=2529071843137395365' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/2529071843137395365'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/2529071843137395365'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2009/04/mvp-award-for-2009-year.html' title='MVP Award for 2009 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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-2543819525849833636</id><published>2009-03-05T05:08:00.000-08:00</published><updated>2009-03-05T05:16:48.039-08:00</updated><title type='text'>What is a shared drive/s used by SQL Server instance?</title><content type='html'>SQL Server 2005 has a great dynamic management view named sys.dm_io_cluster_shared_drives . If you have clustering and want to know what &lt;br /&gt;shared disk/s which is/are used by clustered SQL Server instance run the following query.&lt;br /&gt;&lt;br /&gt;SELECT DriveName&lt;br /&gt;FROM sys.dm_io_cluster_shared_drives  &lt;br /&gt;ORDER BY DriveName&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-2543819525849833636?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/2543819525849833636/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=2543819525849833636' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/2543819525849833636'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/2543819525849833636'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2009/03/what-is-sshared-drive-is-used-by-sql.html' title='What is a shared drive/s used by SQL Server instance?'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-3157024302063606980</id><published>2009-03-03T01:44:00.000-08:00</published><updated>2009-03-03T02:43:20.522-08:00</updated><title type='text'>Running DTS without SQL Server?</title><content type='html'>Seems like unreasonable request.Well, we had a client who wanted by click on icon on his Desktop to launch a DTS Package which runs on another computer.I'd like to share with you the script that friend of mine created on a workstation that did not have SQL Server installed.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;MD "\Program Files\Microsoft SQL Server\80\Tools\Binn\Resources\1033"&lt;br /&gt;&lt;br /&gt;COPY sqlresld.dll "\Program Files\Microsoft SQL Server\80\Tools\Binn"&lt;br /&gt;&lt;br /&gt;COPY dtsffile.dll "\Program Files\Microsoft SQL Server\80\Tools\Binn"&lt;br /&gt;COPY dtsffile.rll "\Program Files\Microsoft SQL Server\80\Tools\Binn\Resources\1033"&lt;br /&gt;regsvr32.exe "\Program Files\Microsoft SQL Server\80\Tools\Binn\dtsffile.dll"&lt;br /&gt;&lt;br /&gt;COPY dtspkg.dll "\Program Files\Microsoft SQL Server\80\Tools\Binn"&lt;br /&gt;COPY dtspkg.rll "\Program Files\Microsoft SQL Server\80\Tools\Binn\Resources\1033"&lt;br /&gt;regsvr32.exe "\Program Files\Microsoft SQL Server\80\Tools\Binn\dtspkg.dll"&lt;br /&gt;&lt;br /&gt;COPY dtspump.dll "\Program Files\Microsoft SQL Server\80\Tools\Binn"&lt;br /&gt;COPY dtspump.rll "\Program Files\Microsoft SQL Server\80\Tools\Binn\Resources\1033"&lt;br /&gt;regsvr32.exe "\Program Files\Microsoft SQL Server\80\Tools\Binn\dtspump.dll"&lt;br /&gt;&lt;br /&gt;COPY axscphst.dll "\Program Files\Microsoft SQL Server\80\Tools\Binn"&lt;br /&gt;COPY axscphst.rll "\Program Files\Microsoft SQL Server\80\Tools\Binn\Resources\1033"&lt;br /&gt;regsvr32.exe "\Program Files\Microsoft SQL Server\80\Tools\Binn\axscphst.dll"&lt;br /&gt;&lt;br /&gt;COPY dtsrun.exe "\Program Files\Microsoft SQL Server\80\Tools\Binn"&lt;br /&gt;&lt;br /&gt;COPY dtsrun.rll "\Program Files\Microsoft SQL Server\80\Tools\Binn\Resources\1033"&lt;br /&gt;&lt;br /&gt;COPY custtask.dll "\Program Files\Microsoft SQL Server\80\Tools\Binn"&lt;br /&gt;COPY custtask.rll "\Program Files\Microsoft SQL Server\80\Tools\Binn\Resources\1033"&lt;br /&gt;regsvr32.exe "\Program Files\Microsoft SQL Server\80\Tools\Binn\custtask.dll"&lt;br /&gt;&lt;br /&gt;COPY sqlunirl.dll %SYSTEMROOT%\system32&lt;br /&gt;&lt;br /&gt;PAUSE&lt;br /&gt;--------------&lt;br /&gt;"\Program Files\Microsoft SQL Server\80\Tools\Binn\dtsrun.exe" /FProcessAppSalesCube.dts&lt;br /&gt;&lt;br /&gt;PAUSE&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-3157024302063606980?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/3157024302063606980/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=3157024302063606980' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/3157024302063606980'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/3157024302063606980'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2009/03/running-dts-without-sql-server.html' title='Running DTS without SQL Server?'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-3054987088935707667</id><published>2009-02-04T06:44:00.000-08:00</published><updated>2009-02-04T07:06:26.104-08:00</updated><title type='text'>How to render the Report to PDF/EXCEL format</title><content type='html'>I had a client who wanted to get his report directly to PDF file. Previously, he used to use a Report Viewer component in application and then exporting it to the appropriate format.&lt;br /&gt;In this example I render my report to the PDF file that will be located on disk C:\.&lt;br /&gt;This report also accepts one parameter.The key of the module is ReportExecutionService class which contains some methods that we need to use. &lt;br /&gt;&lt;br /&gt;Please refer to the BOL&lt;br /&gt;ms-help://MS.SQLCC.v9/MS.SQLSVR.v9.en/rswsref9/html/d6ce295b-25f4-4ce3-8d1a-765d7e7d9815.htm&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;--VB.NET&lt;br /&gt;&lt;br /&gt;Imports Microsoft.SqlServer.ReportingServices2005&lt;br /&gt; &lt;br /&gt;Private Sub CreatePdfFile()&lt;br /&gt;        Dim Res As New Execution.ReportExecutionService&lt;br /&gt;        Dim params(0) As Execution.ParameterValue&lt;br /&gt;        Res.Credentials = System.Net.CredentialCache.DefaultCredentials&lt;br /&gt;        Res.Url = "http://servername/ReportServer/ReportExecution2005.asmx?wsdl"&lt;br /&gt;        params(0) = New Execution.ParameterValue&lt;br /&gt;        params(0).Name = "ParamName"&lt;br /&gt;        params(0).Value = Value&lt;br /&gt;&lt;br /&gt;        Dim ReportPath As String = "/Reports/ReportName"&lt;br /&gt;&lt;br /&gt;        Res.LoadReport(ReportPath, Nothing)&lt;br /&gt;        Dim Format As String = "PDF"&lt;br /&gt;        Dim devInfo As String = Nothing&lt;br /&gt;        Dim extension As String = Nothing&lt;br /&gt;        Dim mimeType As String = Nothing&lt;br /&gt;        Dim encoding As String = Nothing&lt;br /&gt;        Dim warnings() As Execution.Warning = Nothing&lt;br /&gt;        Dim stremIds() As String = Nothing&lt;br /&gt;        Dim result() As Byte = Nothing&lt;br /&gt;        Res.SetExecutionParameters(params, "en-us") --ParameterLanguage&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;        result = Res.Render(Format, devInfo, extension, mimeType, encoding, warnings, stremIds)&lt;br /&gt;        IO.File.WriteAllBytes("C:\Report.pdf", result)&lt;br /&gt;    End Sub&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-3054987088935707667?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/3054987088935707667/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=3054987088935707667' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/3054987088935707667'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/3054987088935707667'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2009/02/how-to-render-report-to-pdfexcel-format.html' title='How to render the Report to PDF/EXCEL format'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-7560750080271366320</id><published>2009-01-08T00:35:00.000-08:00</published><updated>2011-12-14T06:53:42.039-08:00</updated><title type='text'>Executing multiple script files via SQLCMD</title><content type='html'>I'm sure most people either DBA or developers faced at least once a requirement to execute bunch of script files. As you know we can do it by using Script Task in SSIS, however I would like to share with you a infrequent used :r parameter in SQLCMD utilty. Let say you have three files (.sql) that do an UPDATE,INSERT and finally SELECT statements. All you need is to have a single batch file which will execute those .sql files.&lt;br /&gt;Create a batch file named DML.sql and add the below code&lt;br /&gt;:r "c:\myUpdate.sql"&lt;br /&gt;&lt;br /&gt;:r "c:\myInsert.sql"&lt;br /&gt;&lt;br /&gt;:r "c:\MySelect.sql"&lt;br /&gt;&lt;br /&gt;Make sure that you leave free space between commands.&lt;br /&gt;--Usage&lt;br /&gt;EXEC  master..xp_cmdshell 'SQLCMD -S Server\SQLSERVERDEV2005 -i"c:\DML.sql"'&lt;br /&gt;&lt;br /&gt;I recommend you to read this article with more examples about how using :r parameter.&lt;br /&gt;http://blogs.msdn.com/patrickgallucci/archive/2007/09/03/sqlcmd-and-the-power-of-the-little-r.aspx&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-7560750080271366320?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/7560750080271366320/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=7560750080271366320' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/7560750080271366320'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/7560750080271366320'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2009/01/executing-multiple-script-file-via.html' title='Executing multiple script files via SQLCMD'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-8159824779603917326</id><published>2008-12-26T07:08:00.000-08:00</published><updated>2008-12-26T07:24:07.528-08:00</updated><title type='text'>Happy New Year</title><content type='html'>I think that is my first blog which does not relate to SQL Server,though why not?&lt;br /&gt;I wish to SQL Server Consultants lots of contracts to help out businesses in 2009, I wish to SQL Server DBAs  lots of interesting work but that their databases will run smoothly,I wish to SQL Server Developers to write efficient queries and resolve challenges and to every body and their families in 2009  I wish you health ,happiness,wealth and if you look forward to a good year ahead, spread happiness with these wonderful New Year wishes.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-8159824779603917326?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/8159824779603917326/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=8159824779603917326' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/8159824779603917326'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/8159824779603917326'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2008/12/happy-new-year.html' title='Happy New 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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-9050504289110700168</id><published>2008-12-18T01:34:00.000-08:00</published><updated>2008-12-18T02:05:03.393-08:00</updated><title type='text'>Do you always write parameterized query?</title><content type='html'>As you know SQL Server creates an execution plan for the query/stored procedure and stores it in procedure cache in order to reuse it when  you run the query next time.&lt;br /&gt;But more and more execution plans in memory will also hurt the performance as we do not have a control the procedure cache's size so what would you do? Lets see the below scenario I've ran on the DEV(DO NOT run on Production) machine. First of all I used DBCC FREEPROCCACHE which clears the procedure cache and causes ad hoc queries to&lt;br /&gt;be recompiled. Next I ran actually the same query with different parameters.&lt;br /&gt;&lt;br /&gt;DBCC FREEPROCCACHE&lt;br /&gt;&lt;br /&gt;SELECT * FROM Production.WorkOrder &lt;br /&gt;WHERE ProductID=522&lt;br /&gt;&lt;br /&gt;SELECT * FROM Production.WorkOrder &lt;br /&gt;WHERE ProductID=737&lt;br /&gt;&lt;br /&gt;DECLARE @i INT&lt;br /&gt;set @i = 518&lt;br /&gt;SELECT *&lt;br /&gt;FROM Production.WorkOrder &lt;br /&gt;WHERE ProductID = @i&lt;br /&gt;GO&lt;br /&gt;--See how many plans SQL Server created&lt;br /&gt;SELECT stats.execution_count AS exec_count, &lt;br /&gt; p.size_in_bytes as [size], &lt;br /&gt; [sql].[text] as [plan_text]&lt;br /&gt;FROM sys.dm_exec_cached_plans p&lt;br /&gt;OUTER APPLY sys.dm_exec_sql_text (p.plan_handle) sql&lt;br /&gt;JOIN sys.dm_exec_query_stats stats ON stats.plan_handle = p.plan_handle&lt;br /&gt;&lt;br /&gt;/*&lt;br /&gt;1 49152 DECLARE @i INT  set @i = 518  SELECT *  FROM Production.WorkOrder   WHERE ProductID = @i&lt;br /&gt;1 24576 SELECT * FROM Production.WorkOrder   WHERE ProductID=737&lt;br /&gt;1 24576 SELECT * FROM Production.WorkOrder   WHERE ProductID=522&lt;br /&gt;*/&lt;br /&gt;As you see SQL Server created THREE execution plans.&lt;br /&gt;Now let's wrap the query within a stored procedure and see what will be happened.&lt;br /&gt;&lt;br /&gt;CREATE PROCEDURE spTest&lt;br /&gt;@i INT&lt;br /&gt;AS&lt;br /&gt;SELECT *&lt;br /&gt;FROM Production.WorkOrder &lt;br /&gt;WHERE ProductID = @i&lt;br /&gt;GO&lt;br /&gt;&lt;br /&gt;EXEC spTest 522&lt;br /&gt;EXEC spTest 737&lt;br /&gt;EXEC spTest 518&lt;br /&gt;&lt;br /&gt;--See how many plans SQL Server created&lt;br /&gt;SELECT stats.execution_count AS exec_count, &lt;br /&gt; p.size_in_bytes as [size], &lt;br /&gt; [sql].[text] as [plan_text]&lt;br /&gt;FROM sys.dm_exec_cached_plans p&lt;br /&gt;OUTER APPLY sys.dm_exec_sql_text (p.plan_handle) sql&lt;br /&gt;JOIN sys.dm_exec_query_stats stats ON stats.plan_handle = p.plan_handle&lt;br /&gt;&lt;br /&gt;/*&lt;br /&gt;3 40960 CREATE PROCEDURE spTest  @i INT  AS  SELECT *  FROM Production.WorkOrder   WHERE ProductID = @i  &lt;br /&gt;*/ &lt;br /&gt;Wow, you we get single execution plan that SQL Server used three times.&lt;br /&gt;I also recommend you to read Tony's blog about the subject&lt;br /&gt;http://sqlblogcasts.com/blogs/tonyrogerson/archive/2007/07/07/procedure-cache-tuning-sizing-from-1gbyte-to-768kbytes-increase-the-size-of-usable-data-cache.aspx&lt;br /&gt;&lt;br /&gt;Tony writes in his blog and I'm completely agree with him.&lt;br /&gt;"Surprised? It's one of the reasons DBA's keep bleating on about using stored procedures, it forces the mindset to use the procedure cache more effectively. By parameterising, but preferably using stored procedures we get plan reuse which means a) less compiles thereby reducing CPU load, b) more available pages for the data cache thereby reducing physical disk IO and c) DBA’s are always right ;)."&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;PS.Besides using stored procedures we can use sp_executesql with parameters as below&lt;br /&gt;&lt;br /&gt;EXEC sp_executesql N'SELECT  SUM(ProductID) AS ProductTotal&lt;br /&gt;FROM Production.WorkOrder &lt;br /&gt;WHERE ProductID= @ProductID', N'@ProductID INT', 722&lt;br /&gt;&lt;br /&gt;EXEC sp_executesql N'SELECT  SUM(ProductID) AS ProductTotal&lt;br /&gt;FROM Production.WorkOrder &lt;br /&gt;WHERE ProductID= @ProductID', N'@ProductID INT', 522&lt;br /&gt;&lt;br /&gt;.....&lt;br /&gt;&lt;br /&gt;GO&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-9050504289110700168?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/9050504289110700168/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=9050504289110700168' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/9050504289110700168'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/9050504289110700168'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2008/12/do-you-always-write-parameterized-query.html' title='Do you always write parameterized query?'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-4322169359691211676</id><published>2008-12-04T05:19:00.000-08:00</published><updated>2008-12-04T05:35:18.798-08:00</updated><title type='text'>When constraint remains in system table even the table is no longer exists</title><content type='html'>Ok, I'm talking about temporary tables and yes,it could happen to you even you think that the temporary table is no longer exists. Consider the below script where stored procedure inserts the data with wrong type which caused the error(245conversion error). As you propably know we would never name the constraints for local temporary tables (as opposite to permanent tables), because if we run the following statement from two connections we get something like that.&lt;br /&gt;/*&lt;br /&gt;Msg 2714, Level 16, State 4, Line 1&lt;br /&gt;There is already an object named 'PK_tmp1' in the database&lt;br /&gt;*/&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;USE master&lt;br /&gt;GO&lt;br /&gt;ALTER PROCEDURE spSample&lt;br /&gt;AS&lt;br /&gt;CREATE TABLE #tmp(&lt;br /&gt; col int --PRIMARY KEY NONCLUSTERED--,&lt;br /&gt; CONSTRAINT PK_tmp1 PRIMARY KEY NONCLUSTERED&lt;br /&gt; (&lt;br /&gt;  col&lt;br /&gt; ) &lt;br /&gt;)&lt;br /&gt;BEGIN TRY&lt;br /&gt;INSERT INTO #tmp SELECT 'A'&lt;br /&gt;END TRY&lt;br /&gt;BEGIN CATCH&lt;br /&gt;SELECT ERROR_NUMBER() &lt;br /&gt;END CATCH&lt;br /&gt;&lt;br /&gt;--Usage&lt;br /&gt;USE master&lt;br /&gt;EXEC spSample&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;USE tempdb&lt;br /&gt;SELECT * FROM  sys.objects&lt;br /&gt; WHERE  OBJECT_NAME(OBJECT_ID)like '%#tmp%'&lt;br /&gt;&lt;br /&gt;Try add DROP TABLE #tmp in BEGIN CATCH ..block and then SQL Server deletes the constraint as well.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-4322169359691211676?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/4322169359691211676/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=4322169359691211676' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/4322169359691211676'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/4322169359691211676'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2008/12/when-constraint-remains-in-system-table.html' title='When constraint remains in system table even the table is no longer exists'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-1283777105654037400</id><published>2008-11-12T06:34:00.000-08:00</published><updated>2008-11-12T06:47:44.037-08:00</updated><title type='text'>Can I hide undesirable database in SSMS?</title><content type='html'>We have discussed the issue many times and there is an opened connection to Microsoft to add this feature in the next release. Recently I was visiting our client and we tried to do something for the subject.&lt;br /&gt;&lt;br /&gt;1) CREATE new SQL login 'John'&lt;br /&gt;2) CREATE a user named 'John in master database&lt;br /&gt;3) GRANT CREATE DATABASE to John&lt;br /&gt;4) While impersonating John, create a database called 'demo'&lt;br /&gt;5) REVOKE CREATE DATABASE permission from John&lt;br /&gt;6) REVOKE VIEW ANY DATABASE permission from PUBLIC&lt;br /&gt;7) Register this server as John&lt;br /&gt;8) From the 'John' session, expand database tree. Now, you should see only master, tempdb, dbtest&lt;br /&gt;9) GRANT VIEW ANY DATABASE to PUBLIC&lt;br /&gt;10) From the 'John' session, you should see all the databases.&lt;br /&gt;&lt;br /&gt;However, this works perfectly if the user is the owner of the database.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-1283777105654037400?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/1283777105654037400/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=1283777105654037400' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/1283777105654037400'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/1283777105654037400'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2008/11/can-i-hide-undesirable-database-in-ssms.html' title='Can I hide undesirable database in SSMS?'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-5735731230434301564</id><published>2008-10-22T06:17:00.000-07:00</published><updated>2008-10-22T06:47:27.876-07:00</updated><title type='text'>Track down DDL Database changes</title><content type='html'>If you want to know who alters/drops/creates tables/views/stored procedures... I would like to share with you the following script. As you know DDL Triggers (introduced in SQL Server 2005) work very like the DML triggers but details of the event that fired the a trigger are available only in XML format.&lt;br /&gt;&lt;br /&gt;Fist of all I create a table that will hold events.&lt;br /&gt;&lt;br /&gt;CREATE TABLE [dbo].[DDL_ChangeEvents](&lt;br /&gt; [ID] [int] IDENTITY(1,1) NOT NULL,&lt;br /&gt; [Session_ID] [int] NOT NULL CONSTRAINT [DF_ddl_change_Session_ID]  DEFAULT (@@spid),&lt;br /&gt; [Session_IPAddress] [nvarchar](50) NULL,&lt;br /&gt; [Insert_Date] [datetime] NOT NULL CONSTRAINT [DF_ddl_change_Insert_Date]  DEFAULT (GETDATE()),&lt;br /&gt; [Username] [nvarchar](100) NOT NULL CONSTRAINT [DF_DDL_change_Username]  DEFAULT (CONVERT([nvarchar](100),ORIGINAL_LOGIN(),(0))),&lt;br /&gt; [EventType] [nvarchar](200) NULL,&lt;br /&gt; [objectName] [nvarchar](200) NULL,&lt;br /&gt; [objectType] [nvarchar](200) NULL,&lt;br /&gt; [sql] [nvarchar](max) NULL&lt;br /&gt;) ON [PRIMARY]&lt;br /&gt;&lt;br /&gt;It wont help if I get only SPID of the session as in many cases users get logged with only one defined login or even with 'sa'.So I need IP address of those workstations thus I added Session_IPAddress column.&lt;br /&gt;Now, let's create a database trigger to capture the info.&lt;br /&gt;&lt;br /&gt;CREATE TRIGGER [trgDataDDLChangeEvent] ON DATABASE &lt;br /&gt;    FOR DDL_DATABASE_LEVEL_EVENTS &lt;br /&gt;AS &lt;br /&gt;    DECLARE @eventdata XML &lt;br /&gt;    SET @eventdata = EVENTDATA() &lt;br /&gt;    IF @eventdata.value('(/EVENT_INSTANCE/EventType)[1]', 'nvarchar(200)') &lt;br /&gt;        &lt;&gt; 'CREATE_STATISTICS'  &lt;br /&gt;        INSERT  INTO DDL_ChangeEvents&lt;br /&gt;                ( &lt;br /&gt;                  EventType, &lt;br /&gt;                  ObjectName, &lt;br /&gt;                  ObjectType, &lt;br /&gt;                  [sql] ,&lt;br /&gt;                  Session_IPAddress&lt;br /&gt;                ) &lt;br /&gt;                SELECT @eventdata.value('(/EVENT_INSTANCE/EventType)[1]', &lt;br /&gt;                              'nvarchar(200)'), &lt;br /&gt;                  @eventdata.value('(/EVENT_INSTANCE/ObjectName)[1]', &lt;br /&gt;                              'nvarchar(200)'), &lt;br /&gt;                  @eventdata.value('(/EVENT_INSTANCE/ObjectType)[1]', &lt;br /&gt;                              'nvarchar(200)'), &lt;br /&gt;                  @eventdata.value('(/EVENT_INSTANCE/TSQLCommand)[1]', &lt;br /&gt;                              'nvarchar(max)'), client_net_address&lt;br /&gt;                 FROM sys.dm_exec_connections WHERE session_id=@@SPID&lt;br /&gt;; &lt;br /&gt;&lt;br /&gt;Well I won't bother to record CREATE STATISTIC events hence there is an IF block to skip this event. I get the IP Address from sys.dm_exec_connections DMV which has client_net_address column.&lt;br /&gt;Now create/drop/alter table (also via SSMS) for example and query the DDL_ChangeEvents table to see what happened.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-5735731230434301564?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/5735731230434301564/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=5735731230434301564' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/5735731230434301564'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/5735731230434301564'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2008/10/track-down-ddl-database-changes.html' title='Track down DDL Database changes'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-5087471497016779733</id><published>2008-10-05T04:44:00.000-07:00</published><updated>2008-10-05T06:00:21.815-07:00</updated><title type='text'>There is a clever way of rebuild indexes</title><content type='html'>Hi everybody. &lt;br /&gt;It seems like I am seeing more and more inquires from our clients asking for help solving performance related issues with rebuilding indexes. All of them (or almost all of them) have been using Maintanace Plan Rebuild/Reorganize Index Task. We have lots of clients who have pretty big databases(&gt;200GB) and have not hired yeat a DBA:-).They used to use this task and specify all tables as well as all databases, moreover, one client used to run such tasks in the middle of work day. It leads to locks on tables and performance decreasing. I would also notice you to not cancelling the task as SQL Server will rolback the whole transactions and you are about to wait a lot of time. Just let the task to complete. I suggested instead of running the task, first, identify fragmented indexes on tables that have more than 1000 pages.&lt;br /&gt;&lt;br /&gt;DECLARE @RebuildStatement nvarchar(4000)&lt;br /&gt;DECLARE RebuildStatements CURSOR LOCAL FAST_FORWARD&lt;br /&gt;FOR&lt;br /&gt;SELECT  'ALTER INDEX  '+i.name+ ' ON '+&lt;br /&gt;OBJECT_NAME(i.object_id)+' REORGANIZE;'&lt;br /&gt;FROM&lt;br /&gt;sys.dm_db_index_physical_stats(db_id(), NULL, NULL, NULL, 'DETAILED') phystat inner JOIN sys.indexes i &lt;br /&gt;ON i.object_id = phystat.object_id &lt;br /&gt;AND i.index_id = phystat.index_id WHERE phystat.avg_fragmentation_in_percent &gt; 40 &lt;br /&gt;and page_count&gt;=1000&lt;br /&gt;&lt;br /&gt;OPEN RebuildStatements&lt;br /&gt;WHILE 1 = 1&lt;br /&gt;BEGIN&lt;br /&gt;    FETCH NEXT FROM RebuildStatements INTO @RebuildStatement&lt;br /&gt;    IF @@FETCH_STATUS &lt;&gt; 0 BREAK&lt;br /&gt;    EXEC(@RebuildStatement)&lt;br /&gt;END&lt;br /&gt;CLOSE RebuildStatements&lt;br /&gt;DEALLOCATE RebuildStatements&lt;br /&gt;&lt;br /&gt;The above SELECT generates a simple script to REORGANIZE (change to REBUILD) indexes and EXECUTES the dynamic sql. As you probaly know this script has to be run on SQL Server 2005/2008 and do not forget about really great feature such rebuilding indexes ONLINE. For more details please see BOL.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-5087471497016779733?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/5087471497016779733/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=5087471497016779733' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/5087471497016779733'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/5087471497016779733'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2008/10/there-is-clever-way-of-rebuild-indexes.html' title='There is a clever way of rebuild 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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-5548263136219568107</id><published>2008-09-15T00:17:00.000-07:00</published><updated>2008-09-15T00:47:41.398-07:00</updated><title type='text'>To speak kindly about RedGate</title><content type='html'>All of us are aware that restore database with different collation may cause a headache. That what happened to one of our databases where one developer created a database without to pay attention about what kind of data he is going to deal with. Ok,we can set COLLATION even per column , but what if you have lots of tables to be altered with new COLLATION. Well, we can use either import/export or SSIS package or perhaps write some T-SQL script to do the job , however I'd like to tell you how easy to get a new database with desired COLLATION by using RedGate tool.&lt;br /&gt;1) CREATE DATABASE dbname COLLATE 'your desired collation'&lt;br /&gt;2) Open RedGate (SQL Compare)tool to move the structure of source db to the destination db. That's all.&lt;br /&gt;&lt;br /&gt;I takes a few minutes even we had 25GB database. I'd strongly recommend to have a look at this great tool.(www.red-gate.com)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-5548263136219568107?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/5548263136219568107/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=5548263136219568107' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/5548263136219568107'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/5548263136219568107'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2008/09/to-speak-kindly-about-redgate.html' title='To speak kindly about RedGate'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-1986741061001886705</id><published>2008-09-01T23:32:00.000-07:00</published><updated>2008-09-02T03:39:29.893-07:00</updated><title type='text'>Computed column is PERSISTED?</title><content type='html'>Hi everybody. &lt;br /&gt;I'd like to share with you how important to define a computed column to be PESRISTED.&lt;br /&gt;As you know from the BOL&lt;br /&gt;/*&lt;br /&gt;For columns specified as PERSISTED, the SQL Server 2005 Database Engine physically stores the computed values in the table and updates the values when any other columns on which the computed column depends are updated. By marking a computed column as PERSISTED, you can create indexes on computed columns defined on expressions that are deterministic, but not precise.&lt;br /&gt;*/&lt;br /&gt;I visited our client two days ago who has been experienced with performance issue for one of their very important query.One big table containes a computed column which SELECT statement is using for to return to the client. We have seen very high number of logical reads and TWO computer scalar iterators. For an obvious reason we  define the computed column as PESRISTED and performance was increased dramatically.Moreover, create an index on computed column and see how perfromance will be increased more..&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;See demo script to see how it is affected.&lt;br /&gt;&lt;br /&gt;CREATE TABLE t(c INT NOT NULL identity(1,1) PRIMARY KEY,&lt;br /&gt;c1  AS '00'+cast(c AS VARCHAR(100)))&lt;br /&gt;&lt;br /&gt;SET NOCOUNT ON &lt;br /&gt;INSERT INTO t  DEFAULT VALUES&lt;br /&gt;GO 100000&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;SET STATISTICS IO ON&lt;br /&gt;SET STATISTICS PROFILE ON&lt;br /&gt;SELECT c1 FROM t&lt;br /&gt;--Table 't'. Scan count 1, logical reads 1250, physical reads 0, read-ahead reads 8, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.&lt;br /&gt;&lt;br /&gt;--select c1 from t&lt;br /&gt;  |--Compute Scalar(DEFINE:([master].[dbo].[t].[c1]=[master].[dbo].[t].[c1]))&lt;br /&gt;       |--Compute Scalar(DEFINE:([master].[dbo].[t].[c1]='00'+CONVERT(varchar(100),[master].[dbo].[t].[c],0)))&lt;br /&gt;            |--Clustered Index Scan(OBJECT:([master].[dbo].[t].[PK__t__1446FBA6]))&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;SET STATISTICS IO ON&lt;br /&gt;SET STATISTICS PROFILE ON&lt;br /&gt;&lt;br /&gt;SELECT c1 FROM t&lt;br /&gt;--Table 't'. Scan count 0, logical reads 50, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.&lt;br /&gt;--select c1 from t&lt;br /&gt;  |--Compute Scalar(DEFINE:([master].[dbo].[t].[c1]=[master].[dbo].[t].[c1]))&lt;br /&gt;       |--Clustered Index Scan(OBJECT:([master].[dbo].[t].[PK__t__1446FBA6]))&lt;br /&gt;&lt;br /&gt;DROP TABLE t&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-1986741061001886705?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/1986741061001886705/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=1986741061001886705' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/1986741061001886705'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/1986741061001886705'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2008/09/computed-colimn-is-persisted.html' title='Computed column is PERSISTED?'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-6044745609351752611</id><published>2008-08-19T04:49:00.000-07:00</published><updated>2008-08-19T21:13:33.734-07:00</updated><title type='text'>String or binary data would be truncated</title><content type='html'>Hi folks.&lt;br /&gt;I named this article with this pretty famous error message. I'm sure that everybody has seen this error at least once.This week I visited our client who asked me very intresting question. They have a table with many columns that most of them defined as VARCHAR(n) datatype. One of the query has been failing with below error.&lt;br /&gt;&lt;br /&gt;"Msg 8152, Level 16, State 14, Line 1&lt;br /&gt;String or binary data would be truncated.&lt;br /&gt;The statement has been terminated."&lt;br /&gt;&lt;br /&gt;Well ,we knew for sure that the user supplied a string which does not match with column datatype  but real question is WHICH of so many columns? As I said, they have more than 50 columns and it was pretty difficult to identify on which column is failed.&lt;br /&gt;My point is that it would be nice to have more information from this error message about which column is failing and I hope that MS will do something for the matter.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-6044745609351752611?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/6044745609351752611/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=6044745609351752611' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/6044745609351752611'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/6044745609351752611'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2008/08/string-or-binary-data-would-be.html' title='String or binary data would be truncated'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>6</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-8285289175263060970</id><published>2008-07-28T00:42:00.000-07:00</published><updated>2008-07-28T01:00:48.598-07:00</updated><title type='text'>Cannot delete a job which is related to MP?</title><content type='html'>Hi folks &lt;br /&gt;If you are using SQL Server 2005 (SP) and used to build Maintanace Plans especially with adding subplans so you are probable seen the folowing error message &lt;br /&gt;/*&lt;br /&gt;Drop failed for Job ‘jobname’. (Microsoft.SqlServer.Smo)&lt;br /&gt;&lt;br /&gt;The DELETE statement conflicted with the REFERENCE constraint “FK_subplan_job_id”. The conflict occurred in database “msdb”, table “dbo.sysmaintplan_subplans”, column ‘job_id’.&lt;br /&gt;The statement has been terminated. (Microsoft SQL Server, Error: 547) &lt;br /&gt;*/&lt;br /&gt;&lt;br /&gt;So if you create a MP,SQL Server will create a job and SSIS which is refernced to the subplan as well as inserts the data into system tables in msdb database. (sysmaintplan_subplans,sysjobs_view,sysjobschedules).&lt;br /&gt;&lt;br /&gt;Intresting is that if you execute a job it makes more insert into log table called sysmaintplan_log. All of these tables are linked through FK and PK relationships. The problem is when you try to delete a job it gives a Foreign Key errors until you manually remove those entries by the SQL tables.&lt;br /&gt;&lt;br /&gt;Please see the link written by Jonas Kempas http://gudenas.com/2007/04/20/sql-server-2005-delete-maintenance-plan-error/  explains step by step how to delete not associated jobs.&lt;br /&gt;&lt;br /&gt;I tried it and it worked just fine.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-8285289175263060970?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/8285289175263060970/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=8285289175263060970' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/8285289175263060970'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/8285289175263060970'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2008/07/cannot-delete-job-which-is-related-to.html' title='Cannot delete a job which is related to MP?'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-8158516086723508075</id><published>2008-07-02T04:35:00.000-07:00</published><updated>2008-07-02T04:47:16.788-07:00</updated><title type='text'>How do we open a large table?</title><content type='html'>This question raised when we worked at the client's side by one of developers. They use SQL Server 2005 (SP2) and remembered that in SQL Server 2000 we can open the table via EM and then specify (All rows , Top rows...)  do you remember?&lt;br /&gt;&lt;br /&gt;I rarely use SSMS  to open/edit tables data, and it seems that MS just removed this option. In SQL Server 2005 we have TABLESAMPLE clause that used to open a table with lots of rows. So we only needed to see what kind of data this psecific table has without open entire table or using TOP clause.&lt;br /&gt;&lt;br /&gt;This example returns an approximate percentage of rows and generates a random value for each physical 8-KB page in the table.&lt;br /&gt;&lt;br /&gt;SELECT * FROM table&lt;br /&gt;TABLESAMPLE system(5 PERCENT)&lt;br /&gt;&lt;br /&gt;I'd really advise you to read BOL about this great feature&lt;br /&gt;ms-help://MS.SQLCC.v9/MS.SQLSVR.v9.en/udb9/html/8868e8fd-6c42-4171-9eab-a0e38cb1bfd3.htm&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-8158516086723508075?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/8158516086723508075/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=8158516086723508075' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/8158516086723508075'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/8158516086723508075'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2008/07/how-do-we-open-large-table.html' title='How do we open a large 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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-3125739867197183467</id><published>2008-06-19T05:42:00.000-07:00</published><updated>2008-06-19T06:00:54.463-07:00</updated><title type='text'>How do you maintain SharePoint databases?</title><content type='html'>My latest post is about how maintaining MSDB database, today I would like to share with you some thoughts about maintaining Sharepoint databases.&lt;br /&gt;&lt;br /&gt;Last week I visited our client who has been working with SharePoint (MOSS7) and started complain about performance of SQL Server. I identified long running queries as well as very good number of deadlocks that happened every hour to databases which belong to SharePoint product. However , when I recommend to add some indexes on the tables people start almost crying not to do that as they were told that SharePoint databases are self managed product and DBA should not be touched it at all. It looks to me very strange , but that was my first experience with MOSS and I decided to do some searching on internet. &lt;br /&gt;&lt;br /&gt;I found a couple of documents (even published by MS) to read them as if Sharepoint SQL Server performance can be managed by Shrinking &amp; Defragging the DB. &lt;br /&gt;I also asked some Sharepoint people and they say that accessing the database directly, changing anything on their databases &lt;br /&gt;aside from what's provided out of the box, etc. is not supported unless you &lt;br /&gt;do it thru the Sharepoint API. Hmm.... looks strange , does not?&lt;br /&gt;&lt;br /&gt;Finally I ended up with sample script to identify very fragmented indexes and running ALTER INDEX  index_name ON tablename REORGANIZE;&lt;br /&gt;&lt;br /&gt;PS.&lt;br /&gt;I could not imagine a customer being unwilling to create whatever indexes are &lt;br /&gt;necessary to ensure reasonable performance of a production Sharepoint &lt;br /&gt;system.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-3125739867197183467?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/3125739867197183467/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=3125739867197183467' title='10 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/3125739867197183467'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/3125739867197183467'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2008/06/how-do-you-maintain-sharepoint.html' title='How do you maintain SharePoint databases?'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>10</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-3344650889123392212</id><published>2008-05-18T04:17:00.000-07:00</published><updated>2008-05-18T04:39:13.742-07:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='p'/><title type='text'>How do you maintain MSDB database?</title><content type='html'>Last week I visited our client who has pretty big databases and performs BACKUP LOG ..operation on almost all user databases. Now, one of the most critical databases got corrupted  and the DBA was pretty confident that he won't loose any data (as he had backup of log file) and brings the database from the backup within 10-12 minutes.&lt;br /&gt;&lt;br /&gt;They also have very well written stored procedure that does RESTORE DATABASE based on name of the database and number of log files to be restored. They run the stored procedure and it has been running for almost 5 hours till DBA canceled the process. What happened? Why it has taken so much time? I thought about it and asked him a question,:-"Have you ever cleared backup history?", he replied  that he hasn't. Then we checked backupset system database that contained more than one million rows!!!!&lt;br /&gt;&lt;br /&gt;I remember SQL Server MVP Geoff N.Hiten wrote the blog about the issue and I even posted a comment on.Please check the following article&lt;br /&gt;http://weblogs.sqlteam.com/geoffh/archive/2008/01/21/MSDB-Performance-Tuning.aspx&lt;br /&gt;&lt;br /&gt;It tooks only 3 minutes to run a script that create indexes and about 15 munutes to run&lt;br /&gt;use msdb&lt;br /&gt;&lt;br /&gt;go &lt;br /&gt;&lt;br /&gt;declare @OldestDate datetime &lt;br /&gt;&lt;br /&gt;set @OldestDate = getdate() -100&lt;br /&gt;&lt;br /&gt; &lt;br /&gt;&lt;br /&gt;exec sp_delete_backuphistory @OldestDate &lt;br /&gt;&lt;br /&gt;Now that it is finished , our RESTORE command took only 12 minutes to complete.&lt;br /&gt;I'd like to point out  how important is to clear backup history (Fortunately, in SQL Server 2005 we have builtin taks to do the job) as on time 'X' you will succefully restore a needed database.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-3344650889123392212?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/3344650889123392212/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=3344650889123392212' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/3344650889123392212'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/3344650889123392212'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2008/05/how-do-you-maintain-msdb-database.html' title='How do you maintain MSDB database?'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-5412049060992972668</id><published>2008-05-05T00:44:00.000-07:00</published><updated>2008-05-05T00:47:09.857-07:00</updated><title type='text'>Getting row count of table in SQL Server 2005</title><content type='html'>It is much easier in SQL Server 2005 to get row count per table.&lt;br /&gt;&lt;br /&gt;SELECT&lt;br /&gt;[TableName]=tbl.name,&lt;br /&gt;[RowCount] = SUM&lt;br /&gt;(&lt;br /&gt;CASE&lt;br /&gt;WHEN (pt.index_id &lt; 2) AND (au.type = 1) THEN pt.rows&lt;br /&gt;ELSE 0&lt;br /&gt;END&lt;br /&gt;)&lt;br /&gt;FROM&lt;br /&gt;sys.tables tbl&lt;br /&gt;INNER JOIN sys.partitions pt&lt;br /&gt;ON tbl.object_id = pt.object_id&lt;br /&gt;INNER JOIN sys.allocation_units au&lt;br /&gt;ON pt.partition_id = au.container_id&lt;br /&gt;GROUP BY&lt;br /&gt;tbl.name ORDER BY [RowCount]DESC;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-5412049060992972668?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/5412049060992972668/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=5412049060992972668' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/5412049060992972668'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/5412049060992972668'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2008/05/getting-row-count-of-table-in-sql.html' title='Getting row count of table in SQL Server 2005'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>6</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-4967226468533710089</id><published>2008-05-04T02:05:00.000-07:00</published><updated>2008-05-04T02:20:17.195-07:00</updated><title type='text'>Getting next value</title><content type='html'>Hi folks. I'd like to share with you some technique to get a next value from the table means  to create  your own sequence mechanism.&lt;br /&gt;As we all know that an Identity property may have gaps, so use the below script to retrieve a next value. We create a table with a one row and a only one column which holding the last used sequence value.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;CREATE TABLE seq(col int not null);&lt;br /&gt;INSERT INTO seq values(0);&lt;br /&gt;go&lt;br /&gt;SELECT * FROM seq&lt;br /&gt;&lt;br /&gt;CREATE PROC spget_nextseq @next_val AS INT OUTPUT&lt;br /&gt;AS&lt;br /&gt;UPDATE seq SET @next_val= col = col + 1;&lt;br /&gt;go&lt;br /&gt;&lt;br /&gt;-- usage&lt;br /&gt;DECLARE @i as int;&lt;br /&gt;EXEC spget_nextseq @i output;&lt;br /&gt;SELECT @i;&lt;br /&gt;&lt;br /&gt;Note, in OLTP application where many connections run this script you may end up with deadlocks. One way to pevent it is using lock hint called TABLOCK &lt;br /&gt;&lt;br /&gt;ALTER PROC spget_nextseq @next_val AS INT OUTPUT&lt;br /&gt;AS&lt;br /&gt;UPDATE seq SET @next_val= col = col + 1 FROM seq WITH (TABLOCK);&lt;br /&gt;go&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-4967226468533710089?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/4967226468533710089/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=4967226468533710089' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/4967226468533710089'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/4967226468533710089'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2008/05/getting-next-value.html' title='Getting next value'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-2253158432290726072</id><published>2008-04-20T22:54:00.000-07:00</published><updated>2008-04-20T23:21:41.360-07:00</updated><title type='text'>Quick look at IN predicate</title><content type='html'>There are lots of articles and techniques onnthe internet about  how to deal with delimited parameters. Last Wed I was visited a client that asked to write a quick query to  return the data based on  delimited values . Here we go&lt;br /&gt;&lt;br /&gt;Use pubs&lt;br /&gt;&lt;br /&gt;DECLARE @t VARCHAR(50)&lt;br /&gt;SET @t = ('Bennet,smith')&lt;br /&gt;&lt;br /&gt;SELECT *&lt;br /&gt;  FROM authors&lt;br /&gt; WHERE ',' + @t + ',' LIKE '%,' + au_lname + ',%'&lt;br /&gt;&lt;br /&gt;SELECT  * FROM&lt;br /&gt; authors WHERE CHARINDEX(',' +  au_lname + ',',','+ @t+',')&gt;0&lt;br /&gt;&lt;br /&gt;As you can imagine, performance will be horrible.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-2253158432290726072?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/2253158432290726072/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=2253158432290726072' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/2253158432290726072'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/2253158432290726072'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2008/04/quick-look-at-in-predicate.html' title='Quick look at IN predicate'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-3115754620094260782</id><published>2008-03-17T03:51:00.001-07:00</published><updated>2008-03-17T04:38:48.630-07:00</updated><title type='text'>SQL Server Agent Jobs duration Report</title><content type='html'>Hi folks. I would like to share with  you the following simple script to show us jobs duration report in SQL Server 2005. I manipulated with INTERGER values stored by SQL Server to convert them into DATETIME/CHAR(8) datatypes to represent the data.&lt;br /&gt;Thanks to SQL Server MVP Peter Ward provided me with StartTime calculation.&lt;br /&gt;&lt;br /&gt;WITH job_duration_view&lt;br /&gt;AS&lt;br /&gt;(&lt;br /&gt;SELECT name,&lt;br /&gt;StartTime =   CONVERT(DATETIME, RTRIM(last_run_date)) +  &lt;br /&gt;    (last_run_time * 9 + last_run_time % 10000 * 6 + last_run_time % 100 * 10 + 25 *   last_run_duration) / 216e4 ,&lt;br /&gt;CONVERT(CHAR(8),DATEADD(ss,last_run_duration,CAST(last_run_date AS CHAR(8))),114)&lt;br /&gt;AS duration &lt;br /&gt; FROM msdb.dbo.sysjobservers js&lt;br /&gt;JOIN msdb.dbo.sysjobs j ON  j.job_id = js.job_id &lt;br /&gt;WHERE last_run_date &gt;0 AND last_run_time &gt;0&lt;br /&gt;)  SELECT name AS job_name,StartTime,&lt;br /&gt;StartTime -'19000101'+Duration AS EndDate ,Duration&lt;br /&gt;FROM job_duration_view&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-3115754620094260782?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/3115754620094260782/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=3115754620094260782' title='7 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/3115754620094260782'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/3115754620094260782'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2008/03/sql-server-agent-jobs-duration-report.html' title='SQL Server Agent Jobs duration Report'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>7</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-2762282094603639673</id><published>2008-03-09T08:10:00.000-07:00</published><updated>2008-03-09T08:37:46.787-07:00</updated><title type='text'>Change collation in tempdb</title><content type='html'>What's happening if you installed SQL Server instance with a collation that is different from a database collation? We just started testing our production application and everything seem to work well, however one of our stored procedure inserts hebrew characters into temporary table and then after some operations the data get insertded into a real table. Guess what wee have seen in the database? Right,we have seen '????' symbols. Sure, if you do not use temporary table and insert the data directly into a permanent table you will see the right characters. Someone said  that we should decline of using temporary table and insert the data into 'temporary' permanent table. Another guy said that we should run ALTER DATABASE tempdb command to change COLLATION, but as we know you cannot run this statement on system databases. &lt;br /&gt;The error  is&lt;br /&gt;Msg 3708, Level 16, State 5, Line 1&lt;br /&gt;Cannot alter the database 'tempdb' because it is a system database.&lt;br /&gt;&lt;br /&gt;So what is the solution? Well , use the REBUILDDATABASE option in Setup.exe or re-install the instance. Fortunately, the whole story happened on developing machine and we did not forget to install PRODUCTION server with right collation:-).&lt;br /&gt;&lt;br /&gt;Just wanted to note you how important is to choose the 'right' collation while installing production server.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-2762282094603639673?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/2762282094603639673/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=2762282094603639673' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/2762282094603639673'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/2762282094603639673'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2008/03/change-collation-in-tempdb.html' title='Change collation in tempdb'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>6</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-2805046876974489152</id><published>2008-03-02T01:51:00.000-08:00</published><updated>2008-03-02T04:33:11.624-08:00</updated><title type='text'>Scheduling database backup in SQL 2005 Express Edition</title><content type='html'>I have been recently visited client who uses SQL Server 2005 Express Edition for his business. As  you know that edition is supplied without SQL Server Agent (which was in MSDE). So,the question is how to schedule backup of the database in that case? I would like to thank  to Jasper Smith, who wrote these greate articles&lt;br /&gt;&lt;br /&gt;Automating Database maintenance in SQL 2005 Express Edition Part I&lt;br /&gt;http://www.sqldbatips.com/showarticle.asp?ID=27&lt;br /&gt;&lt;br /&gt;Automating Database maintenance in SQL 2005 Express Edition Part II&lt;br /&gt;http://www.sqldbatips.com/showarticle.asp?ID=29&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;CREATE PROCEDURE usp_BackupDatabases&lt;br /&gt;@dbname SYSNAME,&lt;br /&gt;@backup_path VARCHAR(100)&lt;br /&gt;AS&lt;br /&gt;DECLARE @dt VARCHAR(20) ---date of db backup&lt;br /&gt;DECLARE @FileName VARCHAR(50) ---build path and dbname&lt;br /&gt;DECLARE @sql VARCHAR(100) ---build final sql statement&lt;br /&gt;&lt;br /&gt;SET @dt =CONVERT(VARCHAR(15),GETDATE(),112)&lt;br /&gt;SELECT @FileName = @backup_path+@dbname + @dt+'.bak'&lt;br /&gt;&lt;br /&gt;SET @sql = N'BACKUP DATABASE [' + @dbname + '] TO DISK = ''' + @FileName + '''' &lt;br /&gt;EXEC(@sql)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The above is simple stored procedure that accepts two parameters and performs BACKUP of the database.Now that, we have created stored procedure lets put the call of the sp into a text file ( I use userdbfullbackup.sql) and place the file on filesystem.&lt;br /&gt;usp_BackupDatabases @dbname='$(DB)',@backup_path='$(BACKUPFOLDER)'&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;SQLCMD utility 'knows' to accept parameters (-v),so we can easily provide parameters.&lt;br /&gt;&lt;br /&gt;--Usage&lt;br /&gt;EXEC  master..xp_cmdshell 'SQLCMD -S URID\SQLSERVERDEV2005 -i"c:\userdbfullbackup.sql" -v DB="Northwind" -v BACKUPFOLDER="c:\"'&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;But wait , till now we just written a stored procedure that backups our databases and tested it , so what is about scheduling?&lt;br /&gt;&lt;br /&gt;Windows Servers/XP have Scheduled Tasks Wizard&lt;br /&gt;You can refernce to below KB&lt;br /&gt;http://support.microsoft.com/default.aspx?scid=kb;en-us;308569&amp;sd=tech&lt;br /&gt;&lt;br /&gt;Please see Jasper's explanation ( do not want repetition). &lt;br /&gt;http://www.sqldbatips.com/showarticle.asp?ID=27&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-2805046876974489152?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/2805046876974489152/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=2805046876974489152' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/2805046876974489152'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/2805046876974489152'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2008/03/schedule-database-backup-in-sql-2005.html' title='Scheduling database backup in SQL 2005 Express Edition'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>6</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-5618590660548643992</id><published>2008-02-24T04:55:00.000-08:00</published><updated>2008-02-24T05:14:42.300-08:00</updated><title type='text'>ISNULL changes to NOT to allow NULLs</title><content type='html'>Hi folks. Actually, it is pretty old trick/feature I would like to show you. Let's say we have a table called 'Test1' with one column as VARCHAR(10) which allows inserting  NULLs. Well,we want to change this  to NOT allow NULLs and I'm also going to change datatype for this column at the same time ,so just for testing let's assume that 'Test1' does not contain alphabetic values.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;CREATE TABLE Test1&lt;br /&gt;    (&lt;br /&gt;    Col1 varchar(10) NOT NULL,&lt;br /&gt;    )&lt;br /&gt;INSERT INTO Test1 VALUES('1')&lt;br /&gt;&lt;br /&gt;EXEC sp_rename 'Test1', 'Test1_Old'&lt;br /&gt;GO&lt;br /&gt;SELECT ISNULL(CAST(Col1 AS int), 0) AS Col1  ---Makes Col1 not to allow NULLs&lt;br /&gt;    INTO Test1&lt;br /&gt;    FROM Test1_Old&lt;br /&gt;&lt;br /&gt;DROP TABLE Test1_Old&lt;br /&gt;&lt;br /&gt;Make sure that you do not have duplicates, so you can create a constraint on the column.&lt;br /&gt;/*&lt;br /&gt;ALTER TABLE Test1&lt;br /&gt;    ADD CONSTRAINT PK_Test1&lt;br /&gt;    PRIMARY KEY (Col1)&lt;br /&gt;*/&lt;br /&gt;GO&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-5618590660548643992?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/5618590660548643992/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=5618590660548643992' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/5618590660548643992'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/5618590660548643992'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2008/02/isnull-changes-to-not-to-allow-nulls.html' title='ISNULL changes to NOT to allow NULLs'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-7888599206083825997</id><published>2008-02-12T00:56:00.000-08:00</published><updated>2008-02-12T01:36:17.256-08:00</updated><title type='text'>Percent of completion for some operations in SS2005</title><content type='html'>Hi folks&lt;br /&gt;I have seen many and many businesses today have already upgraded to SQL Server 2005 and at this time I would like to share with you some of new features that this product gives us.&lt;br /&gt;As you know SQL Server 2005 provides lots of dynamic management views (DMV) to get access to internal behaviour of processes or commands. If I remember well, someone of MVPs has already published about this feature , so anywhere..&lt;br /&gt;&lt;br /&gt;Take a look at this script. Run for example BACKUP DATABASE comand and in another session run the below script. It provides you with start time column , percent of complete and what possible completion time is...(estimated_completion_time ) COOL, right?. &lt;br /&gt;&lt;br /&gt;SELECT TOP 2 start_time,&lt;br /&gt; percent_complete ,estimated_completion_time &lt;br /&gt; FROM sys.dm_exec_requests &lt;br /&gt;ORDER BY start_time desc &lt;br /&gt;&lt;br /&gt;Check it out with those commands&lt;br /&gt;&lt;br /&gt;DBCC CHECKDB&lt;br /&gt;DBCC SHRINKDATABASE&lt;br /&gt;DBCC SHRINKFILE&lt;br /&gt;BACKUP DATABASE&lt;br /&gt;ROLLBACK&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-7888599206083825997?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/7888599206083825997/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=7888599206083825997' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/7888599206083825997'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/7888599206083825997'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2008/02/percent-of-completion-for-some.html' title='Percent of completion for some operations in SS2005'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-7118645690160668426</id><published>2008-01-21T04:24:00.000-08:00</published><updated>2008-01-21T04:34:56.285-08:00</updated><title type='text'>Tables that do not have Clustered Index</title><content type='html'>I'd like to share with you this script that returns name of tables that do not have Clustered Index defined on.You may ask, why do we need to create a Clustered Index on the table at all? Well,I think it is a subject for another blog ,  my experience is that every table should have clustered index.&lt;br /&gt;&lt;br /&gt;--SQL Server 2005 (SP2)&lt;br /&gt;&lt;br /&gt;SELECT name AS object_name FROM sys.objects WHERE name NOT IN&lt;br /&gt;(&lt;br /&gt;SELECT o.name AS object_name&lt;br /&gt; FROM sys.indexes i&lt;br /&gt;JOIN sys.objects o&lt;br /&gt;ON i.object_id = o.object_id&lt;br /&gt;WHERE objectproperty(o.object_id,'IsMSShipped') =0&lt;br /&gt;AND I.type=1 ---Clustered Index&lt;br /&gt;)  AND objectproperty(sys.objects.object_id,'IsMSShipped') =0 ---Only user objects&lt;br /&gt; AND objectproperty(sys.objects.object_id,'IsTable') =1 ---Only tables&lt;br /&gt;ORDER BY name&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-7118645690160668426?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/7118645690160668426/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=7118645690160668426' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/7118645690160668426'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/7118645690160668426'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2008/01/tables-that-do-not-have-clustered-index.html' title='Tables that do not have Clustered Index'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-3290841326182302293</id><published>2008-01-08T06:14:00.000-08:00</published><updated>2008-01-08T06:40:23.108-08:00</updated><title type='text'>Columns_Updated() within a trigger</title><content type='html'>Happy New Year to everyone. Once someone asked me about what columns are affected by the UPDATE statement. As you probaly know that in SQL Server 2005 we can do that in more elegant way, but for SQL Server 2000 I decided perfrom some testing.&lt;br /&gt;&lt;br /&gt;CREATE TABLE Test (c1 INT NOT NULL PRIMARY KEY, c2 CHAR(1),c3 INT)&lt;br /&gt;GO&lt;br /&gt;--Insert some data&lt;br /&gt;INSERT INTO Test VALUES (1,'A',100)&lt;br /&gt;INSERT INTO Test VALUES (2,'B',500)&lt;br /&gt;&lt;br /&gt;--Create a trigger&lt;br /&gt;CREATE TRIGGER tr_my_trigger ON Test FOR UPDATE&lt;br /&gt;AS&lt;br /&gt;DECLARE @ColumnID INT&lt;br /&gt;DECLARE @Columns VARCHAR(8000)&lt;br /&gt;SET @Columns = SPACE(0)&lt;br /&gt;SET @ColumnID = 1&lt;br /&gt;WHILE @ColumnID &lt;= (SELECT COUNT(*)&lt;br /&gt;                   FROM INFORMATION_SCHEMA.COLUMNS&lt;br /&gt;                  WHERE TABLE_NAME = 'Test')&lt;br /&gt;BEGIN&lt;br /&gt;IF (SUBSTRING(COLUMNS_UPDATED(),(@ColumnID - 1) / 8 + 1, 1))  &amp;&lt;br /&gt;                POWER(2, (@ColumnID - 1) % 8) =&lt;br /&gt;                POWER(2, (@ColumnID - 1) % 8)&lt;br /&gt;    SET @Columns = @Columns + CAST(@ColumnID AS VARCHAR) + ','&lt;br /&gt;    SET @ColumnID = @ColumnID + 1&lt;br /&gt;END&lt;br /&gt;PRINT 'Updated columns are :' + @Columns &lt;br /&gt;&lt;br /&gt;---&lt;br /&gt;UPDATE Test SET c2='D',c3=2000 WHERE c1=1&lt;br /&gt;--Updated columns are :2,3&lt;br /&gt;We see that the second and the third column were affected according to the INFORMATION_SCHEMA.COLUMNS ordered by ORDINAL_POSITION (check it out). It is not comfortable to see those numbers , but with a little effort you can get also name of columns from the INFORMATION_SCHEMA.COLUMNS.&lt;br /&gt;&lt;br /&gt;I suggest you not to run/use that on the production server as it may hurt performance.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-3290841326182302293?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/3290841326182302293/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=3290841326182302293' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/3290841326182302293'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/3290841326182302293'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2008/01/columnsupdated-within-trigger.html' title='Columns_Updated() within a trigger'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>6</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-1351327930112833854</id><published>2007-12-17T22:22:00.000-08:00</published><updated>2007-12-17T22:45:20.568-08:00</updated><title type='text'>Rebuild index, when does it complete?</title><content type='html'>I have recently worked on the client site (SQL Server 2000) to help on optimization. They have a very huge table (about 250 million rows) and perfom rebuild index every day at night. That jobs has beeen completed in two hours or so till this monday. The job was in 'executin status' for more than 10 hours and imagine what happened to the LOG file. My first thing was it would be really helpfull to have any system table that shows a percentage or whatever  about the rebuild index process (something similar to what we have in SQL Server 2005 for some commands (BACKUP DATABASE) to see percent's complete )as I did not intend to KILL the process.Finally we got on and the process was completed. I suggested to the client rebuilding only heavy fragmented indexes especially if you have such huge table. Also , it is unnecessary to rebuild that table every day (it depends on your business requirements) ,so in their case , it is enough  once a week.&lt;br /&gt;&lt;br /&gt;In SQL Server 2005 for below operations you are able to see completion time or percent of completion.&lt;br /&gt;&lt;br /&gt;DBCC CHECKDB&lt;br /&gt;DBCC SHRINKDATABASE&lt;br /&gt;DBCC SHRINKFILE&lt;br /&gt;BACKUP DATABASE&lt;br /&gt;ROLLBACK&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;SELECT TOP 2 start_time,&lt;br /&gt; percent_complete ,estimated_completion_time &lt;br /&gt; FROM sys.dm_exec_requests &lt;br /&gt;ORDER by start_time desc&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-1351327930112833854?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/1351327930112833854/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=1351327930112833854' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/1351327930112833854'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/1351327930112833854'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2007/12/rebuild-index-when-does-it-complete.html' title='Rebuild index, when does it complete?'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-1830209059090387749</id><published>2007-12-02T04:51:00.000-08:00</published><updated>2007-12-02T05:03:19.675-08:00</updated><title type='text'>Shrink TempDB database</title><content type='html'>I have been recently worked for some client who has huge tempdb size (SQL Server 2000). As you probably know, you cannot reduce physical size of tempdb without restart MSSQLServices. Using the command DBCC SHRINKFILE to shrink the individual tempdb files you must run DBCC SHRINKFILE command while no other activity occurs in the tempdb database. To make sure that other processes cannot use tempdb while DBCC SHRINKFILE executes, you must restart SQL Server in the single user &lt;br /&gt;mode.&lt;br /&gt;So I would like to show some stepS we ended up. The client completely understands the problem cause tempdb database to be growing and I hope it is not short time solution.&lt;br /&gt;&lt;br /&gt;1) Stop SQL Server. Open a command prompt, and then start SQL Server by &lt;br /&gt;typing the following command:&lt;br /&gt;&lt;br /&gt;   "sqlservr -c -f" (remove the quotation)&lt;br /&gt;&lt;br /&gt;The -c and -f parameters force SQL Server to start in a minimum &lt;br /&gt;configuration mode with a tempdb size of 1 MB for the data file and 0.5 MB &lt;br /&gt;for the log file.&lt;br /&gt;Connect to SQL Server with Query Analyzer, and then issue the following &lt;br /&gt;TSQL commands for the tempdb database files that you need to shrink&lt;br /&gt;&lt;br /&gt;      use tempdb&lt;br /&gt;      go&lt;br /&gt;&lt;br /&gt;      dbcc shrinkfile (tempdev, 'target size in MB') --for the primary data file&lt;br /&gt;      or&lt;br /&gt;      dbcc shrinkfile (templog, 'target size in MB')--for the log file&lt;br /&gt;&lt;br /&gt;As MS says&lt;br /&gt;"An advantage of DBCC SHRINKFILE is that it can reduce the size of a file to &lt;br /&gt;a size smaller than its original size. A limitation of DBCC SHRINKFILE is that you cannot make the database smaller than the size of the model database."&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-1830209059090387749?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/1830209059090387749/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=1830209059090387749' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/1830209059090387749'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/1830209059090387749'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2007/12/shrink-tempdb-database.html' title='Shrink TempDB database'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-4936135282320960271</id><published>2007-11-13T05:41:00.000-08:00</published><updated>2007-11-13T05:59:15.931-08:00</updated><title type='text'>Non-updateable views</title><content type='html'>As we know from BOL &lt;br /&gt;"Updatable views can modify more than one table involved in the view. The DELETE, INSERT, and UPDATE statements can reference a view as long as SQL Server can translate the user's update request unambiguously to updates in the base tables referenced in the view's definition"&lt;br /&gt;&lt;br /&gt;But I would like to share with you one method to prevent updating view that based on single table. Let's see the following&lt;br /&gt;&lt;br /&gt;CREATE TABLE1 (col1 int,col2 int,col3 int,col4 int,col5 int)&lt;br /&gt;GO&lt;br /&gt;INSERT INTO t1 VALUES (1,1,11,2,12)&lt;br /&gt;INSERT INTO t1 VALUES (8,10,15,25,55)&lt;br /&gt;INSERT INTO t1 VALUES (9,1,11,2,5,81)&lt;br /&gt;GO&lt;br /&gt;CREATE VIEW V1 WITH VIEW_METADATA&lt;br /&gt;AS&lt;br /&gt;SELECT&lt;br /&gt;  col1,&lt;br /&gt;  col2+0 AS col2,&lt;br /&gt;  col3,&lt;br /&gt;  col4+0 AS col4,&lt;br /&gt;  col5&lt;br /&gt; FROM T1&lt;br /&gt;GO&lt;br /&gt;&lt;br /&gt;--Usage&lt;br /&gt;&lt;br /&gt;UPDATE v1 SET col1=100 where col2=10 &lt;br /&gt;The above statement is succeed, but what happened to the next one&lt;br /&gt;&lt;br /&gt;UPDATE v1 SET col2=1000 where col3=15&lt;br /&gt;--Update or insert of view or function 'v1' failed because it contains a derived or --constant field.&lt;br /&gt;&lt;br /&gt;You cannot derived column ,as it should be referring to the update, not the view.&lt;br /&gt;In SQL Server 2000, you can handle it using an INSTEAD OF trigger.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-4936135282320960271?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/4936135282320960271/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=4936135282320960271' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/4936135282320960271'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/4936135282320960271'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2007/11/non-updateable-views.html' title='Non-updateable views'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-4618689015296288336</id><published>2007-10-11T06:08:00.000-07:00</published><updated>2007-10-13T22:42:10.963-07:00</updated><title type='text'>Reading Registry</title><content type='html'>Sometimes we need to read registry by using T-SQL . I would like to show you some scripts  to do the job.&lt;br /&gt;&lt;br /&gt;--Getting MDAC&lt;br /&gt;EXEC master..xp_regread&lt;br /&gt;    N'HKEY_LOCAL_MACHINE',&lt;br /&gt;    N'Software\Microsoft\DataAccess',&lt;br /&gt;    N'Version' &lt;br /&gt;&lt;br /&gt;--Getting an account SQL Server runs under&lt;br /&gt;DECLARE @serviceaccount varchar(100)&lt;br /&gt;EXECUTE master.dbo.xp_instance_regread&lt;br /&gt;N'HKEY_LOCAL_MACHINE',&lt;br /&gt; N'SYSTEM\CurrentControlSet\Services\MSSQLSERVER',&lt;br /&gt; N'ObjectName',&lt;br /&gt; @ServiceAccount OUTPUT,&lt;br /&gt; N'no_output'&lt;br /&gt;&lt;br /&gt;SELECT @Serviceaccount&lt;br /&gt;&lt;br /&gt;---Getting SQL Server path's installation and the data path&lt;br /&gt;DECLARE @DataPath nvarchar( 512 ) , @SQLPath nvarchar( 512 ) ,&lt;br /&gt;        @ToolsPath nvarchar( 512 )&lt;br /&gt;EXECUTE sp_MSget_setup_paths @SQLPath OUT , @DataPath OUT;&lt;br /&gt;PRINT 'SQLPath   : ' + QUOTENAME( @SQLPath , '"');&lt;br /&gt;PRINT 'DataPath  : ' + QUOTENAME( @DataPath , '"');&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-4618689015296288336?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/4618689015296288336/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=4618689015296288336' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/4618689015296288336'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/4618689015296288336'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2007/10/reading-registry.html' title='Reading Registry'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-394457210097449088</id><published>2007-10-08T07:23:00.000-07:00</published><updated>2007-10-08T07:34:18.382-07:00</updated><title type='text'>How many times stored procedures have recompiled?</title><content type='html'>I have recently visited our client and we found out that He has a very high number of stored procedures that have recompiled. Certainly, we have observed some perfomance decreasing. Fortunately , the client has alredy upgraded to SQL Server 2005(SP2) and apart from running SQL Server Profile to indentify those stored procedures we came out with the following query.&lt;br /&gt;&lt;br /&gt;It gives you the top 10 stored procedures that have been recompiled.&lt;br /&gt;&lt;br /&gt;select *  &lt;br /&gt;from sys.dm_exec_query_optimizer_info &lt;br /&gt; &lt;br /&gt;select top 10 &lt;br /&gt;    sql_text.text, &lt;br /&gt;    sql_handle, &lt;br /&gt;    plan_generation_num, &lt;br /&gt;    execution_count, &lt;br /&gt;    dbid, &lt;br /&gt;    objectid  &lt;br /&gt;from  &lt;br /&gt;    sys.dm_exec_query_stats  &lt;br /&gt;    cross apply sys.dm_exec_sql_text(sql_handle) as sql_text &lt;br /&gt;where  &lt;br /&gt;    plan_generation_num &gt;1 &lt;br /&gt;order by plan_generation_num desc&lt;br /&gt;&lt;br /&gt;Note that  the plan_generation_num indicates the number of times the stored procedure has recompiled. Very cool feature.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-394457210097449088?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/394457210097449088/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=394457210097449088' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/394457210097449088'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/394457210097449088'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2007/10/how-many-times-stored-procedures-have.html' title='How many times stored procedures have recompiled?'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-4071513163545005148</id><published>2007-10-01T01:14:00.000-07:00</published><updated>2007-10-01T01:57:24.724-07:00</updated><title type='text'>RESTORE LOG file and WITH FILE behaviour</title><content type='html'>Recently I have been visited one of the our clients and have been asked the following question.&lt;br /&gt;"What if I have 3 fileid within a backup file and when restore the log being done, I do not specify WITH FILE option,so do I get latest Log's data?". To answer the question I decide to conduct some test script.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;CREATE DATABASE demo&lt;br /&gt;GO&lt;br /&gt;ALTER DATABASE demo SET RECOVERY FULL&lt;br /&gt;GO&lt;br /&gt;CREATE TABLE demo..t1(c1 INT NOT NULL PRIMARY KEY)&lt;br /&gt;--insert one row&lt;br /&gt;insert demo..t1 VALUES (1)&lt;br /&gt;--BACKUP the data&lt;br /&gt;BACKUP DATABASE demo TO DISK = 'c:\temp\demo.bak' WITH INIT&lt;br /&gt;GO&lt;br /&gt;insert demo..t1 VALUES (2)&lt;br /&gt;--start BACKUP log data &lt;br /&gt;BACKUP log demo  TO DISK = 'c:\temp\demo_log.bak'  WITH INIT&lt;br /&gt;GO&lt;br /&gt;insert demo..t1 VALUES (3)&lt;br /&gt;--- BACKUP log FILE at this time WITH FILE =2&lt;br /&gt;BACKUP log demo TO DISK = 'c:\temp\demo_log.bak'  WITH NOINIT&lt;br /&gt;GO&lt;br /&gt;insert demo..t1 VALUES (4)&lt;br /&gt;--- BACKUP log FILE at this time WITH FILE =3&lt;br /&gt;BACKUP log demo TO DISK = 'c:\temp\demo_log.bak'  WITH NOINIT&lt;br /&gt;&lt;br /&gt;--Lets do RESTORE and  see what is going on&lt;br /&gt;RESTORE DATABASE demo FROM DISK = 'c:\temp\demo.bak' WITH FILE = 1, NORECOVERY&lt;br /&gt;RESTORE LOG demo FROM DISK = 'c:\temp\demo_log.bak' WITH FILE = 1, NORECOVERY&lt;br /&gt;RESTORE LOG demo FROM DISK = 'c:\temp\demo_log.bak' WITH RECOVERY ----Do you expect the last one?&lt;br /&gt;&lt;br /&gt;GO&lt;br /&gt;SELECT * FROM demo..t1&lt;br /&gt;c1          &lt;br /&gt;----------- &lt;br /&gt;1&lt;br /&gt;2&lt;br /&gt;It did not return rows 3 and 4.&lt;br /&gt;&lt;br /&gt;So that means if you DO NOT specify WITH FILE in RESTORE LOG file you DO NOT get latest FILEID but only the first one.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-4071513163545005148?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/4071513163545005148/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=4071513163545005148' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/4071513163545005148'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/4071513163545005148'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2007/10/restore-log-file-and-with-file.html' title='RESTORE LOG file and WITH FILE behaviour'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-4504887895030210797</id><published>2007-09-23T00:51:00.000-07:00</published><updated>2007-09-23T01:03:04.599-07:00</updated><title type='text'>Insert data into a Text File</title><content type='html'>As you propably know that MS introduced in SQL Server 2005 a new command utility called SQLCMD. In this article I would like to show the difference between SQLCMD and "old fashioned" OSQL in terms of formating the text file's output.&lt;br /&gt;&lt;br /&gt;If you run these two statements and look at the files , you'll see that SQL Server inserts three dotted lines in myoutput2.txt, not so good for reading. Opposite, in myoutput1.txt you see very good reading format. &lt;br /&gt;&lt;br /&gt;EXEC  master..xp_cmdshell 'SQLCMD -S URID\SQLSERVERDEV2005 -E -Q "SELECT TOP 10 * FROM pubs.dbo.authors" -b -o c:\myoutput1.txt', no_output&lt;br /&gt;EXEC  master..xp_cmdshell 'OSQL -S URID\SQLSERVERDEV2005 -E -Q "SELECT TOP 10 * FROM pubs.dbo.authors" -b -o c:\myoutput2.txt', no_output&lt;br /&gt;&lt;br /&gt;Despite that SQL Server 2005 supports OSQL utility, I strongly recommend you using SQLCMD.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-4504887895030210797?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/4504887895030210797/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=4504887895030210797' title='8 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/4504887895030210797'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/4504887895030210797'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2007/09/insert-data-into-text-file.html' title='Insert data into a Text File'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>8</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-786613158129219741</id><published>2007-09-16T22:11:00.000-07:00</published><updated>2007-09-16T22:19:41.723-07:00</updated><title type='text'>Disable all Foreign Keys</title><content type='html'>Recently I was told to clear up all user tables in the client's database SQL Server 2000 SP3.There are lots of contsrtaints and for this purpose we would like to disable them in order to delete all data. Try it on Northwind database before using on production. If you want to enable constraints ,simple replace NOCHECK with CHECK.&lt;br /&gt;&lt;br /&gt;DECLARE @TableName nvarchar(257),&lt;br /&gt;    @ForeignKeyConstraintName sysname,&lt;br /&gt;    @SQLStatement nvarchar(4000)&lt;br /&gt;&lt;br /&gt;DECLARE TableList CURSOR&lt;br /&gt;    LOCAL FAST_FORWARD READ_ONLY FOR&lt;br /&gt;SELECT&lt;br /&gt;    QUOTENAME(TABLE_SCHEMA) +&lt;br /&gt;    N'.' +&lt;br /&gt;    QUOTENAME(TABLE_NAME) AS TableName,&lt;br /&gt;    QUOTENAME(CONSTRAINT_NAME) AS ForeignKeyConstraintName&lt;br /&gt;FROM&lt;br /&gt;    INFORMATION_SCHEMA.TABLE_CONSTRAINTS&lt;br /&gt;WHERE&lt;br /&gt;    CONSTRAINT_TYPE = 'FOREIGN KEY' AND&lt;br /&gt;    OBJECTPROPERTY&lt;br /&gt;    (&lt;br /&gt;        OBJECT_ID&lt;br /&gt;        (&lt;br /&gt;        QUOTENAME(TABLE_SCHEMA) +&lt;br /&gt;        N'.' +&lt;br /&gt;    QUOTENAME(TABLE_NAME)&lt;br /&gt;    ),&lt;br /&gt;    'IsMSShipped') = 0&lt;br /&gt;&lt;br /&gt;OPEN TableList&lt;br /&gt;WHILE 1 = 1&lt;br /&gt;BEGIN&lt;br /&gt;    FETCH NEXT FROM TableList INTO&lt;br /&gt;        @TableName,&lt;br /&gt;        @ForeignKeyConstraintName&lt;br /&gt;    IF @@FETCH_STATUS = -1 BREAK&lt;br /&gt;    SET @SQLStatement =&lt;br /&gt;        N'ALTER TABLE ' +&lt;br /&gt;        @TableName +&lt;br /&gt;        N' NOCHECK CONSTRAINT ' +&lt;br /&gt;        @ForeignKeyConstraintName&lt;br /&gt;    RAISERROR (@SQLStatement, 0, 1) WITH NOWAIT&lt;br /&gt;    EXEC sp_executesql @SQLStatement&lt;br /&gt;END&lt;br /&gt;CLOSE TableList&lt;br /&gt;DEALLOCATE TableList&lt;br /&gt;GO&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-786613158129219741?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/786613158129219741/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=786613158129219741' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/786613158129219741'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/786613158129219741'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2007/09/disable-all-foreign-keys.html' title='Disable all Foreign Keys'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-2024861373843506788</id><published>2007-08-28T02:42:00.000-07:00</published><updated>2007-08-28T02:50:50.444-07:00</updated><title type='text'>Getting an index's information</title><content type='html'>I was asked by one of the client to get all info about the indexes i.e name of the index , column's name, table's name etc. Please take a look at following script to return the data for SQL Server 2000.&lt;br /&gt;use pubs&lt;br /&gt;&lt;br /&gt;SELECT tbl = object_name(i.id), i.name as index_name, c.name as column_name,&lt;br /&gt;          isunique = indexproperty(i.id, i.name, 'IsUnique'),&lt;br /&gt;          isclustered = indexproperty(i.id, i.name, 'IsClustered'),&lt;br /&gt;          constrtype = CASE o.type&lt;br /&gt;                            WHEN 'PK' THEN 'PRIMARY KEY'&lt;br /&gt;                            WHEN 'UQ' THEN 'UNIQUE'&lt;br /&gt;                       END&lt;br /&gt;   FROM sysindexes i &lt;br /&gt;   JOIN syscolumns c on i.id = c.id&lt;br /&gt;   JOIN sysindexkeys k on i.id = k.id&lt;br /&gt;                   and i.indid = k.indid&lt;br /&gt;                   and c.colid = k.colid&lt;br /&gt;   LEFT JOIN sysobjects o ON o.name = i.name&lt;br /&gt;                           AND o.xtype in ('PK', 'UQ')&lt;br /&gt;                           AND o.parent_obj = i.id&lt;br /&gt;   WHERE indexproperty(i.id, i.name, 'IsHypothetical')   = 0&lt;br /&gt;     AND indexproperty(i.id, i.name, 'IsStatistics')     = 0&lt;br /&gt;     AND indexproperty(i.id, i.name, 'IsAutoStatistics') = 0&lt;br /&gt;     AND objectproperty(i.id,'IsMSShipped')=0&lt;br /&gt;   ORDER BY tbl, i.name, k.keyno&lt;br /&gt;&lt;br /&gt;For SQL Server 2005 I use the script written by Kalen Delaney.Remember there is new type of index called INCLUDE.&lt;br /&gt;&lt;br /&gt;CREATE VIEW get_index_columns&lt;br /&gt;AS&lt;br /&gt;SELECT object_name(ic.object_id) as object_name , index_name = i.name, &lt;br /&gt;'column' = c.name,&lt;br /&gt;        'column usage' = CASE ic.is_included_column&lt;br /&gt;                    WHEN 0 then 'KEY'&lt;br /&gt;                    ELSE 'INCLUDED'&lt;br /&gt;        END&lt;br /&gt;FROM sys.index_columns ic JOIN sys.columns c&lt;br /&gt;    ON ic.object_id = c.object_id&lt;br /&gt;    AND ic.column_id = c.column_id&lt;br /&gt;JOIN sys.indexes i&lt;br /&gt;    ON i.object_id = ic.object_id&lt;br /&gt;    AND i.index_id = ic.index_id&lt;br /&gt;&lt;br /&gt;After creating the view, you can select from it, and it will give you the &lt;br /&gt;KEY columns and the INCLUDED columns in all the indexes in all the tables. &lt;br /&gt;Or, you can add a WHERE clause for your own table or index:&lt;br /&gt;&lt;br /&gt;SELECT * FROM get_index_columns&lt;br /&gt;WHERE object_name = 'mytable'&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-2024861373843506788?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/2024861373843506788/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=2024861373843506788' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/2024861373843506788'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/2024861373843506788'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2007/08/getting-indexs-information.html' title='Getting an index&apos;s information'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-7145732794447428941</id><published>2007-08-14T01:20:00.000-07:00</published><updated>2007-11-06T21:53:05.714-08:00</updated><title type='text'>Foreign Key dependency</title><content type='html'>Some time ago I was asked to truncate lots of tables in client's database.As such,all those tables have FK and PK dependency. You get the error like that if you run TRUNCATE TABLE Categories in Nortwind database as an example.&lt;br /&gt;--Cannot truncate table 'Categories' because it is being referenced by a FOREIGN KEY constraint.&lt;br /&gt;&lt;br /&gt;So , there is no such great problem as you know the database structure and can easily indetify parent-child tables. But what if you don't?&lt;br /&gt;I'd like to show the function (sadly, I don't remember by whom it is written) that returns the tables ordered by its level. Level 0 means that it is refernced table and should be truncated last.&lt;br /&gt;&lt;br /&gt;CREATE VIEW VFKs&lt;br /&gt;AS&lt;br /&gt;&lt;br /&gt;SELECT&lt;br /&gt;  FK.TABLE_SCHEMA AS child_table_schema,&lt;br /&gt;  FK.TABLE_NAME AS child_table_name,&lt;br /&gt;  PK.TABLE_SCHEMA AS parent_table_schema,&lt;br /&gt;  PK.TABLE_NAME AS parent_table_name&lt;br /&gt;FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS AS RC&lt;br /&gt;  JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS FK&lt;br /&gt;    ON RC.CONSTRAINT_SCHEMA = FK.CONSTRAINT_SCHEMA&lt;br /&gt;    AND RC.CONSTRAINT_NAME = FK.CONSTRAINT_NAME&lt;br /&gt;  JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS PK&lt;br /&gt;    ON RC.UNIQUE_CONSTRAINT_SCHEMA = PK.CONSTRAINT_SCHEMA&lt;br /&gt;    AND RC.UNIQUE_CONSTRAINT_NAME = PK.CONSTRAINT_NAME&lt;br /&gt;GO&lt;br /&gt;&lt;br /&gt;CREATE FUNCTION dbo.fn_get_tree_of_tables()&lt;br /&gt;  RETURNS @tree TABLE&lt;br /&gt;(&lt;br /&gt;  table_schema SYSNAME NOT NULL,&lt;br /&gt;  table_name SYSNAME NOT NULL,&lt;br /&gt;  lvl INT NOT NULL,&lt;br /&gt;  PRIMARY KEY(table_schema, table_name)&lt;br /&gt;)&lt;br /&gt;AS&lt;br /&gt;BEGIN&lt;br /&gt;&lt;br /&gt;  DECLARE @lvl AS INT&lt;br /&gt;  SET @lvl = 0&lt;br /&gt;&lt;br /&gt;  -- top level tables&lt;br /&gt;  INSERT INTO @tree&lt;br /&gt;    SELECT DISTINCT&lt;br /&gt;      parent_table_schema,&lt;br /&gt;      parent_table_name,&lt;br /&gt;      @lvl&lt;br /&gt;    FROM VFKs AS P&lt;br /&gt;    WHERE NOT EXISTS&lt;br /&gt;      (SELECT *&lt;br /&gt;       FROM VFKs AS C&lt;br /&gt;       WHERE C.child_table_schema = P.parent_table_schema&lt;br /&gt;         AND  C.child_table_name = P.parent_table_name)&lt;br /&gt;&lt;br /&gt;  WHILE @@rowcount &gt; 0&lt;br /&gt;  BEGIN&lt;br /&gt;&lt;br /&gt;    SET @lvl = @lvl + 1&lt;br /&gt;&lt;br /&gt;    -- non top level tables&lt;br /&gt;    INSERT INTO @tree&lt;br /&gt;      SELECT DISTINCT&lt;br /&gt;        child_table_schema,&lt;br /&gt;        child_table_name,&lt;br /&gt;        @lvl&lt;br /&gt;      FROM VFKs AS C&lt;br /&gt;        JOIN @tree AS P&lt;br /&gt;         ON P.table_schema = C.parent_table_schema&lt;br /&gt;         AND P.table_name = C.parent_table_name&lt;br /&gt;         AND lvl = @lvl - 1&lt;br /&gt;      WHERE NOT EXISTS&lt;br /&gt;        (SELECT *&lt;br /&gt;         FROM @tree AS T&lt;br /&gt;         WHERE T.table_schema = C.child_table_schema&lt;br /&gt;           AND T.table_name = C.child_table_name)&lt;br /&gt;&lt;br /&gt;  END&lt;br /&gt;&lt;br /&gt;  SET @lvl = 0&lt;br /&gt;&lt;br /&gt;  -- tables with no fks&lt;br /&gt;  INSERT INTO @tree&lt;br /&gt;    SELECT TABLE_SCHEMA, TABLE_NAME, @lvl&lt;br /&gt;    FROM INFORMATION_SCHEMA.TABLES AS TB&lt;br /&gt;    WHERE NOT EXISTS(&lt;br /&gt;        SELECT *&lt;br /&gt;        FROM @tree AS TR&lt;br /&gt;        WHERE TB.TABLE_SCHEMA = TR.table_schema&lt;br /&gt;          AND TB.TABLE_NAME = TR.table_name)&lt;br /&gt;      AND&lt;br /&gt;        TB.TABLE_TYPE = 'BASE TABLE'&lt;br /&gt;&lt;br /&gt;  RETURN&lt;br /&gt;&lt;br /&gt;END&lt;br /&gt;GO&lt;br /&gt;&lt;br /&gt;Here's the results of an invocation of the function in Northwind:&lt;br /&gt;&lt;br /&gt;SELECT * FROM dbo.fn_get_tree_of_tables() AS T&lt;br /&gt;ORDER BY lvl&lt;br /&gt;&lt;br /&gt;table_schema table_name                          lvl&lt;br /&gt;------------ ----------------------------------- ---&lt;br /&gt;dbo          Categories                          0&lt;br /&gt;dbo          CustomerDemographics                0&lt;br /&gt;dbo          Customers                           0&lt;br /&gt;dbo          Employees                           0&lt;br /&gt;dbo          Region                              0&lt;br /&gt;dbo          Shippers                            0&lt;br /&gt;dbo          Suppliers                           0&lt;br /&gt;dbo          Products                            1&lt;br /&gt;dbo          Territories                         1&lt;br /&gt;dbo          Orders                              1&lt;br /&gt;dbo          CustomerCustomerDemo                1&lt;br /&gt;dbo          Order Details                       2&lt;br /&gt;dbo          EmployeeTerritories                 2&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-7145732794447428941?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/7145732794447428941/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=7145732794447428941' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/7145732794447428941'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/7145732794447428941'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2007/08/foreign-key-dependency.html' title='Foreign Key dependency'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-1022043481505588464</id><published>2007-08-06T05:30:00.000-07:00</published><updated>2007-08-06T05:46:17.324-07:00</updated><title type='text'>Object's modified date</title><content type='html'>There are lots of businesses have already upgraded to SQL Server 2005.At this time I'd like to take a look at great feature that we have in SQL Server 2005. I have been asked so many times how to know WHEN does the object change? It is a headache in SQL Server 2000. Fortunately, now we can answer that question.&lt;br /&gt;&lt;br /&gt;Let's say the user changed number of stored procedures.Using the following statement we can easily get the data.&lt;br /&gt;&lt;br /&gt;SELECT name,modify_date FROM sys.procedures&lt;br /&gt;ORDER BY modify_date desc&lt;br /&gt;&lt;br /&gt;But let's take it a little bit futher. I'd like to know when does user update the table last time?&lt;br /&gt;&lt;br /&gt;CREATE TABLE dbo.uri(c INT NOT NULL PRIMARY KEY)&lt;br /&gt;GO&lt;br /&gt;INSERT INTOdbo.uri VALUES(10)&lt;br /&gt;--we got one entry &lt;br /&gt;&lt;br /&gt;SELECT object_name(object_id) AS name ,last_user_update&lt;br /&gt;FROM sys.dm_db_index_usage_stats&lt;br /&gt;WHERE database_id = db_id( 'demo' )&lt;br /&gt;&lt;br /&gt;---try to change the data&lt;br /&gt;UPDATE dbo.uri SET c =50 WHERE c =10&lt;br /&gt;&lt;br /&gt;--you'll see that last_user_update  is changed&lt;br /&gt;&lt;br /&gt;SELECT object_name(object_id) AS name ,last_user_update&lt;br /&gt;FROM sys.dm_db_index_usage_stats&lt;br /&gt;WHERE database_id = db_id( 'demo' )&lt;br /&gt;&lt;br /&gt;So ,you can play with these Dynamic Management Views to obtain more info.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-1022043481505588464?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/1022043481505588464/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=1022043481505588464' title='8 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/1022043481505588464'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/1022043481505588464'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2007/08/objects-modified-date.html' title='Object&apos;s modified date'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>8</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-2033873425477930309</id><published>2007-07-10T00:23:00.000-07:00</published><updated>2007-07-10T00:34:51.213-07:00</updated><title type='text'>Columns List of Views</title><content type='html'>One of myclient asked me nice question about getting columns list for views. Well, please take a look at following solution I came up with.&lt;br /&gt; &lt;br /&gt;The first view just holds the data about view and their columns and as you can see it is not easy to read it at all. &lt;br /&gt;&lt;br /&gt;CREATE VIEW v_getcolumns&lt;br /&gt;AS&lt;br /&gt;SELECT  INFORMATION_SCHEMA.TABLES.TABLE_NAME ,COLUMN_NAME&lt;br /&gt;FROM INFORMATION_SCHEMA.TABLES JOIN INFORMATION_SCHEMA.COLUMNS &lt;br /&gt;ON INFORMATION_SCHEMA.TABLES.TABLE_NAME=INFORMATION_SCHEMA.COLUMNS.TABLE_NAME&lt;br /&gt;WHERE TABLE_TYPE = 'VIEW' AND&lt;br /&gt;    OBJECTPROPERTY(&lt;br /&gt;        OBJECT_ID(&lt;br /&gt;            QUOTENAME(INFORMATION_SCHEMA.TABLES.TABLE_SCHEMA) + '.' +&lt;br /&gt;            QUOTENAME(INFORMATION_SCHEMA.TABLES.TABLE_NAME)&lt;br /&gt;        ), 'IsMSShipped') = 0&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;The final step I build a crosstab  to contacanate the names of the columns per view based on count.I ened upi with 10 columns per view as maximum number, you can modify it for your needs.The cool thing is if you specify '*' in SELECT statement within a view and running the following query you get all columns of the table.&lt;br /&gt;&lt;br /&gt;SELECT TABLE_NAME ,MAX(CASE WHEN rnk = 1 then COLUMN_NAME ELSE '' end)&lt;br /&gt;           +MAX(CASE WHEN rnk =  2 THEN ', ' + COLUMN_NAME ELSE '' end)&lt;br /&gt;           +MAX(CASE WHEN rnk =  3 THEN ', ' + COLUMN_NAME ELSE '' end)&lt;br /&gt;           +MAX(CASE WHEN rnk =  4 THEN ', ' + COLUMN_NAME ELSE '' end) &lt;br /&gt;           +MAX(CASE WHEN rnk =  5 THEN ', ' + COLUMN_NAME ELSE '' end)&lt;br /&gt;           +MAX(CASE WHEN rnk =  6 THEN ', ' + COLUMN_NAME ELSE '' end)&lt;br /&gt;           +MAX(CASE WHEN rnk =  7 THEN ', ' + COLUMN_NAME ELSE '' end)&lt;br /&gt;           +MAX(CASE WHEN rnk =  8 THEN ', ' + COLUMN_NAME ELSE '' end)&lt;br /&gt;           +MAX(CASE WHEN rnk =  9 THEN ', ' + COLUMN_NAME ELSE '' end)&lt;br /&gt;           +MAX(CASE WHEN rnk =  10 THEN ', ' + COLUMN_NAME ELSE '' end) AS columns&lt;br /&gt;FROM&lt;br /&gt;(&lt;br /&gt;SELECT A.TABLE_NAME ,A.COLUMN_NAME,COUNT(*)rnk&lt;br /&gt;FROM v_getcolumns A, v_getcolumns B&lt;br /&gt;WHERE A.TABLE_NAME=B.TABLE_NAME AND&lt;br /&gt;A.COLUMN_NAME&gt;=B.COLUMN_NAME&lt;br /&gt;GROUP BY a.TABLE_NAME ,a.COLUMN_NAME&lt;br /&gt;) AS der&lt;br /&gt;GROUP BY TABLE_NAME&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-2033873425477930309?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/2033873425477930309/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=2033873425477930309' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/2033873425477930309'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/2033873425477930309'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2007/07/columns-list-of-views.html' title='Columns List of Views'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-1886730966868348457</id><published>2007-07-08T05:09:00.000-07:00</published><updated>2007-07-08T05:34:05.931-07:00</updated><title type='text'>T-SQL and CLR types do not match</title><content type='html'>As  you know in SQL Server 2005 you can reference to .NET classes doing some operations that in T-SQL is realy hard to perform. For example if you need to format very long and 'complicated' string value , so by using .NET String method you can easily achive it. But I'd like to show you another example actually posted by Itzik Ben-Gan on his website ,so I just modified his C# code to VB.NET that is invoking the ToString method of the SqlDateTime datatype with a format string.&lt;br /&gt;&lt;br /&gt;Imports Microsoft.SqlServer.Server&lt;br /&gt;Imports System.Data.SqlClient&lt;br /&gt;Public Class Valid&lt;br /&gt;    "&lt;"SqlFunction(DataAccess:=DataAccessKind.Read, IsDeterministic:=True)"&gt;" _&lt;br /&gt;         Public Shared Function fn_format_datetime(ByVal dt As SqlTypes.SqlDateTime, _&lt;br /&gt;        ByVal formatstring As SqlTypes.SqlString) As String&lt;br /&gt;        If dt.IsNull Or formatstring.IsNull Then&lt;br /&gt;            Return formatstring.IsNull()&lt;br /&gt;        Else&lt;br /&gt;            Return dt.Value.ToString(formatstring.Value)&lt;br /&gt;        End If&lt;br /&gt;&lt;br /&gt;    End Function&lt;br /&gt;&lt;br /&gt;End Class&lt;br /&gt;&lt;br /&gt;I assume you are familiar with .NET technology , so I build a solution  and try to reference to this function within my database.I created ASSEMBLY without problem but when I tried to create a function I got the error "CREATE FUNCTION for "fn_format_datetime1" failed because T-SQL and CLR types for return value do not match." &lt;br /&gt;&lt;br /&gt;CREATE ASSEMBLY dateformat1 FROM 'C:\dateformat1 .dll'&lt;br /&gt;with permission_set = SAFE&lt;br /&gt;CREATE FUNCTION  fn_format_datetime(@dt as datetime,&lt;br /&gt; @formatstring as VARCHAR(100))RETURNS VARCHAR(100)&lt;br /&gt;AS EXTERNAL NAME dateformat1.[dateformat1.classname].fn_format_datetime&lt;br /&gt;&lt;br /&gt;As you see T-SQL datatype VARCHAR(n) does not match .NET STRING class. I should use NVARCHAR(n) datatype to get it working, did not know it.&lt;br /&gt;&lt;br /&gt;CREATE ASSEMBLY dateformat1 FROM 'C:\dateformat1 .dll'&lt;br /&gt;with permission_set = SAFE&lt;br /&gt;CREATE FUNCTION  fn_format_datetime(@dt as datetime,&lt;br /&gt; @formatstring as NVARCHAR(4000))RETURNS NVARCHAR(4000)&lt;br /&gt;AS EXTERNAL NAME dateformat1.[dateformat1.classname].fn_format_datetime&lt;br /&gt;&lt;br /&gt;--Usage&lt;br /&gt;SELECT &lt;br /&gt;  dbo.fn_format_datetime(GETDATE(), 'yyyyMMdd'),&lt;br /&gt;   dbo.fn_format_datetime(GETDATE(), 'yyyymmdd');&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-1886730966868348457?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/1886730966868348457/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=1886730966868348457' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/1886730966868348457'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/1886730966868348457'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2007/07/t-sql-and-clr-types-do-not-match.html' title='T-SQL and CLR types do not match'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-7676972383922998557</id><published>2007-06-26T02:33:00.000-07:00</published><updated>2007-07-03T23:51:27.133-07:00</updated><title type='text'>Trigger's definition</title><content type='html'>I have had a clinet who wanted to script out the source of triggers on database level, so I'm talking about SQL Server 2005. Well as you probably know you can easily script the objects by using Tasks ---&gt; Generate Scripts.. and moreover if you installed SP2 you will be able to script the object per file.&lt;br /&gt;&lt;br /&gt;I would like to show you more simple way. You join two system tables  on object_id column where sys.sql_modules table has a definition column which my client wanted to get.The WHERE condition restricts the ouptupt for triggers that were designed on database level.&lt;br /&gt;&lt;br /&gt;SELECT definition FROM  sys.sql_modules sm&lt;br /&gt;INNER JOIN sys.triggers st ON&lt;br /&gt;sm.object_id=st.object_id&lt;br /&gt;WHERE parent_class_desc ='DATABASE'&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-7676972383922998557?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/7676972383922998557/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=7676972383922998557' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/7676972383922998557'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/7676972383922998557'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2007/06/triggers-definition.html' title='Trigger&apos;s definition'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-8722542293984290747</id><published>2007-06-20T05:34:00.000-07:00</published><updated>2008-03-10T00:25:25.597-07:00</updated><title type='text'>Is Linked Server available?</title><content type='html'>In SQL Server 2000 we have been checking an availability of linked server by using &lt;br /&gt;xp_cmdshell along with 'ping' command.It worked fine but what does SQL Server 2005 propose to us?&lt;br /&gt;There is a system stored procedure (sp_testlinkedserver )that accepts name of linked server as a parameter and returns the status of server.&lt;br /&gt;&lt;br /&gt;Take a look at the script below.&lt;br /&gt;&lt;br /&gt;DECLARE @srvr NVARCHAR(128), @statusval INT;&lt;br /&gt;SET @srvr = 'Linked_Server';&lt;br /&gt;BEGIN TRY&lt;br /&gt;    EXEC @statusval = sp_testlinkedserver @srvr;&lt;br /&gt;END TRY&lt;br /&gt;BEGIN CATCH&lt;br /&gt;    SET @statusval = SIGN(@@ERROR);&lt;br /&gt;END CATCH;&lt;br /&gt;IF @statusval &lt;&gt; 0&lt;br /&gt;  RAISERROR('Unable to connect to linked server. Try later!', 16, 2 );&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-8722542293984290747?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/8722542293984290747/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=8722542293984290747' title='6 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/8722542293984290747'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/8722542293984290747'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2007/06/is-linked-server-available.html' title='Is Linked Server available?'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>6</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-7034003321011227680</id><published>2007-06-03T04:38:00.000-07:00</published><updated>2007-06-03T23:55:17.736-07:00</updated><title type='text'>Enabling AWE on Cluster environment</title><content type='html'>I know many people ask this question. Recently I had a client wants me to enable AWE on the cluster which has two nodes (Active/Passive). He had only one instance of SQL Server 2000 (SP4) and 8 GB RAM and /3GB in the Boot.INI.&lt;br /&gt;&lt;br /&gt;1)&lt;br /&gt;Add /PAE parameter to Boot.INI file. PAE is the added ability of the IA32 processor to address more than 4 GB of physical memory.&lt;br /&gt;2)&lt;br /&gt;You need to install hotfix on SQL Server instance from Microsoft (http://support.microsoft.com/kb/899761) that fixs the bug "Not all memory is available when AWE is enabled".&lt;br /&gt;3)&lt;br /&gt;Make sure that domain account SQL Server is running under is added to "Lock pages in memory" local group policy.&lt;br /&gt;4)&lt;br /&gt;Finally we configured 6 GB for SQL Server as it was business requirements.&lt;br /&gt;&lt;br /&gt;sp_configure 'show advanced options', 1&lt;br /&gt;RECONFIGURE&lt;br /&gt;GO&lt;br /&gt;sp_configure 'awe enabled', 1&lt;br /&gt;RECONFIGURE&lt;br /&gt;GO&lt;br /&gt;sp_configure 'max server memory', 6144&lt;br /&gt;RECONFIGURE&lt;br /&gt;&lt;br /&gt;5)Restart OS&lt;br /&gt;&lt;br /&gt;Note: we need to run those steps on Active node where the SQL Server instance is running on. When it failed over but the binaries are loaded on all &lt;br /&gt;nodes.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-7034003321011227680?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/7034003321011227680/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=7034003321011227680' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/7034003321011227680'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/7034003321011227680'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2007/06/enabling-awe-on-cluster-environment.html' title='Enabling AWE on Cluster environment'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-6524091467848706830</id><published>2007-05-16T02:46:00.000-07:00</published><updated>2007-05-16T21:22:32.973-07:00</updated><title type='text'>Linked servers and an Identity property</title><content type='html'>Someone in sqlserver.programming group asked nice question about getting an IDENTITY from the linked server. I came up with the following solution. I assume that there is a table on the linked server that has one column defined as indentity property.&lt;br /&gt; &lt;br /&gt;CREATE FUNCTION dbo.fn_get_remoteidentity() &lt;br /&gt;RETURNS INT&lt;br /&gt;AS&lt;br /&gt;BEGIN&lt;br /&gt;  RETURN(&lt;br /&gt;    SELECT row_id&lt;br /&gt;    FROM OPENQUERY(&lt;br /&gt;           [put here your server name],&lt;br /&gt;           'SET NOCOUNT ON;&lt;br /&gt;            INSERT INTO dbname..usertable DEFAULT VALUES &lt;br /&gt;            ROLLBACK;&lt;br /&gt;            SELECT SCOPE_IDENTITY() AS row_id;') AS Der)&lt;br /&gt;END&lt;br /&gt;Another question was how to insert an INDENTITY property in the table on remote(linked) server?&lt;br /&gt;Well in that case you will need to build dynamic SQL along with SET IDENTITY_INSERT&lt;br /&gt;command.&lt;br /&gt;&lt;br /&gt;DECLARE @sql NVARCHAR(400)&lt;br /&gt;SELECT @sql = ' &lt;br /&gt;        CREATE TABLE test(id INT IDENTITY(1,1)) &lt;br /&gt;        SET IDENTITY_INSERT test ON &lt;br /&gt;        INSERT test DEFAULT VALUES &lt;br /&gt;        SET IDENTITY_INSERT test OFF &lt;br /&gt;        SELECT id FROM test'&lt;br /&gt;--Usage&lt;br /&gt;EXEC [LinkedServer].master.dbo.sp_executesql @sql&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-6524091467848706830?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/6524091467848706830/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=6524091467848706830' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/6524091467848706830'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/6524091467848706830'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2007/05/linked-servers-and-identity.html' title='Linked servers and an Identity property'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-4211263123701694104</id><published>2007-05-13T02:46:00.000-07:00</published><updated>2007-05-13T07:41:44.553-07:00</updated><title type='text'>Global stored procedure</title><content type='html'>I have my doubts that many of us have been used this system stored procedure(sp_MS_marksystemobject) to resolve the problems. But it exists and gives you an ability to return the data in the scope of selected database. Well, across number of databases having one table with the same structure and being in the scope of desired database you'll return the data (by using the following stored procedure) from the table located in desired database.To make the stored procedure to recognize the 'desired' data we use sp_MS_marksystemobject system stored procedure. BTW, it works as well as on SQL Server 2000 and on SQL Server 2005(SP2).&lt;br /&gt;&lt;br /&gt;USE master&lt;br /&gt;CREATE TABLE test (c1 VARCHAR(50)) &lt;br /&gt;INSERT test VALUES('master')&lt;br /&gt;go&lt;br /&gt;CREATE PROC sp_test AS&lt;br /&gt;SELECT * FROM test&lt;br /&gt;GO&lt;br /&gt;USE northwind&lt;br /&gt;CREATE TABLE test (c1 VARCHAR(50)) &lt;br /&gt;INSERT test VALUES('northwind')&lt;br /&gt;USE pubs&lt;br /&gt;CREATE TABLE test(c1 VARCHAR(50))&lt;br /&gt;INSERT test VALUES('pubs')&lt;br /&gt;USE pubs&lt;br /&gt;EXEC sp_test --returns 'master'&lt;br /&gt;USE master&lt;br /&gt;EXEC sp_MS_marksystemobject sp_test&lt;br /&gt;USE pubs&lt;br /&gt;EXEC sp_test --returns 'pubs'&lt;br /&gt;USE northwind&lt;br /&gt;EXEC sp_test --returns 'northwind'&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-4211263123701694104?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/4211263123701694104/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=4211263123701694104' title='7 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/4211263123701694104'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/4211263123701694104'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2007/05/global-stored-procedure.html' title='Global stored procedure'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>7</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-4595073671193006657</id><published>2007-05-09T00:42:00.000-07:00</published><updated>2007-05-09T00:48:18.113-07:00</updated><title type='text'>Find specific data type of the column</title><content type='html'>Recently i was told to return tables which have FLOAT datatype defined on columns.The version of the product was  SQL Server 2000 ,however it will work on SQL Server 2005 as well, so take a look at very very simple script.&lt;br /&gt;&lt;br /&gt;SELECT   columns.TABLE_NAME ,COLUMN_NAME&lt;br /&gt;FROM     information_schema.columns columns&lt;br /&gt;JOIN     information_schema.tables  tables&lt;br /&gt;ON       tables.table_name = columns.table_name&lt;br /&gt;WHERE    tables.table_type = 'base table'&lt;br /&gt;AND      columns.data_type = 'float'&lt;br /&gt;ORDER BY columns.table_name, columns.column_name&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-4595073671193006657?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/4595073671193006657/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=4595073671193006657' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/4595073671193006657'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/4595073671193006657'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2007/05/find-specific-data-type-of-column.html' title='Find specific data type of the column'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-3136780803721424051</id><published>2007-05-06T04:01:00.000-07:00</published><updated>2007-05-06T21:15:00.200-07:00</updated><title type='text'>Can we look into LOG file?</title><content type='html'>In SQL Server 2005 with fn_dblog function you can get more info about what's happening in SQL Server. MS has added a new data like 'partitionid' column for example.&lt;br /&gt;&lt;br /&gt;Let us do some testing&lt;br /&gt;&lt;br /&gt;USE Demo&lt;br /&gt;--create a table for testing&lt;br /&gt;CREATE TABLE dbo.Test1 (c INT)&lt;br /&gt;--populate the table with data&lt;br /&gt;&lt;br /&gt;WITH numbers AS&lt;br /&gt;(SELECT 1 as Num&lt;br /&gt;UNION ALL &lt;br /&gt;SELECT Num + 1 FROM numbers WHERE Num &lt;10) &lt;br /&gt;INSERT INTO dbo.Test1 SELECT Num FROM  numbers OPTION(Maxrecursion 10)&lt;br /&gt;&lt;br /&gt;--delete the data&lt;br /&gt;DELETE FROM  dbo.Test1 &lt;br /&gt;&lt;br /&gt;In order to identify how many 'DELETE' operation  and when does it start? Run the followinq query.&lt;br /&gt;&lt;br /&gt;SELECT [Transaction Name],[begin time FROM ::fn_dblog( NULL,NULL)WHERE  [Transaction Name] ='DELETE'&lt;br /&gt;&lt;br /&gt;Ok, the next step I'd like to know how long does DELETION take? So we have  [end time column] for this purpose. We need to write the below query  which gets little complicated.&lt;br /&gt;&lt;br /&gt;WITH cte_delete&lt;br /&gt;AS&lt;br /&gt;(&lt;br /&gt;SELECT [Transaction Name],[begin time],[end time]&lt;br /&gt;FROM ::fn_dblog( NULL,NULL)d&lt;br /&gt;WHERE [begin time] IS NOT NULL OR [end time]IS NOT NULL&lt;br /&gt;) ,cte_d&lt;br /&gt;AS&lt;br /&gt;(&lt;br /&gt; SELECT [Transaction Name],[begin time],(SELECT MIN([end time]) FROM cte_delete C&lt;br /&gt; WHERE cte_delete.[begin time]&lt;=C.[end time])AS [end time]&lt;br /&gt; FROM cte_delete WHERE [Transaction Name] ='DELETE' OR&lt;br /&gt;[Transaction Name]  IS NULL &lt;br /&gt; )&lt;br /&gt;SELECT * FROM cte_d&lt;br /&gt;WHERE [begin time] IS NOT NULL OR [end time]IS NOT NULL&lt;br /&gt;&lt;br /&gt;Also take a look at 'Operation' column.&lt;br /&gt;&lt;br /&gt;SELECT Operation,AllocUnitName,[Transaction Name],[begin time],[end time]&lt;br /&gt;FROM ::fn_dblog( NULL,NULL)&lt;br /&gt;WHERE Operation ='LOP_DELETE_ROWS' OR [Transaction Name]='DELETE'&lt;br /&gt;&lt;br /&gt;To identify which tables are being hit  by DELETE operation.&lt;br /&gt;&lt;br /&gt;SELECT AllocUnitName, COUNT([Current LSN])&lt;br /&gt;FROM ::fn_dblog(NULL,NULL )&lt;br /&gt;WHERE Operation = N'LOP_DELETE_ROWS'&lt;br /&gt;GROUP BY AllocUnitName&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-3136780803721424051?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/3136780803721424051/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=3136780803721424051' title='39 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/3136780803721424051'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/3136780803721424051'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2007/05/can-we-look-into-log-file.html' title='Can we look into LOG file?'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>39</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-8719747271428633322</id><published>2007-05-01T01:27:00.000-07:00</published><updated>2007-05-01T01:41:45.873-07:00</updated><title type='text'>String manipulation (extracting)</title><content type='html'>I probably should create some group of articles for string manuipulation.At this time I'd like to show you an example to extract portion of text prior/after to some charcter/symbol.An idea is simple to iterate through string and return the text prior (in my example)  to 'N' appearance of specified symbol.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;CREATE FUNCTION dbo.udf_Extract(&lt;br /&gt;  @target varchar(8000),&lt;br /&gt;  @string varchar(8000),&lt;br /&gt;  @i int&lt;br /&gt;) RETURNS varchar(8000) AS BEGIN&lt;br /&gt;  DECLARE @pos int&lt;br /&gt;  SET @pos = 0&lt;br /&gt;  WHILE @i &gt; 0 BEGIN&lt;br /&gt;    SET @pos = CHARINDEX(@target,@string,@pos+1)&lt;br /&gt;    SET @i = @i - 1&lt;br /&gt;    IF @pos = 0 RETURN '0'&lt;br /&gt;  END&lt;br /&gt;  RETURN SUBSTRING (@string,1,@pos-1)&lt;br /&gt;END&lt;br /&gt;GO&lt;br /&gt;&lt;br /&gt;--Usage&lt;br /&gt;SELECT  dbo.udf_Extract('@','hjdhjdhj@hjjsd@hjdfhdfj@jjj,3)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-8719747271428633322?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/8719747271428633322/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=8719747271428633322' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/8719747271428633322'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/8719747271428633322'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2007/05/string-manipulation-extracting.html' title='String manipulation (extracting)'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-2882178071942467993</id><published>2007-04-29T03:44:00.000-07:00</published><updated>2007-04-29T03:51:40.976-07:00</updated><title type='text'>Which tables have a trigger/s?</title><content type='html'>I have been recently helping my fried on very large database ,which has hundred od tables to determine which table has a trigger. So I came out with the below script that may help others to identify triggers define on the tables.&lt;br /&gt; &lt;br /&gt;--SQL Server 2000(SP3)&lt;br /&gt;&lt;br /&gt;SELECT&lt;br /&gt;    name&lt;br /&gt;,   CASE WHEN instrig &lt;&gt; 0 then OBJECT_NAME (instrig) ELSE 'None' END as&lt;br /&gt;'Insert Trigger'&lt;br /&gt;,   CASE WHEN updtrig &lt;&gt; 0 then OBJECT_NAME  (updtrig) ELSE 'None' END as&lt;br /&gt;'Update Trigger'&lt;br /&gt;,   CASE WHEN  deltrig &lt;&gt; 0 then OBJECT_NAME (deltrig) ELSE 'None' END as&lt;br /&gt;'Delete Trigger'&lt;br /&gt;FROM&lt;br /&gt;    sysobjects&lt;br /&gt;WHERE&lt;br /&gt;    type    = 'U'&lt;br /&gt;AND&lt;br /&gt;(    instrig &lt;&gt; 0&lt;br /&gt;OR&lt;br /&gt;    updtrig &lt;&gt; 0&lt;br /&gt;OR&lt;br /&gt;    deltrig &lt;&gt; 0&lt;br /&gt;)&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-2882178071942467993?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/2882178071942467993/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=2882178071942467993' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/2882178071942467993'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/2882178071942467993'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2007/04/has-table-trigger.html' title='Which tables have a trigger/s?'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-3871478564768860669</id><published>2007-04-22T02:48:00.000-07:00</published><updated>2007-04-22T02:55:28.994-07:00</updated><title type='text'>What's the version of SQL Server?</title><content type='html'>I have seen this question (see post's subject) so many times in newsgroup asking people, so I decided to post some usefull info about SQL Server by using SERVERPROPERTY command. &lt;br /&gt; &lt;br /&gt;SELECT @@VERSION AS [Server Information]&lt;br /&gt;&lt;br /&gt;SELECT SERVERPROPERTY('ServerName') AS [ServerName]&lt;br /&gt;SELECT SERVERPROPERTY('MachineName') AS [MachineName]&lt;br /&gt;SELECT SERVERPROPERTY('InstanceName') AS [InstanceName]&lt;br /&gt;&lt;br /&gt;SELECT SERVERPROPERTY('productversion') AS [ProductVersion]&lt;br /&gt;SELECT SERVERPROPERTY('ProductLevel') AS [ProductLevel]&lt;br /&gt;SELECT SERVERPROPERTY('Edition') AS [Edition]&lt;br /&gt;&lt;br /&gt;SELECT SERVERPROPERTY('Collation') AS [Collation]&lt;br /&gt;SELECT SERVERPROPERTY('IsClustered') AS [IsClustered]&lt;br /&gt;SELECT SERVERPROPERTY('IsFullTextInstalled') AS [IsFullTextInstalled]&lt;br /&gt;SELECT SERVERPROPERTY('IsIntegratedSecurityOnly') AS &lt;br /&gt;[IsIntegratedSecurityOnly]&lt;br /&gt;SELECT SERVERPROPERTY('LicenseType') AS [LicenseType]&lt;br /&gt;SELECT SERVERPROPERTY('NumLicenses') AS [NumLicenses]&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-3871478564768860669?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/3871478564768860669/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=3871478564768860669' title='32 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/3871478564768860669'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/3871478564768860669'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2007/04/whats-version-of-sql-server.html' title='What&apos;s the version of SQL Server?'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>32</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-4271797404389444367</id><published>2007-04-19T03:44:00.000-07:00</published><updated>2007-04-22T04:50:29.931-07:00</updated><title type='text'>Mixed or Windows Authentication Only</title><content type='html'>Ok, at this time I would to discuss about the one but realy painful issue we have to face where we use SQL Authentication (Mixed).&lt;br /&gt;Consider  the following situation. You are using SQL Server (I speak for SQL Server 2000 SP4) Authentication(Mixed). &lt;br /&gt;You also have a database and user within which is mapped to the SQL login. Now , you have been told to move the database on the new server. So you created a backup of the database , copied it to the new server as well as create a SQL Login with the same name as you had on the old one. Ok, now it is time to perform restore database, so everything went just fine but when theh users tried to connnect to the database (through application) on the new server they get the error. What happened? The thing is that SQL Server is 'losing' SIDs that mapped between a login and user when you created a new SQL Login on the new server (master..syslogins) and a database user (dbname..sysuser)that you brought within a database. So we have sp_change_users_login system stored procedure that takes care of re-mapping SID's.&lt;br /&gt;&lt;br /&gt;However, that DOES NOT happen if you use Windows Authentication Only.That means SQL Server keeps SID's realtionship between Windows Login and User database although you move the database on the new server.&lt;br /&gt;&lt;br /&gt;Just one more argument to use Windows Authentication Only.&lt;br /&gt;&lt;br /&gt;In SQL Server 2005 SP2 MS has introduced some greate features on subject.&lt;br /&gt;http://blogs.msdn.com/lcris/archive/2007/02/19/sql-server-2005-some-new-security-features-in-sp2.aspx.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-4271797404389444367?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/4271797404389444367/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=4271797404389444367' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/4271797404389444367'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/4271797404389444367'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2007/04/sql-or-windows-authentication.html' title='Mixed or Windows Authentication Only'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-3534868239580399154</id><published>2007-04-15T02:13:00.000-07:00</published><updated>2007-04-15T02:16:31.900-07:00</updated><title type='text'>Drop all indexes</title><content type='html'>Well, sometimes we have to do that. A large INSERT statement may run faster  for instance. The below script just generate DROP command of all indexes in current dataabse. You can add a filter to specify a table name for example.(SQL Server 2000)&lt;br /&gt;&lt;br /&gt;SELECT&lt;br /&gt;    'DROP INDEX ' +&lt;br /&gt;    QUOTENAME(USER_NAME(o.uid)) +&lt;br /&gt;    '.' +&lt;br /&gt;    QUOTENAME(o.name) +&lt;br /&gt;    '.' +&lt;br /&gt;    QUOTENAME(i.name)&lt;br /&gt;FROM sysobjects o&lt;br /&gt;JOIN sysindexes i ON&lt;br /&gt;    i.id = o.id&lt;br /&gt;WHERE i.indid BETWEEN 1 AND 254 AND&lt;br /&gt;    INDEXPROPERTY(i.id, i.name, 'IsStatistics') = 0 AND&lt;br /&gt;    INDEXPROPERTY(i.id, i.name, 'IsHypothetical') = 0 AND&lt;br /&gt;    OBJECTPROPERTY(o.id, 'IsMSShipped') = 0&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-3534868239580399154?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/3534868239580399154/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=3534868239580399154' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/3534868239580399154'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/3534868239580399154'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2007/04/drop-all-indexes.html' title='Drop all 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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-15099950690239530</id><published>2007-04-10T00:54:00.000-07:00</published><updated>2007-04-10T03:51:43.150-07:00</updated><title type='text'>Some third parties break DIFFERENTIAL backup chains</title><content type='html'>I'd like to share some things I have seen on  my client's machine. He has a full backup database set up once a night and every four hours differential back during the work day. But at the same time ( it should be done at night) the network administrator has ran tird party software backup to copy .MDF/LDF files. When we started to restore database  it throws the error that SQL Server cannot apply this bakup file to the full backup file created at night. We did lots of testing on the developing machine and did not het the error. And only when we asked the net.admin not to run this software then we were able to restore the database succefully. &lt;br /&gt;&lt;br /&gt;Note, I have not seen that doing LOG files backup does break the chains.&lt;br /&gt;&lt;br /&gt;So please do lots of testing before you find your self without properly created disaster recovery strategy.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;I found that MS has this KB explaining the behaviour.&lt;br /&gt;http://support.microsoft.com/kb/903643.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-15099950690239530?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/15099950690239530/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=15099950690239530' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/15099950690239530'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/15099950690239530'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2007/04/third-party-software-breaks.html' title='Some third parties break DIFFERENTIAL backup chains'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-7103800998796873524</id><published>2007-03-26T05:23:00.001-07:00</published><updated>2007-04-10T01:10:21.657-07:00</updated><title type='text'>Output clause and Auditing of data</title><content type='html'>There is very common scenario to auditing of data in a table using triggers. In this case, the trigger uses information from the inserted and updated tables to add rows into the audit tables. MS has introduced a new OUTPUT clause in SQL Server 2005 to audit data instead of using triggers to insert rows into an audit table.&lt;br /&gt; &lt;br /&gt;CREATE TABLE test (col INT NOT NULL);&lt;br /&gt;CREATE TABLE test_audit ( old_col INT NOT NULL, new_col INT NULL);&lt;br /&gt;INSERT INTO test  (col) values( 1 );&lt;br /&gt;INSERT INTO test  (col) values( 2 );&lt;br /&gt; &lt;br /&gt;UPDATE test &lt;br /&gt;   SET col = col + 1&lt;br /&gt;OUTPUT deleted.col ,inserted.col  into test_audit&lt;br /&gt; WHERE col = 1;&lt;br /&gt; &lt;br /&gt;DELETE FROM test&lt;br /&gt;OUTPUT deleted.col, NULL into test_audit&lt;br /&gt; WHERE col = 2;&lt;br /&gt; &lt;br /&gt;SELECT * FROM test&lt;br /&gt;SELECT * FROM test_audit;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-7103800998796873524?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/7103800998796873524/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=7103800998796873524' title='3 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/7103800998796873524'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/7103800998796873524'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2007/03/output-clause-and-auditing-of-data_26.html' title='Output clause and Auditing of data'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>3</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-3689344831340035774</id><published>2007-03-21T01:04:00.000-07:00</published><updated>2007-05-20T23:33:26.831-07:00</updated><title type='text'>Detaching and Attaching database</title><content type='html'>At this time I'd like to share some scripts that used undocumented (sorry) but very popular among DBA's sp_MSforeachdb stored procedure. If you have hundred of databases on your server and you was told to move those databases to another server the quick way to do the job  is performing the following scripts. First of all we detach all user databases from the server and the second query will output an attach &lt;br /&gt;script. The  thing is that the second query will output the script  that attach the database that has more than one LOG file.&lt;br /&gt;&lt;br /&gt;I'm sure that you have backuped the user databases before performing such tasks.&lt;br /&gt;&lt;br /&gt;--Detaching&lt;br /&gt;EXEC sp_MSforeachdb&lt;br /&gt;'IF N''?'' NOT IN(''model'' ,''master'', ''msdb'', ''tempdb'')&lt;br /&gt;   BEGIN&lt;br /&gt;SELECT ''EXEC sp_detach_db ''''?'''',''''true''''''&lt;br /&gt;    END'&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;--Attaching&lt;br /&gt;EXEC sp_msforeachdb&lt;br /&gt;    'SELECT ''EXEC sp_attach_db ''''?'''',&lt;br /&gt;    '''''' + RTRIM(filename) + '''''',''&lt;br /&gt;    FROM ?..sysfiles&lt;br /&gt;    WHERE fileid = (SELECT MIN(fileid) FROM ?..sysfiles)&lt;br /&gt;    UNION ALL&lt;br /&gt;    SELECT '''''''' + RTRIM(filename) + ''''''''&lt;br /&gt;    FROM ?..sysfiles&lt;br /&gt;    WHERE fileid &gt; (SELECT MIN(fileid) FROM ?..sysfiles) AND&lt;br /&gt;     fileid &lt; (SELECT MAX(fileid) FROM ?..sysfiles)&lt;br /&gt;    UNION ALL&lt;br /&gt;    SELECT '''''''' + RTRIM(filename) + ''''''''&lt;br /&gt;    FROM ?..sysfiles&lt;br /&gt;    WHERE fileid = (SELECT MAX(fileid) FROM ?..sysfiles)'&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-3689344831340035774?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/3689344831340035774/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=3689344831340035774' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/3689344831340035774'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/3689344831340035774'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2007/03/detaching-and-attaching-database.html' title='Detaching and Attaching database'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-310749932891096354</id><published>2007-03-14T00:39:00.000-07:00</published><updated>2007-03-14T00:55:05.754-07:00</updated><title type='text'>String manipulation (searching)</title><content type='html'>Recently I have seen some post in the news group regarding seraching a string in the table or even part of string. Yes , there are lots of resources in the internet how to do that. I would like to shere some ideas also here.&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Consider the following structure. We need to find in col1 a part of string. Let us find occurence of '23'.&lt;br /&gt; &lt;br /&gt;CREATE TABLE #Test&lt;br /&gt;(&lt;br /&gt; col INT,&lt;br /&gt; col1 VARCHAR(100)&lt;br /&gt;)&lt;br /&gt;INSERT INTO #Test VALUES (1,'12,23,44,56')&lt;br /&gt;INSERT INTO #Test VALUES (2,'26,45,22,32')&lt;br /&gt;INSERT INTO #Test VALUES (3,'45,22,34,23')&lt;br /&gt;&lt;br /&gt;DECLARE @st VARCHAR(10)&lt;br /&gt;SET @st='23'&lt;br /&gt;SELECT *&lt;br /&gt;FROM #test&lt;br /&gt;WHERE ','+col1+',' LIKE '%,'+@st+',%'&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Now, take a look at following table&lt;br /&gt;CREATE TABLE #Test (contact VARCHAR(20) PRIMARY KEY)&lt;br /&gt;&lt;br /&gt;INSERT INTO #Test VALUES ('Bill Clinton')&lt;br /&gt;INSERT INTO #Test VALUES ('George W. Bush')&lt;br /&gt;INSERT INTO #Test VALUES ('John F Kennedy')&lt;br /&gt;INSERT INTO #Test VALUES ('Smith')&lt;br /&gt;&lt;br /&gt;1)&lt;br /&gt;SELECT contact&lt;br /&gt; FROM #Test &lt;br /&gt; WHERE contact LIKE '%[A-Z] [A-Z][^A-Z]%[A-Z]'&lt;br /&gt;----&lt;br /&gt;2)&lt;br /&gt;SELECT contact&lt;br /&gt; FROM #Test &lt;br /&gt; WHERE contact LIKE '% _ %' or contact LIKE '% _. %'&lt;br /&gt;&lt;br /&gt;The first one returns all people that have a middle name. The second one returns&lt;br /&gt;people who has not '.' in this name after middle name.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-310749932891096354?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/310749932891096354/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=310749932891096354' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/310749932891096354'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/310749932891096354'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2007/03/string-manipulation-searching.html' title='String manipulation (searching)'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-7755089252883040898</id><published>2007-03-11T05:05:00.000-07:00</published><updated>2007-03-11T05:17:37.911-07:00</updated><title type='text'>Script Objects SS2K</title><content type='html'>Well, there are still lots of people/businesses use SQL Server 2000 I'd like to put the following script using SQL DMO object library. You have to open a new project with VB6 and put the code in the new form.&lt;br /&gt;The cool thing is that you get the script without default COLLATION to be specify.&lt;br /&gt;See if you need to run the script on another server/s or database/s that have different collation. &lt;br /&gt;&lt;br /&gt;Also, Tibor Karaszi has great info at his web site&lt;br /&gt;http://www.karaszi.com/SQLServer/info_generate_script.asp&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Sub ScriptDB(strLogin As String, strPwd As String, _&lt;br /&gt;             strDataBase As String, StrFilePath As String)&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;   Dim sql As Object&lt;br /&gt;   Dim db As Object&lt;br /&gt;   Dim objTrigger As Object&lt;br /&gt;   Dim intOptions As Long&lt;br /&gt;   Dim genObj&lt;br /&gt;   &lt;br /&gt;   Set sql = CreateObject("SQLDMO.SQLServer")&lt;br /&gt;   Set db = CreateObject("SQLDMO.Database")&lt;br /&gt;   Set objTrigger = CreateObject("SQLDMO.Trigger")&lt;br /&gt;    &lt;br /&gt;   Const sDrops As Integer = 1&lt;br /&gt;   Const sIncludeHeaders As Long = 131072&lt;br /&gt;   Const sDefault As Integer = 4&lt;br /&gt;   Const sAppendToFile As Integer = 256&lt;br /&gt;   Const sBindings As Integer = 128&lt;br /&gt;   ' No collation to be specify&lt;br /&gt;   Const SQLDMOScript2_NoCollation As Long = 8388608&lt;br /&gt;    &lt;br /&gt;   ' We need to specify multiple behaviors&lt;br /&gt;   ' for the ScriptType argument, you use "Or" to combine these.&lt;br /&gt;   intOptions = sDrops Or sIncludeHeaders Or _&lt;br /&gt;   sDefault Or sAppendToFile Or sBindings Or SQLDMOScript2_NoCollation&lt;br /&gt;    &lt;br /&gt;   ' Connect to server &lt;br /&gt;   sql.Connect "(local)", strLogin, strPwd&lt;br /&gt;   Set db = sql.Databases(strDataBase, "dbo")&lt;br /&gt;    &lt;br /&gt;   ' Script User Defined Data Types&lt;br /&gt;   For Each genObj In db.UserDefinedDatatypes&lt;br /&gt;      genObj.Script intOptions, StrFilePath&lt;br /&gt;   Next&lt;br /&gt;    &lt;br /&gt;   ' Script Tables and Triggers, ignoring system&lt;br /&gt;   ' tables and system generated triggers&lt;br /&gt;   For Each genObj In db.Tables&lt;br /&gt;      If genObj.SystemObject = False Then&lt;br /&gt;         genObj.Script intOptions, StrFilePath,,SQLDMOScript2_NoCollation&lt;br /&gt;         For Each objTrigger In genObj.Triggers&lt;br /&gt;            If objTrigger.SystemObject = False Then&lt;br /&gt;               objTrigger.Script intOptions, StrFilePath&lt;br /&gt;            End If&lt;br /&gt;         Next&lt;br /&gt;      End If&lt;br /&gt;   Next&lt;br /&gt;        &lt;br /&gt;   ' Script Rules &lt;br /&gt;   For Each genObj In db.Rules&lt;br /&gt;      genObj.Script intOptions, StrFilePath&lt;br /&gt;   Next&lt;br /&gt;    &lt;br /&gt;   ' Script Defaults&lt;br /&gt;   For Each genObj In db.Defaults&lt;br /&gt;      genObj.Script intOptions, StrFilePath&lt;br /&gt;   Next&lt;br /&gt;    &lt;br /&gt;   ' Script Sprocs, ignoring system sprocs&lt;br /&gt;   For Each genObj In db.StoredProcedures&lt;br /&gt;      If genObj.SystemObject = False Then&lt;br /&gt;         genObj.Script intOptions, StrFilePath&lt;br /&gt;      End If&lt;br /&gt;   Next&lt;br /&gt;        &lt;br /&gt;   ' Script Views, ignoring system views and informational schemas&lt;br /&gt;   For Each genObj In db.Views&lt;br /&gt;      If genObj.SystemObject = False Then&lt;br /&gt;         genObj.Script intOptions, StrFilePath&lt;br /&gt;      End If&lt;br /&gt;   Next&lt;br /&gt;            &lt;br /&gt;      &lt;br /&gt;End Sub&lt;br /&gt;&lt;br /&gt;To call the procedure save the module and supply the parameters.   &lt;br /&gt;Call ScriptDB("UserName","Password","DatabaseName","C:\DBScript.SQL")&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-7755089252883040898?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/7755089252883040898/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=7755089252883040898' title='4 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/7755089252883040898'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/7755089252883040898'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2007/03/script-objects.html' title='Script Objects SS2K'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>4</thr:total></entry><entry><id>tag:blogger.com,1999:blog-5334232245085456076.post-9204175322687995131</id><published>2007-03-11T04:58:00.000-07:00</published><updated>2007-03-11T08:11:26.847-07:00</updated><title type='text'>Getting the value of Constraint</title><content type='html'>Recently I was asked by my colleague to return tha actual value of CHECK constaint that he has defined on the table.&lt;br /&gt;I think it can be usefull for some reports that we've done  on the database.&lt;br /&gt;Tested on SQL Server 2000(SP3a) and SQL Server 2005(SP2a)&lt;br /&gt;&lt;br /&gt;CREATE TABLE Test&lt;br /&gt;(&lt;br /&gt; col INT,&lt;br /&gt; coldatetime DATETIME&lt;br /&gt;)&lt;br /&gt;&lt;br /&gt;ALTER  TABLE  Test&lt;br /&gt;ADD CONSTRAINT coldatetime_check&lt;br /&gt;CHECK ( coldatetime &gt; '20070101')&lt;br /&gt;&lt;br /&gt;SELECT  text FROM  syscomments&lt;br /&gt;WHERE  id =OBJECT_ID('coldatetime_check')&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/5334232245085456076-9204175322687995131?l=dimantdatabasesolutions.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://dimantdatabasesolutions.blogspot.com/feeds/9204175322687995131/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=5334232245085456076&amp;postID=9204175322687995131' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/9204175322687995131'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/5334232245085456076/posts/default/9204175322687995131'/><link rel='alternate' type='text/html' href='http://dimantdatabasesolutions.blogspot.com/2007/03/getting-value-of-constraint.html' title='Getting the value of Constraint'/><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='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry></feed>
