|
|
|
UDP Broadcaster
      
Group: General Forum Members
Last Login: Wednesday, May 15, 2013 11:30 AM
Points: 1,495,
Visits: 1,507
|
|
| Comments posted to this topic are about the item Out of order
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Yesterday @ 8:55 AM
Points: 5,099,
Visits: 20,190
|
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Yesterday @ 2:04 AM
Points: 1,968,
Visits: 1,819
|
|
CREATE VIEW vw_clients AS SELECT TOP 100 PERCENT Code, Name FROM Clients ORDER BY Code
I am curious to know if the VIEW above always returns records in sorted order.
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Yesterday @ 4:56 AM
Points: 1,256,
Visits: 4,253
|
|
Carlo Romagnano (10/21/2011)
CREATE VIEW vw_clients AS SELECT TOP 100 PERCENT Code, Name FROM Clients ORDER BY Code
I am curious to know if the VIEW above always returns records in sorted order.
No, it doesn't, unless you're still running SQL 2000. On more recent SQL versions the optimiser will see that the TOP 100 PERCENT and the ORDER BY are both redundant and will remove them. If you want the results of a view to be sorted, you have to include the ORDER BY in the SELECT statement you use to query the view.
|
|
|
|
|
Ten Centuries
      
Group: General Forum Members
Last Login: Yesterday @ 4:24 AM
Points: 1,158,
Visits: 642
|
|
|
|
|
|
Hall of Fame
       
Group: General Forum Members
Last Login: Yesterday @ 1:49 AM
Points: 3,123,
Visits: 4,310
|
|
Good question... even then, the sort order of any select from the view is not guaranteed....
____________________________________________ Space, the final frontier? not any more... All limits henceforth are self-imposed. “libera tute vulgaris ex”
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Yesterday @ 2:04 AM
Points: 1,968,
Visits: 1,819
|
|
paul.knibbs (10/21/2011)
Carlo Romagnano (10/21/2011)
CREATE VIEW vw_clients AS SELECT TOP 100 PERCENT Code, Name FROM Clients ORDER BY Code
I am curious to know if the VIEW above always returns records in sorted order. No, it doesn't, unless you're still running SQL 2000. On more recent SQL versions the optimiser will see that the TOP 100 PERCENT and the ORDER BY are both redundant and will remove them. If you want the results of a view to be sorted, you have to include the ORDER BY in the SELECT statement you use to query the view. Thanks. In fact, in sql2000 the optimizer use an index scan (create index idx_client_code on Clients(Code)).
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Yesterday @ 8:39 AM
Points: 1,602,
Visits: 1,084
|
|
Well, I'm glad I looked at these posts. I knew you could order a view if you did the SELECT TOP (100) PERCENT thing but I hadn't realised that it didn't work! So what looked like an easy question has become an opportunity to learn something new.
|
|
|
|
|
SSCommitted
      
Group: General Forum Members
Last Login: Yesterday @ 2:04 AM
Points: 1,968,
Visits: 1,819
|
|
Stewart "Arturius" Campbell (10/21/2011) Good question... even then, the sort order of any select from the view is not guaranteed.... In fact, the TOP clause with the "ORDER BY", in this case, is just a filter and not a command to sort rows.
|
|
|
|
|
SSC Eights!
      
Group: General Forum Members
Last Login: Yesterday @ 2:20 AM
Points: 841,
Visits: 317
|
|
|
|
|