如何从SQL Server获取下一个标识值 [英] How to get the next identity value from SQL Server

查看:136
本文介绍了如何从SQL Server获取下一个标识值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从 SQL Server 获取下一个标识值。

I need to get the next identity value from SQL Server.

我使用此代码:

SELECT IDENT_CURRENT('table_name') + 1

这是正确的,但是当 table_name 为空(下一个标识值为 1)时返回 2,但结果为 1

This is correct, but when the table_name is empty (and next identity value is "1") returned "2" but result is "1"

推荐答案

我认为您想寻找一种替代方法来计算下一个可用值(例如将列设置为自动)。

I think you'll want to look for an alternative way to calculate the next available value (such as setting the column to auto-increment).

IDENT_CURRENT 文档,有关空表:


当IDENT_CURRENT值为NULL时(因为表具有从未包含行或已被截断),则IDENT_CURRENT函数

When the IDENT_CURRENT value is NULL (because the table has never contained rows or has been truncated), the IDENT_CURRENT function returns the seed value.

它似乎还不那么可靠,尤其是当您最终设计出功能更多的应用程序时

It doesn't even seem all that reliable, especially if you end up designing an app that has more than one person writing to the table at the same time.


请谨慎使用IDENT_CURRENT来预测下一个生成的标识值。实际生成的值可能不同于IDENT_CURRENT加上IDENT_INCR,因为其他会话执行了插入操作。

Be cautious about using IDENT_CURRENT to predict the next generated identity value. The actual generated value may be different from IDENT_CURRENT plus IDENT_INCR because of insertions performed by other sessions.

这篇关于如何从SQL Server获取下一个标识值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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