返回存储过程中最后插入的行的标识 [英] Return identity of last inserted row from stored procedure

查看:62
本文介绍了返回存储过程中最后插入的行的标识的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从存储过程中返回最后插入的行的身份.

I am trying to return identity of last inserted row from a stored procedure.

我的代码的简化版本如下:

A simplified version of my code looks like this:

CREATE PROCEDURE [sp_name]
    @AuthorisationCode uniqueidentifier
AS
    INSERT INTO [tablename]
           ([AuthorisationCode]
           )
     VALUES
           (@AuthorisationCode
           )

 RETURN @@IDENTITY

GO

我正在通过Enterprise库4.1中的Execute Scalar调用此存储过程.

I am calling this stored procedure via Execute Scalar in Enterprise library 4.1.

它返回null.有人看到我在做什么错了.

It returns null. Anybody see what I am doing wrong.

推荐答案

我说你应该使用 SCOPE_IDENTITY(),因为 @@ identity 将返回身份最后插入的内容(如果同时运行多个查询,则可能不是您的存储过程).

I'd say you should be using SCOPE_IDENTITY() as @@identity will return the identity of the last thing inserted (which may not be your stored procedure if multiple queries are running simultaneously).

您还需要选择它,而不要返回它.

You also need to SELECT it, not RETURN it.

ExecuteScalar 将返回结果集第一行中的第一列值.

ExecuteScalar will return the first column value from the first row of a result set.

所以...

SELECT SCOPE_IDENTITY();

可能是您想要的.

这篇关于返回存储过程中最后插入的行的标识的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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