Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2006
    Posts
    38

    Unanswered: ORDER BY Can be used in view?

    In MSSql,"Order by" and "distinct" can be used in view?

    thanks!

  2. #2
    Join Date
    Jun 2003
    Posts
    269

    Thumbs up

    u can use 'order by' and 'distinct' in view.u have to use 'top ' key word when use order by clause.See below example,
    Code:
    use pubs
    go
    create view vauthors
    as
     select  distinct top 100 percent city from authors
    order by city
    I love cursor,though I never use it.Cos' I dont want to misuse it.
    ----------------------------------------------

    ----cheers
    ----http://mallier.blogspot.com

  3. #3
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    47
    No. It does not make sense to do order by in the view as a view is just a partion of a table(s). Perform the ordering you required when you select from the view.

    Select *
    from YourView
    Order By your_view_column
    -----------------
    KH


  4. #4
    Join Date
    Feb 2006
    Posts
    38
    I remember that the following SQL is wrong:
    Code:
    create view vauthors
    as
     select  distinct top 100 percent city from authors
    order by city
    Now in MSSQL,it can be run?
    it mustn't include the order by and distinct in view,now it can be?

  5. #5
    Join Date
    Jun 2003
    Posts
    269
    what khtan said is right. u should order by when u select from view.though it is possible that 'order by' clause can be used in view,which u asked
    I love cursor,though I never use it.Cos' I dont want to misuse it.
    ----------------------------------------------

    ----cheers
    ----http://mallier.blogspot.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •