ORDER BY in a cursor

  • Selecting into a temp table first is not a solution. When your cursor selects the data back out of the temp table, there's no guarantee it will be in the same order. Sort Order when SELECT FROM Table Variables Unless that's untrue, you should take that suggestion down since many may not read past it.

  • Good morning,

    Just to ADD, I saw that adding

    DECLARE x_cursor CURSOR STATIC READ_ONLY FOR ...

    Solves the issue

    best regards 🙂

  • rjuarez71 (11/24/2015)


    Good morning,

    Just to ADD, I saw that adding

    DECLARE x_cursor CURSOR STATIC READ_ONLY FOR ...

    Solves the issue

    best regards 🙂

    From MSDN,

    "STATIC

    Defines a cursor that makes a temporary copy of the data to be used by the cursor. All requests to the cursor are answered from this temporary table in tempdb; therefore, modifications made to base tables are not reflected in the data returned by fetches made to this cursor, and this cursor does not allow modifications."

    That may not be what the user originally wanted. The goal is to display the results of the fetches in the order indicated in the SELECT. I played with the original solution provided and it works fine on my end.

    ----------------------------------------------------

Viewing 3 posts - 16 through 17 (of 17 total)

You must be logged in to reply to this topic. Login to reply