如何获得插入行最后一行的自动增量值 [英] How to get the value of autoincrement of last row at the insert
问题描述
我在这个问题上搜索了一个星期,没有任何用处,我认为我没有使用正确的单词
我用t-sql和我的需要使用SQL Server 2008是我优化我的功能,当我插入一个新的行。
我有一个表与第一列是整数自动增量类型的关键和其他列仅供参考
p>当我们执行插入操作时,SQL Server会自动递增键值,并且必须执行select max来获取值,那么是否有像全局变量 @@ IDENTITY
或者一个函数来避免开始结束事务并选择max
- do insert
SELECT SCOPE_IDENTITY() ;
哪个会给你:
插入到
中的标识列中的最后一个标识值的范围相同。范围是一个模块:存储过程,触发器,
函数或批处理。因此,如果
它们在相同的存储过程,函数或批处理中,则两个语句在同一个范围内。
I have googled this problem one week and no thing useful I think am not using the correct word
I am using SQL Server 2008 with t-sql and my need is to optimise my function when I insert a new row.
I have a table with first column is the key of integer autoincrement type and other columns are just for information
When we do an insert, SQL Server increments the key automatically and I have to do a select max to get the value, so is there a way like a global variable like @@IDENTITY
or a function to avoid the begin end transaction and select max
Use SCOPE_IDENTITY
:
-- do insert
SELECT SCOPE_IDENTITY();
Which will give you:
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.
这篇关于如何获得插入行最后一行的自动增量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!