您如何知道下一个身份列将是什么? [英] How Do You Tell What Next Identity Column Will Be?

查看:60
本文介绍了您如何知道下一个身份列将是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一个tsql查询来告知它期望用于下一行插入的SQL Server标识列值是什么?

Is there a tsql query to tell what SQL server identity column value it expects to use for the next row insert?

编辑后添加:

我用

[personID] [int] IDENTITY(1,1) NOT NULL

作为CREATE TABLE命令的一部分.我还尝试了在删除该表中的所有信息时重新设置标识列的种子,但这并不总是有效的.我想知道是否有一种方法可以查看期望下一个标识列号使用的SQL.

as part of my CREATE TABLE command. I've also attempted to reseed identity columns while removing all information in that table and that hasn't always worked. It got me to wondering if there was a way to see what SQL expected to use for your next identity column number.

推荐答案

不,没有任何保证的方式(尽管您当然可以找出下一个值可能 be,则可以先使用另一个命令,然后再使用它).您可以检索的唯一保证值是通过

No, there isn't any guaranteed way (although you can certainly find out what the next value might be, another command might go and use it before you can make any use of it). The only guaranteed value you can retrieve is the previously inserted identity value through SCOPE_IDENTITY() (which will return the identity value last generated for the current scope).

为什么要在使用自动递增的种子身份列之前需要知道该值,目的为何?

It's questionable what purpose why one would need to know the value before (when using an automatically incremented seeded identity column).

如果您之前需要知道该值,那么我建议您自己生成ID.您可以通过在表名上键入一个ids表来完成此操作,或者,如果您有可伸缩性方面的考虑(并且您正在使用事务),则可以为每个需要id的表创建一个id表,并在其中插入id(然后增加).

If you need to know the value before, then I recommend generating the ids yourself. You can do this with an ids table keyed on the table name, or, if you have scalability concerns (and you are using transactions) you can have an id table for each table that needs an id which would have the id to be inserted (and subsequently incremented).

或者,您可以使用GUID,并且可以在将其发送到数据库之前轻松地在客户端上生成它们.

Or, you could use a GUID, and you would be able to easily generate these on the client side before sending it to your database.

这篇关于您如何知道下一个身份列将是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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