Hi Experts,

I have two database in same server I.e. D1 and D2. D1 has one table T1 and D2 has one table T2. T1 and T2 is having same table structure and same columns. But D1 is online database and D2 is offline database.D1 will be updating regularly but for D2 I have to update once in day. In T1 and T2 we have column Time_created (it will capture the time when data is created) and Time_modified (it will capture the time when data is modified)

I have to write a query that will take max(time_created) from T2 and compare with each row of T1 anf IF time_created > max(time_created) of T2 then it should insert the that particular row in T2 ELSE it should check Time_modified of that row if it ‘s within 24 hour than it should update that row in T2.

I have to apply below logic:

Select max(Time_created) from D2.username.T2 as R

IF (Time_created.D1 > R)
Insert row in T2.D2
Else IF(Time_modified is within 12 hour)
Update row in T2.D2


But if else will check only for one row.How can I write above in Loop.


Please suggest.


Thanks