hoangnnm
17-03-2011, 21:39
Chào các bạn, hy vọng hôm nay các bạn giúp tôi giải đáp 1 thắc mắc nữa mà mấy hôm nay bận quá không lên hỏi các bạn được.
Cách đây vài hôm tôi gặp 1 trường hợp thế này :
- Có bảng x đã có số liệu .
- Select col1,col2...., colx into x2 from x .
- delete from x
- insert into x select * from x2 order by col1,col2
- sau đó chạy 1 đoạn update running total theo order trên thì kết quả ra sai bởi order không đúng theo col1,col2.
- Sau khi thay delete from bằng truncate table thì đã chạy bình thường .
Vậy nhờ các bạn chỉ giáo cho tôi biết nguyên nhân là vì sao .
Cám ơn .
[=========> Bổ sung bài viết <=========]
Đây là code ví dụ :
if object_id('tempdb..##test1') is not null drop table ##test1
if object_id('tempdb..##test2') is not null drop table ##test2
Create table ##test1 ( x char(16) , ngay datetime, y char(16) )
declare @i int , @rand varchar(1)
select @i = 1
while @i < 2000
begin
select @rand = substring( convert( varchar(16), rand() ) , 3, 1 )
Select @rand = case when @rand = '0' then '1' else @rand end
insert into ##test1 values ( left(newid(),16 ) , convert ( datetime, '2011-01-0' + @rand ) , left(newid(), 16) )
select @i = @i + 1
end
select * Into ##test2 from ##test1 order by ngay
Delete From ##test1
-- truncate table ##test1 --- neu thay bang truncate thi order ok .
insert into ##test1 select * From ##test2 order by ngay
select x, convert(varchar(12), ngay, 103) as ngay, y From ##test1
[=========> Bổ sung bài viết <=========]
Hello ............. !
Cách đây vài hôm tôi gặp 1 trường hợp thế này :
- Có bảng x đã có số liệu .
- Select col1,col2...., colx into x2 from x .
- delete from x
- insert into x select * from x2 order by col1,col2
- sau đó chạy 1 đoạn update running total theo order trên thì kết quả ra sai bởi order không đúng theo col1,col2.
- Sau khi thay delete from bằng truncate table thì đã chạy bình thường .
Vậy nhờ các bạn chỉ giáo cho tôi biết nguyên nhân là vì sao .
Cám ơn .
[=========> Bổ sung bài viết <=========]
Đây là code ví dụ :
if object_id('tempdb..##test1') is not null drop table ##test1
if object_id('tempdb..##test2') is not null drop table ##test2
Create table ##test1 ( x char(16) , ngay datetime, y char(16) )
declare @i int , @rand varchar(1)
select @i = 1
while @i < 2000
begin
select @rand = substring( convert( varchar(16), rand() ) , 3, 1 )
Select @rand = case when @rand = '0' then '1' else @rand end
insert into ##test1 values ( left(newid(),16 ) , convert ( datetime, '2011-01-0' + @rand ) , left(newid(), 16) )
select @i = @i + 1
end
select * Into ##test2 from ##test1 order by ngay
Delete From ##test1
-- truncate table ##test1 --- neu thay bang truncate thi order ok .
insert into ##test1 select * From ##test2 order by ngay
select x, convert(varchar(12), ngay, 103) as ngay, y From ##test1
[=========> Bổ sung bài viết <=========]
Hello ............. !