Results 1 to 7 of 7

Thread: view, union and order by

  1. #1
    Join Date
    Mar 2003
    Posts
    383

    view, union and order by

    I have a view with union 3 tables, how could I put order by within the view at the bottom?

    I also tried select 100 percent ..... , but order by with error...

    thanks
    David

  2. #2
    Join Date
    Sep 2002
    Posts
    5,938
    I think should order results when you query the view.

  3. #3
    Join Date
    Mar 2003
    Posts
    383
    H Rmiao:

    I think I found the solution:

    select top 100 percent * from (select ...union...select...) as x
    order by column1

    it works to break the barrier of sql2000 limitaion..

    thanks
    David

  4. #4
    Join Date
    Mar 2003
    Posts
    383
    Hi Ramio:

    select * from vw_ABC -->works fine with correct order by.
    but failed in run the application where 37 procs call this view with time-out.

    It looks that the procs call this view is kind of confusing about the select top 100 * from (select a, b, c from ..... union select x, y, z from ....) as X oder by A.

    -D

  5. #5
    Join Date
    Mar 2008
    Posts
    2
    The view don't accept the order by clausula, you can use it when you call the view

    Greetings,
    Santiago Carela
    Rep. Dom.

  6. #6
    Join Date
    Nov 2002
    Location
    New Jersey, USA
    Posts
    3,932
    ANSI standard prohibits ORDER BY in view definition, the workaround you have works but it may not be related to timeouts you have in application. The resultset may be too big for application to handle.

  7. #7
    Join Date
    Mar 2008
    Posts
    11

    ORDEr BY solution

    One thing you can try that doesn't require all that mucking about with TOP 100 PERCENT etc. etc. is to specify the column ordinals. Here's a sample:

    /*************************************/
    SELECT Col1, Col2, Col3, Col4, Col5
    FROM Table1
    UNION ALL
    SELECT Col1, Col2, Col3, Col4, Col5
    FROM Table2
    ORDER BY 3, 2, 5
    /*************************************/

    Hope this helps!

Posting Permissions

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