如何在SQL中添加值并通过存储过程更新它?

时间:2022-08-27 18:16:57

I have table name called "tblBoxStock" and get the value named @newQty and want to add up on top of the current value (column name currentQty).

我有一个名为“tblBoxStock”的表名,并获取名为@newQty的值,并希望在当前值(列名currentQty)之上加起来。

Create proc spUpdateQty @newQty int, @boxName nvarchar(20)
as 
Begin 
    Update tblBoxStock
    set currentQty + @newQty = @newQty
    where boxName = @boxName   
End  

the line

这条线

set currentQty + @newQty = @newQty

shows an error. How can I fix this?

显示错误。我怎样才能解决这个问题?

2 个解决方案

#1


0  

It should be done like this

它应该这样做

Update tblBoxStock
set currentQty = currentQty  + isnull(@newQty,0)
where boxName = @boxName 

#2


0  

You need to set the column value:

您需要设置列值:

Update tblBoxStock
    set currentQty = currentQty + @newQty
    where boxName = @boxName;

Some databases let you write:

有些数据库可以让你写:

Update tblBoxStock
    set currentQty += @newQty
    where boxName = @boxName;

I would suggest testing the value to see if the update needs to be done:

我建议测试值,看看是否需要更新:

Update tblBoxStock
    set currentQty = currentQty + @newQty
    where boxName = @boxName and @newQty <> 0;

#1


0  

It should be done like this

它应该这样做

Update tblBoxStock
set currentQty = currentQty  + isnull(@newQty,0)
where boxName = @boxName 

#2


0  

You need to set the column value:

您需要设置列值:

Update tblBoxStock
    set currentQty = currentQty + @newQty
    where boxName = @boxName;

Some databases let you write:

有些数据库可以让你写:

Update tblBoxStock
    set currentQty += @newQty
    where boxName = @boxName;

I would suggest testing the value to see if the update needs to be done:

我建议测试值,看看是否需要更新:

Update tblBoxStock
    set currentQty = currentQty + @newQty
    where boxName = @boxName and @newQty <> 0;