• 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)).