|
|
Question : UNION and NOT EXISTS
|
|
i have two queries
ie: select id from tableA where condition=true select id from tableB where condition=true
if records are returned from the first query is there anyway to exclude records returned from the secord query. Please note that I know I can do a UNION and "NOT EXISTS" SUBQUERY on the secord query to restrict returned results, however I am hoping there is a better way as the second query is quite complicated and zaps the processor when run twice.
ie: select id from tableA where condition=true UNION select id from tableB where condition=true and not exists (select id from tableA where condition=true)
thanks in advance.
|
Answer : UNION and NOT EXISTS
|
|
ooops forgot the TABLE keyword:
declare @tA table ( id int ) declare @tB table ( id int ) insert into @tA select id from tableA where condition=true insert into @tB select id from tableB where condition=true delete @tB where id in ( select id from @tA) select * from @tA union all select * from @tB
|
|
|
|