本文介绍了对于返回超过 1 个值的 SQL select,当 Id 为 GUID 时它们如何排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道 SQL Server 如何对从查询返回的数据进行排序,并且各个表的 Id 列都是 uniqueidentifier 类型.

I'm wondering how SQL Server orders data that is returned from a query and the Id columns of the respective tables are all of type uniqueidentifier.

我在创建所有 GUID 时使用 NHibernate GuidComb 并执行以下操作:

I'm using NHibernate GuidComb when creating all of the GUIDs and do things like:

Sheet sheet = sheetRepository.Get(_SheetGuid_); // has many lines items
IList<SheetLineItem> lineItems = sheet.LineItems;

我只是想弄清楚当我执行以下操作时将如何订购它们:

I'm just trying to figure out how they'll be ordered when I do something like:

foreach (SheetLineItem lineItem in lineItems)

我找不到关于在订购时通过 SQL 比较 GUID 的方式的好文章,如果是这样的话.

I can't see to find a good article on the way GUIDs are compared by SQL when being ordered, if that's what's happening.

推荐答案

除非您包含 ORDER BY 子句,否则 SQL Server 不保证结果的任何顺序.它可能会以一致的顺序返回(例如聚集索引顺序),但您不能确定情况总是如此(例如,如果查询被拆分并在多个线程上执行,当结果合并时,每次执行的顺序可能不同,因为线程可能以不同的顺序完成).

Unless you incude an ORDER BY clause SQL Server doesn't guarantee any order on the results. It may sem to come back in the some order consistently (e.g. clustered index order) but you can't be sure this will always be the case (e.g. if the query is split and executed on multiple threads, when the results are combined, the order may be different on each execution since the threads may complete in different orders).

获得特定订单的唯一方法是使用 ORDER BY 子句.在 NHibernate 中,这可以通过在映射文件中的包(或等价物)上指定 order-by="..." 来实现.

The only way to get a particular order is to use an ORDER BY clause. In NHibernate, this would be achieved by speifying an order-by="..." on your bags (or equivalent) in your mapping files.

有关order-by"的更多信息,请参阅 NHibernate 文档:http://nhibernate.info/doc/nh/en/index.html

See the NHibernate docs for more info on "order-by": http://nhibernate.info/doc/nh/en/index.html

这篇关于对于返回超过 1 个值的 SQL select,当 Id 为 GUID 时它们如何排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 16:42