我需要从 SQL Server
获取下一个标识值。
我使用这段代码:
SELECT IDENT_CURRENT('table_name') + 1
这是正确的,但是当 table_name
为空时(下一个标识值为“1”)返回“2”但结果为“1”
最佳答案
我想您会想寻找一种替代方法来计算下一个可用值(例如 setting the column to auto-increment )。
来自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.
它甚至看起来都不是那么可靠,尤其是当您最终设计的应用程序有多个人同时写入表格时。
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.
关于c# - 如何从 SQL Server 获取下一个标识值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20805067/