Using UNION on results not on a SELECT statament

  • Hi everybody.

    I need to create a union result for the following case.

    I have 2 or more procedures returning the same data fields with diferent conditions, calculations etc.

    How could I join, I mean, unite them (just like a UNION clause on a SELECT statement)

    Example:

    select fieldA, fieldB, fieldC from mytable where fieldA>10

    UNION

    select fieldA, fieldB, fieldC from temptable where Fielddatetime > last_week_date

    I need something like this

    exec procedure1

    UNION

    exec procedure2

    I cannot use a SELECT UNION because there many calculations on the Stored Procedure code and at the end each proc will return a "1 row result set".

    How should I do this?

    Is it the right way or there is another simple solution?

    Thanks

  • You can create a temp table or a variable table and insert into the temp table the values you get from your SP's

    DECLARE @TempTab TABLE

    ( lastName1VarChar(50),

    firstName1VarChar(50),

    address1VarChar(255))

    insert into @TempTab

    (lastName1,firstName1,address1)

    exec procedure1

    insert into @TempTab

    (lastName1,firstName1,address1)

    exec procedure2

    Select * from @TempTab

    -Roy

  • It´s really simple.

    And the best part is that we can order contents of the TempTable.

    Thanks a lot.

  • Glad to be of help.

    -Roy

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

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