在SQL Server中,如果主键是GUID,如何按插入顺序对表行进行排序? [英] In SQL server how do I order table rows by insertion order if primary key is GUID?

查看:170
本文介绍了在SQL Server中,如果主键是GUID,如何按插入顺序对表行进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的主键,我已经开始在自动增量整数上使用GUID.

I've started using GUIDs over auto-increment integers for my primary keys.

但是,在开发过程中,我习惯于查询(从SQL Management Studio或Visual Studio中)数据库,以查看我的应用程序刚刚插入的记录,而我不能按主键排序这一事实让我感到恼火以查看最新记录.

However, during development I'm used to querying (from SQL management studio or visual studio) database in order to see what records my application just inserted, and I'm pissed off by the fact that I cannot order by primary key desc in order to see the most recent records.

有没有办法完成这项工作?

Is there a way to accomplish tihs?

推荐答案

您不能根据插入顺序对GUID列进行排序.您需要依赖另一列.

You can't order a GUID column based on insertion order. You'll need to rely on another column.

我的建议是添加一个名为CreationMoment的列作为DateTime,其默认值为GETDATE().从理论上讲,您可以发生冲突(即,相同的创建日期/时间),因此您必须确定该数据类型是否合适.以我的经验,这从来都不是问题.

My suggestion is to add a column called CreationMoment as a DateTime with a default value of GETDATE(). Theoretically you could have collisions (i.e. identical date / time of creation), so you'll have to decide if that data type is appropriate. In my experience it's never been an issue.

这篇关于在SQL Server中,如果主键是GUID,如何按插入顺序对表行进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆