如何更新表中具有范围标识标识为主键的所有列 [英] How to update all the columns in the table having scope identity id as primary key

查看:88
本文介绍了如何更新表中具有范围标识标识为主键的所有列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更新table.but中的所有列。但是当我在where where条件中给id时它没有更新任何东西。我的id是具有范围标识的主键



我尝试过:



如果@abc ='更新'

BEGIN



- 设置NOCOUNT ON;



UPDATE tbl_AccountGroup SET Group_name = @ Group_name,Comments = @Comments,

chbox = @chbox,Under_Group = @Under_Group

其中id = @ id





END

i want to update all columns in table.but when i give id in where condition it is not updating anything.my id is primary key with scope identity

What I have tried:

IF @abc = 'Update'
BEGIN

--SET NOCOUNT ON;

UPDATE tbl_AccountGroup SET Group_name=@Group_name,Comments = @Comments,
chbox = @chbox, Under_Group= @Under_Group
where id=@id


END

推荐答案

以下代码都是错误的。
f @abc='update'

begin
set nocount on;
--declare @Unit_Id int



SELECT SCOPE_IDENTITY() AS unit_Id 


update tbl_UnitCreation set Unit_Name=@Unit_Name,Unit_Abbreviation=@Unit_Abbreviation,
Unit_type=@Unit_type,
Decimal_Places=@Decimal_Places,Description=@Description,Super_Unit=@Super_Unit,Per_Unit=@Per_Unit
where unit_Id=unit_Id
end





首先,您选择SCOPE_IDENTITY()但不将其存储在任何位置。您应该执行以下操作:



First off, you select SCOPE_IDENTITY() but you don't store it anywhere. You should be doing something like:

DECLARE @newId INT
SELECT @newID = SCOPE_IDENTITY()





另一个主要问题是更新中的WHERE子句。您正在更新unit_id = unit_id的位置。这将更新表中的每一行。它与WHERE 1 = 1基本相同。



更改为



The other major problem is the WHERE clause in your update. You are updating where unit_id = unit_id. That will update every single row in your table. It's essentially the same as WHERE 1 = 1.

Change to be

WHERE unit_id = @newID


这篇关于如何更新表中具有范围标识标识为主键的所有列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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