日期:2014-05-18  浏览次数:20599 次

一个简单的问题 insert顺序
我在查询分析器里一次执行了5个insert   into   select   语句,把5条数据插入到一张表(表中只有一个字段)中,可是为什么插入的顺序不是按照我写的insert语句的顺序插入的呢


------解决方案--------------------
那個字段是不是關鍵字?
------解决方案--------------------
Create Table TEST
(ID Int)
Insert TEST Select 1
Union All Select 3
Union All Select 2
GO
Select * From TEST
GO
Drop Table TEST
--Result
/*
ID
1
3
2
*/
------解决方案--------------------
自动排列了
------解决方案--------------------

Create Table TEST
(ID Int Primary Key)
Insert TEST Select 1
Union All Select 3
Union All Select 2
GO
Select * From TEST
GO
Drop Table TEST
--Result
/*
ID
1
2
3
*/

------解决方案--------------------
数据库会自动按主键建立物理索引,可能是这个问题。
看看是不是自动按主键排序了
------解决方案--------------------
create table test(dt datetime primary key)
insert test select '2007-02-02 '
union all select '2007-02-04 '
union all select '2007-02-01 '

select * from test

drop table test

------解决方案--------------------
dt
------------------------------------------------------
2007-02-01 00:00:00.000
2007-02-02 00:00:00.000
2007-02-04 00:00:00.000