I have 3 tables
- Table1 with columns emp_id, code_1, code_2
- Table 2 code_1, code_2
- Table3 emp_id, emp_ind
Table2 is a lookup table. I need to update Table3 checking code_1 and code_2 of Table1 present in table2
- If code_2 in table2 is null check if code_1 in table 1 and 2 match , if yes update emp_ind in Table3 as Y
- If code_1 in table2 is null check if code_2 in table 1 and 2 match , if yes update emp_ind in Table3 as Y
- If code_1 and code_2 both are valued in table 2, check if both code1 and code2 in Table1 match with that of table2 , if yes update emp_ind in Table3.
Is there a way to doi it simply instead of checking
Update table_3 t3 Set t3.emp_ind = 'Y' where exists (Select 1 from table1 t1 Inner Join table2 t2 on t1.code_1 = t2.code_1 Where t2.code_2 is null And t2.emp_id= t3.emp_id Union Select 1 from table1 t1 Inner Join table2 t2 on t1.code_2 = t2.code_2 Where t2.code_1 is null And t2.emp_id= t3.emp_id Union Select 1 from table1 t1 Inner Join table2 t2 on t1.code_1 = t2.code_1 And t1.code_2 = t2.code_2 Where t2.emp_id= t3.emp_id )