October 2006 - Posts

When is the MAX value not the TOP value

My blog has moved to sqlblogcasts.com/blogs/simons
The full version of this post can be found here., http://sqlblogcasts.com/blogs/simons/When is the MAX value not the TOP value
The new RSS feed is here

Have a look and try the following statements, and try and think what the expected results are. The key aspect is that the query does not match any rows, no object exists with id = -99
 
declare @maxValue datetime
set @maxValue = '1/1/1900'
select @maxvalue = max(crdate) from sysobjects where id = -99
select 'Using max' ,@maxvalue maxValue
go
declare @maxValue datetime
set @maxValue = '1/1/1900'
select top 1 @maxValue = crdate from sysobjects where id = -99 order by crdate desc
select 'Using top' ,@maxvalue maxValue
go
 
The results are as follows,
 
Test      maxValue
--------- -----------------------
Using max NULL
 
Test          maxValue
--------- -----------------------
Using top 1900-01-01 00:00:00.000
 
There are no rows returned so in the first instance @maxValue will NULL, however in the second the @maxValue will still be the default value
SELECT only assigns a value if a row is returned. With MAX a row is returned, no rows matching the criteria result in a row with a NULLvalue, however with TOP there are no rows so it can't return a row.
 
Depending on the behaviour you want you can achieve the NULL value with TOP if you use SET. This ensures you have consistency.
 
With SET if your subquery returns no rows then a NULL value will be assigned to your variable. i.e.
 
declare @maxValue datetime
set @maxValue = '1/1/1900'
SET @maxvalue = (select top 1 crdate from sysobjects where id = -99 order by crdate desc)
select 'Using top with SET' ,@maxvalue maxValue
go
 
Test               maxValue
------------------ -----------------------
Using top with SET NULL

Tony has a post on the T-SQL Value assignment SET vs SELECT that deals with the other side of the situation when you have multiple rows in a sub query.

... To read the complete version of this post go here, http://sqlblogcasts.com/blogs/simons/When is the MAX value not the TOP value

SQL 2005 Upgrade - Rebuild your indexes

My blog has moved to sqlblogcasts.com/blogs/simons
The full version of this post can be found here., http://sqlblogcasts.com/blogs/simons/SQL 2005 Upgrade - Rebuild your indexes
The new RSS feed is here

I can't say how many times I've read or been told that before you complain about performance issues with SQL2005 you should rebuild your indexes and update your statistics.

Well we've just upgraded our search boxes and was having performance problems, timeouts galore. The reason was the plan being produced was far from optimal. It was estimating it would get 1 row from a very large table, which meant the nested loops it was doing made sense. However the estimation was way off, in fact it was getting ~8,000 rows. This meant the nested loops weren't very effiecient. Even after 90s of running the query hadn't finished and performed more than 20 million reads. (on 2000 it was doing 100,000, this is a complex search involving full text).

... To read the complete version of this post go here, http://sqlblogcasts.com/blogs/simons/SQL 2005 Upgrade - Rebuild your indexes

Some new version of a common browser has been released today

My blog has moved to sqlblogcasts.com/blogs/simons
The full version of this post can be found here., http://sqlblogcasts.com/blogs/simons/Some new version of a common browser has been released today
The new RSS feed is here

Got to jump on the band wagon and tell the world what they already know.

IE7 has been RTM'd

www.microsoft.com/windows/ie

... To read the complete version of this post go here, http://sqlblogcasts.com/blogs/simons/Some new version of a common browser has been released today

CREATE or REPLACE feature request

My blog has moved to sqlblogcasts.com/blogs/simons
The full version of this post can be found here., http://sqlblogcasts.com/blogs/simons/CREATE or REPLACE feature request
The new RSS feed is here

I remember back when I moved from Oracle to 6.5 I found the lack of a create or replace really annoying. Whenever I create new sps I always curse at not being able to do "create or replace myproc..." having to do "if exists (select 1 information_schema...)" or "if object_id(...) is not null" or "if not exists(select 1 from sys.objects..)" is really annoying, especially as there are so many different options.

... To read the complete version of this post go here, http://sqlblogcasts.com/blogs/simons/CREATE or REPLACE feature request

SB - Its expensive to start a conversation

My blog has moved to sqlblogcasts.com/blogs/simons
The full version of this post can be found here., http://sqlblogcasts.com/blogs/simons/SB - Its expensive to start a conversation
The new RSS feed is here

I've just mentioned Remus's post on service broker procedures. The other thing that came up when looking at the conversation group size was the time it took to load the queue. This set of figures shows the load time, the first is the number of conversations and the second the number of messages per conversation, and the final number, the time it took in seconds.

... To read the complete version of this post go here, http://sqlblogcasts.com/blogs/simons/SB - Its expensive to start a conversation

Asynchronous processing - SQL Server Usergroup meeting Tuesday 24th October

My blog has moved to sqlblogcasts.com/blogs/simons
The full version of this post can be found here., http://sqlblogcasts.com/blogs/simons/Asynchronous processing - SQL Server Usergroup meeting Tuesday 24th October
The new RSS feed is here

I will be presenting at the next UK SQL Server Usergroup meeting, at Microsofts Reading campus.

My topic is Asynchronous processing in SQL Server. This will include some techniques for SQL Server 2000 and more from SQL Server 2005

... To read the complete version of this post go here, http://sqlblogcasts.com/blogs/simons/Asynchronous processing - SQL Server Usergroup meeting Tuesday 24th October

TSQL turning into C#

My blog has moved to sqlblogcasts.com/blogs/simons
The full version of this post can be found here., http://sqlblogcasts.com/blogs/simons/TSQL turning into C#
The new RSS feed is here

Following my previous post about the requirement of semi-colons when using service broker statements, I have raised a suggestion to improve the "Incorrect syntax near ..." error message.

... To read the complete version of this post go here, http://sqlblogcasts.com/blogs/simons/TSQL turning into C#

Native Command Substitution is not supported

My blog has moved to sqlblogcasts.com/blogs/simons
The full version of this post can be found here., http://sqlblogcasts.com/blogs/simons/Native Command Substitution is not supported
The new RSS feed is here

In response to a recent support incident we raised we got a response about use of detours in SQL Server.

A detour is a means by which you can change what SQL Server does. This is used by Litespeed to change native backup commands into Litespeed backup commands. Whilst I always thought this a neat idea I always felt it a bit risky. If I want to do a litespeed backup I can easily write the code or use the UI to do so.

... To read the complete version of this post go here, http://sqlblogcasts.com/blogs/simons/Native Command Substitution is not supported