2010-09-06 94 views
3

在Guid上排序記錄的最佳方法是什麼?以下從未給出正確的結果。在GUID上排序SQL Server 2008

With UIDs As (--       
      Select ID = 1, UID = NEWID() 
    Union Select ID = 2, UID = NEWID() 
    Union Select ID = 3, UID = NEWID() 
    Union Select ID = 4, UID = NEWID() 
    Union Select ID = 5, UID = NEWID() 
    Union Select ID = 6, UID = NEWID() 
    Union Select ID = 7, UID = NEWID() 
    Union Select ID = 8, UID = NEWID() 
    Union Select ID = 9, UID = NEWID() 
    Union Select ID = 10, UID = NEWID() 
    Union Select ID = 11, UID = NEWID() 
    Union Select ID = 12, UID = NEWID() 
    Union Select ID = 13, UID = NEWID() 
    Union Select ID = 14, UID = NEWID() 
    Union Select ID = 15, UID = NEWID() 
    Union Select ID = 16, UID = NEWID() 
) 
Select * From UIDs Order BY UID, ID 

改爲使用Guid,我如何在我的選擇中生成順序整數。

感謝

+1

** 1。**你是什麼意思,它沒有給出正確的結果。以什麼方式? ** 2。**最後一行的含義是什麼「取而代之的是使用Guid,我如何在我的選擇中生成順序整數」。 ? – 2010-09-06 23:34:16

回答

4

不就是最好的答案。

解釋如何SQL服務器:How are GUIDs sorted by SQL Server?

你有NEWSEQUENTIALID但這是not guaranteed如您所願進行排序。當你有排名功能時爲什麼要使用GUID?

+0

可能是這樣,但我正在調試,並試圖找到數據庫中的特定鍵,並想按照他們的GUID ID按字母順序對我的行進行排序,所以最有幫助的答案實際上是我需要的:)但是,對,請勿在此類應用程序之外使用它。 – 2017-05-22 12:05:38

10
SELECT * 
FROM myTable 
ORDER BY CAST(myGuid AS VARCHAR(36))