October 19, 2004 at 11:49 am
So the question really is, is this a known bug or is this by design? (and if by design can you explain?)
That computed sorts don't work with concatenation
declare @a varchar(1000)
select @a = ISNULL( @a, '' ) + CONVERT(VARCHAR(10),id ) + ','
FROM sysobjects
ORDER BY id %10
SELECT @a
GO
declare @a varchar(1000)
select @a = ISNULL( @a, '' ) + CONVERT(VARCHAR(10),id ) + ','
FROM (
SELECT TOP 100 PERCENT id
FROM sysobjects
ORDER BY id %10
) w
SELECT @a
October 19, 2004 at 2:35 pm
I think in this particular instance that it is by design. The reason I think this is you are performing a computed calculation but to be able to perform that the server needs to know the data to apply the calculation to.
The example you provide pulls back the sysobjects table in memory (or cache or any other term that you feel comfortable with) and then the server knows here is the data to work with. The other way the server says "I know you want to use x but out of x what data do you want?"
I may be wrong or over simplifying things here and I am sure that any of the great SQL gurus that are here can point us both in the right direction.
The above is just what I feel happens based off the result that I see from running the code.
Good Hunting!
AJ Ahrens
webmaster@kritter.net
October 20, 2004 at 12:48 am
It's a known problem:
http://support.microsoft.com/?kbid=287515
Indeed, aggregate concatenation doesn't work with order by an expression.
The first workarouds that I've tried do not work:
- subqueries
- views
I have found only two working workarounds:
- a computed column in the base table
- temporary tables or table variables
Razvan
PS. Anyway, aggregate concatenation itself is not ANSI SQL and is not documented as a feature of MS SQL, either (as far as I know). So it's just a feature that works... by chance. I have to recognize that I also use it, but I try to use it only on base tables (because of this problem).
October 20, 2004 at 9:56 am
I've also noticed that an ORDER BY list with many columns also causes aggregate concatenation to be unordered. When I reduced the number of columns in the ORDER BY, aggregate concatenation order worked fine.
October 25, 2004 at 3:06 pm
October 26, 2004 at 2:15 am
The requirement to state 'order by' if you want to guarantee a certain order in the result has always been there - it has not been added by MS.
Only bozos who knew that the former SQL Server implementation(s) had a 'feature' that most of the times allowed you to get away without using 'order by' did so.
How rows are physically stored has nothing to do with in which order rows found by a query is returned.... unless you say ORDER BY
It's always been this way, this is nothing new....
/Kenneth
October 26, 2004 at 4:27 am
How rows are physically stored has nothing to do with in which order rows found by a query is returned.... unless you say ORDER BY
Actually this has always been untrue, the fact is the storage order does affect the order of output but so do non-clustered indexes if one is used, choice of cursor thru index structure, and a lot of other conditions including things that happen in the cache factor into how the data is returned. ORDER BY is the only way to ensure you get the expected sort for the data.
October 26, 2004 at 6:39 am
Apologies for being unclear, my mind was in 'generic' SQL mode, not SQL Server specific
My intention was to say;
How rows are physically stored does not necessarily guarantee in which order rows found by a query is returned .... unless you say ORDER BY
The fact that many skips 'order by' is in itself proof that physical structures indeed influences the 'common' resultset - that wasn't my intended message.
If 'order by' isn't specified, rows will be returned in the order they are retrieved - this order may or may not be the same time after another. The only way to ensure expected ordering is to use 'order by' - nothing else. And this is certainly nothing new in SQL.
/Kenneth
October 26, 2004 at 8:58 am
Thanks for your change and hope I didn't come off rude.
Viewing 10 posts - 1 through 9 (of 9 total)
You must be logged in to reply to this topic. Login to reply