获取最后插入的索引 [英] Get the last inserted index

查看:101
本文介绍了获取最后插入的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个MS SQL过程,该过程接收数据,将其插入表中,然后应该在插入数据的位置检索新索引.唯一的唯一字段是ID,我不想简单地对IDENTITY进行计数,因为IDENTITY_INSERT可能使事情搞砸...我能做些安全的事吗?

那就是我在写的,对吗?

I''m writing a MS SQL procedure that receives data, inserts it into a table, and then is supposed to retrieve the new index where the data was inserted. The only UNIQUE field is the ID, and I wouldn''t like to count simply of the IDENTITY, because IDENTITY_INSERT Could mess things up... Is there anything safe that I could do?

That''s what I was writing, is it any good?

CREATE PROCEDURE dbo.Func(@var text)
        AS
	BEGIN
	INSERT INTO Table (Col) VALUES(@var)
	SELECT TOP 1 TableID FROM Table ORDER BY TableID DESC
	RETURN TableID
	END

推荐答案

看看 ^ ]-插入将作为原子工作单元执行,假设您实际上使用了标识列,那么您将能够执行尝试做的事情安全的方式.

最好的问候
Espen Harlinn
Have a look at the OUTPUT Clause[^] - the insert will execute as an atomic unit of work, assuming you actually use an identity column, you will be able to do what you are attempting to do in a safe manner.

Best regards
Espen Harlinn


这篇关于获取最后插入的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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