Tuesday, July 15, 2008

Sorted view in SQL 2005

It looks like something new in SQL 2005. If you try to add a "order by" in the view, the management studio will automatically add a "TOP (100) PERCENT" after the SELECT clause. However this only works in "preview" mode. If you query "select * from [view_name]", you still get the records unsorted.

To get the sorted records from a view, a workaround is to use
SELECT top 1000000000 * FROM

This is not ideal but it works if you know your data count range. Microsoft has a hotfix for this. I can't believe MS let SQL 2005 out of the door with such a bug.

No comments: