Login or Sign Up to become a member!
LessThanDot Sit Logo

LessThanDot

Data Management

Less Than Dot is a community of passionate IT professionals and enthusiasts dedicated to sharing technical knowledge, experience, and assistance. Inside you will find reference materials, interesting technical discussions, and expert tips and commentary. Once you register for an account you will have immediate access to the forums and all past articles and commentaries.

LTD Social Sitings

Lessthandot twitter Lessthandot Linkedin Lessthandot friendfeed Lessthandot facebook Lessthandot rss

Note: Watch for social icons on posts by your favorite authors to follow their postings on these and other social sites.

Your profile

    Search

    XML Feeds

    Google Ads

    « Finding Fragmentation Of An Index And Fixing ItSQL Server 2005 Service Pack 3 - Beta Available For Download »
    comments

    I saw that some people are hitting our site with a search for how to create a sorted view in SQL Server 2008.

    You all know that in SQL Server 2000 you can create a view and use TOP 100 PERCENT with ORDER By and it will be sorted. Since SQL server 2005 that doesn’t work anymore. I actually never understood the need for sorted views to begin with, how hard is it to do something like this

    1. SELECT *
    2. FROM View
    3. ORDER By Column

    Not hard, I guess pople want the convenience of opening the view in SSMS and it is sorted ‘correctly’
    There is a way to get this to work in SQL server 2005, there is a hotfix that will ‘fix’ this but you have to run in 2000 compatability mode.
    The link to the fix is here: FIX: When you query through a view that uses the ORDER BY clause in SQL Server 2008, the result is still returned in random order

    Now let’s get started with the code
    Create this table

    1. create table TestSort (id int not null)
    2. insert TestSort values(1)
    3. insert TestSort values(3)
    4. insert TestSort values(4)
    5. insert TestSort values(5)
    6. insert TestSort values(2)
    7. insert TestSort values(7)
    8. insert TestSort values(9)
    9. insert TestSort values(6)

    And create the view

    1. create view vTestSort
    2. as
    3. select top 100 percent id from TestSort
    4. order by id

    Now do a select from the view

    1. select * from vTestSort

    (result set)
    1
    3
    4
    5
    2
    7
    9
    6

    Oops it is not sorted
    Let’s try something else, we will use 99.99 percent

    1. create view vTestSort2
    2. as
    3. select top 99.99 percent  id from TestSort
    4. order by id

    Run the select against the view

    1. select * from vTestSort2

    (result set)
    1
    2
    3
    4
    5
    6
    7
    9

    look at that, magic! It works

    Let’s try another way by using the max value of an integer

    1. create view vTestSort3
    2. as
    3. select top 2147483648 id from TestSort
    4. order by id

    Run the select against the view

    1. select * from vTestSort3

    (result set)
    1
    2
    3
    4
    5
    6
    7
    9

    And bingo, it also works.

    Now, just because this works right now it doesn’t mean that it will work after you apply the next hotfix or service pack. Why not doing this instead

    1. select * from vTestSort3
    2. order by id

    That will always work and you don’t have to deal with unexpected results down the road

    About the Author

    User bio imageDenis has been working with SQL Server since version 6.5. Although he worked as an ASP/JSP/ColdFusion developer before the dot com bust, he has been working exclusively as a database developer/architect since 2002. In addition to English, Denis is also fluent in Croatian and Dutch, but he can curse in many other languages and dialects (just ask the SQL optimizer) He lives in Princeton, NJ with his wife and three kids.
    Social SitingsTwitterFacebookLinkedInHomePageFlickrLTD RSS Feed
    10329 views
    Instapaper

    3 comments

    Comment from: Alberto Neri [Visitor]
    Alberto Neri Hi:

    You comment:
    "I actually never understood the need for sorted views to begin with, how hard is it to do something like this..."

    But in fact Microsoft recommends Views because them are COMPILED and so the performance are increased. For example, you must tie several tables for accomplish just one result, if you query, and query and query each time this is poor programing technique, with VIEWS SQL automatically updates the "virtual" table and the performance is improved.

    Thanks for you comment I confirms is a bug in Microsoft's SQL 2005-2008

    Bye
    01/05/09 @ 12:45
    Comment from: SQLDenis [Member] Email
    SQLDenis >>with VIEWS SQL automatically updates the "virtual" table and the performance is improved.

    Alberto, are you sure you are not talking about Indexed Views here? A view (not indexed) is nothing but a stored query
    01/05/09 @ 13:05
    Comment from: Kumar Thams [Visitor]
    Kumar Thams Great Articles!! You elaborate it very nice and understandable. Here some good articles also helped me in completing my task. Check this helpful link too....
    http://msdn.microsoft.com/en-us/library/ms187956.aspx
    http://www.dbtalks.com/UploadFile/rohatash123/519/
    http://www.codeproject.com/Articles/38560/Overview-of-View-in-SQL-Server-2005
    03/26/12 @ 04:14

    Leave a comment


    Your email address will not be revealed on this site.

    Your URL will be displayed.
    (Line breaks become <br />)
    (Name, email & website)
    (Allow users to contact you through a message form (your email will not be revealed.)