Today I came through the weird issue for which I need your help. Basically, I am copying a table(temp_a) into table (temp_b) using below query.
select * into temp_b
from temp_a
where cast(date_from as date)>='2010-01-01'
and cast(date_from as date)<'2018-01-01'
Temp_a table Structure and sample data:
id int primary key,
name varchar not null,
date_from datetime,
update_time getdate()
Temp_A
ID name date_from update_time
-------------------------------------------------------
1 A 2010-01-01 2010-01-01
2 B 2011-02-02 2011-02-02
3 C 2012-02-02 2012-02-02
4 D 2013-09-09 2013-09-09
5 E 2014-08-06 2014-08-06
But above query results duplicate records in temp_b table.
Temp_B
ID name date_from update_time
-------------------------------------------------------
1 A 2010-01-01 2010-01-01
1 A 2010-01-01 2010-01-01
2 B 2011-02-02 2011-02-02
3 C 2012-02-02 2012-02-02
3 C 2012-02-02 2012-02-02
4 D 2013-09-09 2013-09-09
5 E 2014-08-06 2014-08-06
Can someone please elaborate what would be the reason for duplicate records in destination table when there is no duplicate in the source table.
Or
How "Select * into" works in the background?
DROP TABLE temp_b
before you run theSELECT ...
part.