Using the data supplied by venku009, my idea can be transformed this way:
createtable #temp1(patno int, hospno int, roomcode bit)createtable #temp2(patno int, hospno int, proc_code int)createtable #temp3(proc_code int, department int)insertinto #temp1select 1, 123, nullunionselect 1, 124,nullinsertinto #temp2select 1, 123, 256unionselect 1, 123, 258unionselect 1, 123, 300unionselect 1, 124, 500insertinto #temp3select 258, 701unionselect 200, 701--select * from #temp1--select * from #temp2--select * from #temp3 ;with cte as (select T1.*, casewhenexists (select 1 from #temp2 T2 innerjoin #temp3 T3 on T2.proc_code = T3.proc_code where T3.department = 701 and T2.patno = T1.patno) then 1 else 0 endas Mark from #temp1 T1)update cte set roomcode = Mark select * from #temp1
For every expert, there is an equal and opposite expert. - Becker's Law
My blog