Question

If i have 2 tables say TABLE_1

EMP_ID    EMP_NAME    EMP_COUNTRY
100       John        Russia
101       Mitchell    UK
102       Sarah       Japan

TABLE_2

EMP_ID    EMP_NAME    EMP_COUNTRY
    200    Sunil         India
    201    Clanton       Germany
    202    XYZ           Australia

I want to check whether EMP_ID exists in [table_1 OR table_2] if it exists in one of the tables then based on that set some flag, How to check this.

Was it helpful?

Solution

select count(*)
from
(select emp_id from table_1 
union 
select emp_id from table_2) t
where t.emp_id = <id_value>

OTHER TIPS

You can also try:

SELECT DECODE((
SELECT SUM(CNT) FROM 
(SELECT COUNT(1) CNT FROM TABLE1 WHERE EMP_ID = yr_emp_id
UNION
SELECT COUNT(1)  CNT FROM TABLE2 WHERE EMP_ID = yr_emp_id)),
0,'FALSE','TRUE') 
FROM DUAL;
If (Select Count(*) From TABLE_1 Where EMP_ID = @EMP_ID) > 0
   Begin
      -- set flag 
   End
Else If (Select Count(*) From TABLE_2 Where EMP_ID = @EMP_ID) > 0
   Begin
     -- or set flag here
   End
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top