sql server 是否介意记录插入的方式? [英] Does sql server minds the way records where inserted?
本文介绍了sql server 是否介意记录插入的方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
(它是如何工作的" - 问题)
假设我有这张表:
Id val
------------------
1 a <--
1 a
1 a
2 b <--
2 b
2 b
现在假设我想要标记的行:
Now lets say I want the marked rows :
我可以依靠这个查询吗:
Can I count on this query :
select id,val from (
select id , val , row_number() over (partition by id order by id) as rn
) k where rn=1
给我 Selected 行?
(注意 order by
子句).它会认为顺序是插入的顺序吗?
(notice the order by
clause). will it consider the order as the order they were inserted ?
推荐答案
不保证行为.您的查询将返回两行,但未定义哪一行.
The behaviour is not guaranteed. Your query will return two rows, but which ones is undefined.
当我尝试
create table #t (id int, val char(1), t timestamp)
insert #t (id, val) values (1,'a')
insert #t (id, val) values (1,'a')
insert #t (id, val) values (2,'b')
insert #t (id, val) values (1,'a')
insert #t (id, val) values (2,'b')
insert #t (id, val) values (2,'b')
select * from #t
select * from (
select *, row_number() over (partition by id order by id) as rn from #t
) k where rn=1
drop table #t
结果是可变的,取决于插入的顺序,但不是一致的第一个插入的行.
the results are variable, depending on the order of insertion, but not consistently the first inserted rows.
这篇关于sql server 是否介意记录插入的方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文