为什么@@Identity 返回 null?

发布于 2024-09-26 13:06:38 字数 540 浏览 5 评论 0原文

我有一个 .NET 2010 应用程序访问 SQL2000 数据库。该代码非常基本。当我插入一条记录时,记录被插入,但没有返回id。 id 列是一个 int 并且它是一个 Identity。这是存储过程...

ALTER PROCEDURE Insert_Vendor

@CorpID as varchar(255), 
@TaxpayerID as varchar(255)
AS

Insert into  dbo.Vendor
(
vdr_CorpID, 
vdr_TaxpayerID
)
values
(
@CorpID, 
@TaxpayerID
)

        IF @@error <> 0 
            BEGIN
                RETURN -1
            END
        ELSE 
        RETURN @@Identity
GO

在接收端...

 int myID = (int)(db.ExecuteScalar(dbCommand));

I have a .NET 2010 app hitting a SQL2000 db. The code is pretty basic. When I insert a record, the record is inserted, but the id is not returned. The id column is an int and it is an Idetity. Here is the stored proc...

ALTER PROCEDURE Insert_Vendor

@CorpID as varchar(255), 
@TaxpayerID as varchar(255)
AS

Insert into  dbo.Vendor
(
vdr_CorpID, 
vdr_TaxpayerID
)
values
(
@CorpID, 
@TaxpayerID
)

        IF @@error <> 0 
            BEGIN
                RETURN -1
            END
        ELSE 
        RETURN @@Identity
GO

And on the receiving end...

 int myID = (int)(db.ExecuteScalar(dbCommand));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

丑疤怪 2024-10-03 13:06:38
  • 您应该始终使用 SCOPE_IDENTITY()
  • 不能通过存储过程中的 RETURN 返回 NULL。您会收到 SQL 警告,并且它会返回零。
  • ExecuteScalar 查找记录集的第一行、第一列。上面没有记录集

...因此您应该使用 SELECT SCOPE_IDENTITY() 而不是 RETURN SELECT SCOPE_IDENTITY()

  • You should always use SCOPE_IDENTITY()
  • NULL can't be returned via RETURN from a stored proc. You'd get a SQL warning and it would return zero.
  • ExecuteScalar looks for the 1st row, 1st column of a recordset. There is no recordset above

... So you'd use SELECT SCOPE_IDENTITY() not RETURN SELECT SCOPE_IDENTITY()

风轻花落早 2024-10-03 13:06:38

执行标量

执行查询,并返回
第一行的第一列
查询返回的结果集

重写为

SELECT -1

和 (因为 scope_indentity() 返回 numeric(38,0)

SELECT CAST(SCOPE_IDENTITY() AS INT)

因此您需要将 RETURN 语句分别

ExecuteScalar

executes the query, and returns the
first column of the first row in the
result set returned by the query

So you need to re-write the RETURN statements as

SELECT -1

and (since scope_indentity() returns numeric(38,0))

SELECT CAST(SCOPE_IDENTITY() AS INT)

respectively

小清晰的声音 2024-10-03 13:06:38

您必须在插入后立即调用@@IDENTITY,请改用Scope_identity()。

you have to call @@IDENTITY right after the insert, use Scope_identity() instead.

站稳脚跟 2024-10-03 13:06:38

因为你的问题遗漏了很多细节,所以我只会提到一些可能的解决方法,因为如果没有所有细节,似乎不可能回答问题。但这是你第一次来这里,所以你会变得更好。你会的吧?

无论如何,首先我想说你应该始终使用scope_identity,因为它更安全。幕后可能发生的事情可能会导致真正的问题。坚持使用scope_identity,你就不必担心。

其次,我建议不要

RETURN

使用

SELECT SCOPE_IDENTITY()

最后我会说为什么不只使用 OUTPUT 参数而不是返回结果。我没有任何东西支持下一个陈述,但我认为它更好。同样没有证据证明这一点,但与模式附带的结果集相比,输出参数的开销似乎更少。

只是我的想法。

Because your question leaves out a lot of details I will just mention a few possible ways around this as it seems impossible to answer a question without all the details. But it's your first time here so you'll get better. You will right?

Anyways first I would say you should always use scope_identity as it is safer. There could be things going on behind the scenes with triggers that could cause this real problems. Stick with scope_identity and you shouldn't have to worry.

Second I would suggest instead of

RETURN

use

SELECT SCOPE_IDENTITY()

Lastly I would say why not just use an OUTPUT parameter vs returning a result. I don't have anything to support this next statement but I would think it is better. Again no proof on that but it just seems like less overhead with output parameter vs resultset that comes with schema.

Just my thoughts.

小…楫夜泊 2024-10-03 13:06:38

我个人建议使用 SCOPE_IDENTITY 而不是 @@IDENTITY。话虽这么说,问题出在存储过程中。上面的 Devio 是正确的,执行标量正在查找第一行的第一列。 RETURN 语句不会执行此操作,因此您需要使用以下项目之一:

    IF @@error <> 0 
    BEGIN
        Select -1
    END
    ELSE 
    Select @@Identity

或:

    IF @@error <> 0 
    BEGIN
        Select -1
    END
    ELSE 
    Select SCOPE_IDENTITY()

I personally would recommend using SCOPE_IDENTITY instead of @@IDENTITY. That being said the problem is in the stored procedure. Devio above was correct the execute scalar is looking for the first column of the first row. The RETURN statement will not do this so you will need to use either one of the below items:

    IF @@error <> 0 
    BEGIN
        Select -1
    END
    ELSE 
    Select @@Identity

or:

    IF @@error <> 0 
    BEGIN
        Select -1
    END
    ELSE 
    Select SCOPE_IDENTITY()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文