gpt4 book ai didi

sql - 如何获取插入时最后一行的自动增量值

转载 作者:行者123 更新时间:2023-12-02 01:02:35 25 4
gpt4 key购买 nike

我用谷歌搜索了这个问题一周,但没有任何有用的东西,我认为我没有使用正确的词

我正在使用带有 t-sql 的 SQL Server 2008,我的需要是在插入新行时优化我的函数。

我有一个表,第一列是整数自动增量类型的键,其他列仅供引用

当我们执行插入操作时,SQL Server 会自动递增键,我必须执行 select max 才能获取值,那么有没有一种方法可以像 @@IDENTITY 这样的全局变量或函数以避免开始结束事务并选择 max

最佳答案

使用SCOPE_IDENTITY :

-- do insert

SELECT SCOPE_IDENTITY();

这会给你:

The last identity value inserted into an identity column in the same scope. A scope is a module: a stored procedure, trigger, function, or batch. Therefore, two statements are in the same scope if they are in the same stored procedure, function, or batch.

关于sql - 如何获取插入时最后一行的自动增量值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13451070/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com