Friday, August 1, 2014

Update with the join of date function

Yes you will not get the result for just removing the seconds.


You can just add an ID on both tables.


Maybe you can move the data in another temp table with ID column that has an identity.



create table #table1 (T_code int, t_date datetime)
insert into #table1 values (300,'2012-01-15 05:43:26.821')
insert into #table1 values (301,'2012-01-15 06:45:26.821')
insert into #table1 values (302,'2012-01-15 06:45:27.821')
insert into #table1 values (303,'2012-01-15 06:45:28.821')
create table #table2 (c_code int,T_code int, t_date datetime)

insert into #table2 values (400,120,'2012-01-15 05:43:27.822')
insert into #table2 values (401,121,'2012-01-15 06:45:28.824')
insert into #table2 values (402,122,'2012-01-15 06:45:29.825')
insert into #table2 values (403,123,'2012-01-15 06:45:30.836')

--Create another temp table with ID
create table #tablea (ID int identity(1,1),T_code int, t_date datetime)
create table #tableb (ID int identity(1,1),c_code int,T_code int, t_date datetime)
--insert table value from table1 and table2
insert into #tablea(T_code,t_date) select * from #table1 order by t_date
insert into #tableb(c_code,T_code,t_date) select * from #table2 order by t_date

select * from #tablea
select * from #tableb
--update the temp tableb
update a
set a.T_code=b.T_code
from #tableb a
join #tablea b
on a.ID=b.ID
--delete records in table2
truncate table #table2
--insert records from tableb to table2
insert into #table2 select c_code,T_code,t_date from #tableb

select * from #table1
select * from #table2


No comments:

Post a Comment