获取我在 Sql Server 中更新的行的 ID [英] Getting the Id of a row I updated in Sql Server

查看:48
本文介绍了获取我在 Sql Server 中更新的行的 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试返回我在 sql 中更新的行的 ID

I'm trying to return the Id of a row I update in sql

UPDATE ITS2_UserNames
  SET AupIp = @AupIp
  WHERE @Customer_ID = TCID AND @Handle_ID = ID

  SELECT @@ERROR AS Error, @@ROWCOUNT AS RowsAffected, SCOPE_IDENTITY() AS ID

而且我的 ID 一直为 Null,我怎样才能得到这个?

and I keep getting Null for the ID, how can I get this?

推荐答案

@@identityscope_identity() 会给你一个新人的身份 行,即.插入后.更新后,行的标识是...@Customer_ID 还是@Handle_Id?如果是不同的字段,则应使用 OUTPUT 子句返回更新行的 ID:

The @@identity and scope_identity() will hand you the identity of a new row, ie. after an insert. After your update, the identity of the row is... @Customer_ID or @Handle_Id? If it is a different field, you should use the OUTPUT clause to return the ID of the updated row:

UPDATE ITS2_UserNames  
SET AupIp = @AupIp  
OUTPUT INSERTED.PrimaryKeyID
WHERE @Customer_ID = TCID AND @Handle_ID = ID

这篇关于获取我在 Sql Server 中更新的行的 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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