Result set order - Union All

  • Hi All,

    I have a question about the defualt order of values in the result set when using "Union ALL" between two tables.In the example below,

    will the table1 values always appear first in the final result set followed by the values from table2?

    Please refer to the example the exmaple below:

    Select employee,employeename from table1

    union all

    Select employee,employeename from table2

    The reason I am asking this is, in my ssrs report I always want to display the employeename

    from table1 in the group header which is grouped by employeeid.

  • There is no default order to any result set, unless you have an ORDER BY clause.

  • Sort order must be explicitly defined in the ORDER BY clause.

    One way to accomplish what you're after could be this:

    SELECT *

    FROM (

    Select 'table1' as SRC, employee,employeename from table1

    union all

    Select 'table2' as SRC, employee,employeename from table2

    ) AS data

    ORDER BY SRC, employee, employeename

    Hope this helps

    Gianluca

    -- Gianluca Sartori

  • Ok, so why not just use the order by in SSRS??

Viewing 4 posts - 1 through 3 (of 3 total)

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