union

  • In a select statement with UNION how do I give the assignment variables

    example

    declare @a int

    SELECT @a=a FROM Table1

    UNION

    SELECT @a=aFROM Table2

    if I give the above and execute then it says that only the 1st select statement can have a assignment variable and if I remove the assignment variable from the second select statement then it also gives a error

    How do I get this value into the @a variable

    Thanks

  • What is your goal? As far as answer when say table 1 a = 1 and table 2 a = 2 then what would be @a? If table 1 is null and table 2 = 1 then what is @a, and vice versa? I need to know what the result is you expect to know the proper answer.

    As for as is. Union is treated like a complete set so you cannot use assignment to get one but not the other, but at the same time assigment with union is disallowed (factor of two potential outcomes for one assigment) such as when you have and = (select x from tblY) and get the error more than one result returned. You could try various methods of eliminating nulls and a subquery for the union. But it the correct solution depends on you final result.

Viewing 2 posts - 1 through 1 (of 1 total)

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