Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 how to avoid union all with where condition query

Author  Topic 

subhaoviya
Posting Yak Master

135 Posts

Posted - 2014-04-02 : 07:19:43
Hi I have query like,

SELECT C1, C2 FROM T1 Where C3='MyVal'
UNION ALL
SELECT C1, C2 FROM T2 Where C3='MyVal'

there is some possibility like
- C3='MyVal' not in both tables T1, T2
- C3='MyVal' May present in any one table
- C3='MyVal' may present in both tables.

Thanks
Subha

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2014-04-02 : 07:59:57
quote:
Originally posted by subhaoviya

Hi I have query like,

SELECT C1, C2 FROM T1 Where C3='MyVal'
UNION ALL
SELECT C1, C2 FROM T2 Where C3='MyVal'

there is some possibility like
- C3='MyVal' not in both tables T1, T2
- C3='MyVal' May present in any one table
- C3='MyVal' may present in both tables.

Thanks
Subha




Please give us examples with all possibilities over the 2 tables AND the wanted result in relation to the example data.


Too old to Rock'n'Roll too young to die.
Go to Top of Page

Lamprey
Master Smack Fu Yak Hacker

4614 Posts

Posted - 2014-04-02 : 11:26:33
quote:
Originally posted by subhaoviya

Hi I have query like,

SELECT C1, C2 FROM T1 Where C3='MyVal'
UNION ALL
SELECT C1, C2 FROM T2 Where C3='MyVal'

there is some possibility like
- C3='MyVal' not in both tables T1, T2
- C3='MyVal' May present in any one table
- C3='MyVal' may present in both tables.

Thanks
Subha



Why do you want to avoid UNION ALL? What issue are you trying to solve? Do you want the same results as a UNION ALL, but without using a UNION ALL?
Go to Top of Page

subhaoviya
Posting Yak Master

135 Posts

Posted - 2014-04-04 : 08:01:00
Hi,
Sorry for late reply, I want to avoid union functions due to performance issue, because both table having million of records. I have resolved it by using dynamic SQL.

just check the existence of value in both tables then for condition.
then executed the query using join function.

thanks
subha
Go to Top of Page
   

- Advertisement -