为什么@@Identity 返回 null?
我有一个 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
...因此您应该使用
SELECT SCOPE_IDENTITY()
而不是RETURN SELECT SCOPE_IDENTITY()
... So you'd use
SELECT SCOPE_IDENTITY()
notRETURN SELECT SCOPE_IDENTITY()
执行标量
重写为
和 (因为
scope_indentity()
返回numeric(38,0)
)因此您需要将 RETURN 语句分别
ExecuteScalar
So you need to re-write the RETURN statements as
and (since
scope_indentity()
returnsnumeric(38,0)
)respectively
您必须在插入后立即调用@@IDENTITY,请改用Scope_identity()。
you have to call @@IDENTITY right after the insert, use Scope_identity() instead.
因为你的问题遗漏了很多细节,所以我只会提到一些可能的解决方法,因为如果没有所有细节,似乎不可能回答问题。但这是你第一次来这里,所以你会变得更好。你会的吧?
无论如何,首先我想说你应该始终使用scope_identity,因为它更安全。幕后可能发生的事情可能会导致真正的问题。坚持使用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
use
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.
我个人建议使用 SCOPE_IDENTITY 而不是 @@IDENTITY。话虽这么说,问题出在存储过程中。上面的 Devio 是正确的,执行标量正在查找第一行的第一列。 RETURN 语句不会执行此操作,因此您需要使用以下项目之一:
或:
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:
or: