我用这样的数字创建了表:How to find gaps of data and insert NULL data points instead having gap
;WITH
Pass0 as (select 1 as C union all select 1), --2 rows
Pass1 as (select 1 as C from Pass0 as A, Pass0 as B),--4 rows
Pass2 as (select 1 as C from Pass1 as A, Pass1 as B),--16 rows
Pass3 as (select 1 as C from Pass2 as A, Pass2 as B),--256 rows
Pass4 as (select 1 as C from Pass3 as A, Pass3 as B),--65536 rows
Pass5 as (select 1 as C from Pass4 as A, Pass4 as B),--4,294,967,296 rows
Tally as (select row_number() over(order by C) as Number from Pass5)
select Number INTO dbo.Numbers from Tally where Number <= 1000000
它就像一个魅力,但经过几个月的SQL Server运行,一些SQL服务重启等,我的数据库正在恢复几分钟。
我不确定恢复时间后我的桌子是否破了,但是我在某些时候注意到我的行没有按顺序排列。
MIN is 1,
MAX is 1000000,
AVG is 500000
所以看起来所有行仍然存在,但最后一行不是1000000。
此外,在第一批3332行之后,它会丢失订单,而下一行编号为49029。
我使用该表来查找数据中的间隙并插入NULL。
基于该表的我的应用程序失败了。如果该表是有序的,我在应用程序方面没有任何检查,但这是我的假设。我正在使用SQL引擎,所以我应该得到保证。
任何想法会发生什么?
答案 0 :(得分:3)
表中的行无法更改订单,因为.....他们没有订单。未定义集的顺序。除非你的select有一个ORDER BY子句,否则它返回的顺序是随机的,甚至可以在两次调用之间改变。
这是基本的SQL。
您需要订单,将订单放入SELECT语句。