Hi @Analyst_SQL ,
check this query:
MERGE #table2 AS Target
USING (select ROW_NUMBER()over(order by t1.A_Date)+210 as [t2_ID]
,t1.EnrollNumbeR,t1.A_Date, case when t1.In_Time is not null then t1.In_Time else t2.In_Time end as [In_Time],t1.out_Time
from #table1 t1 left join #table2 t2 on t2.EnrollNumber =t1.EnrollNumbeR and t2.A_Date = t1.A_Date
)
AS Source
ON Source.EnrollNumbeR = Target.EnrollNumbeR and Source.A_Date = Target.A_Date
WHEN NOT MATCHED BY Target THEN
INSERT (t2_ID , EnrollNumbeR ,A_Date ,In_Time ,out_Time)
VALUES (Source.t2_ID,Source.EnrollNumbeR, Source.A_Date,Source.In_Time,Source.out_Time)
WHEN MATCHED THEN UPDATE SET
Target.In_Time = Source.In_Time,
Target.out_Time = Source.out_Time;
select * from #table2
Best regards
Niko
----------
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.