Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 how to select and update a cell in one query

Author  Topic 

MasterOfUniverse
Starting Member

1 Post

Posted - 2011-07-15 : 09:04:26
i have a table with 2 fields Items and qty, i want to select the qty for an item and update it adding a new qty to the old

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2011-07-15 : 09:54:32
update table
set qty = qty + 200 -- 200 is just an example for the new qty to add
where item = 4711


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2011-07-15 : 09:54:43
A simple way would be as follows:
declare @newQuantity int;
set @newQuantity = 100;

declare @ItemNumber int;
set @ItemNumber = 37444;

update YourTable set
Qty = isnull(Qty,0) + @newQuantity
where
Item = @ItemNumber;
This updates the data for one Item. You could expand this to update the quantities for a set of rows from another table etc.

Go to Top of Page
   

- Advertisement -