tags:

views:

68

answers:

2

I have a table like this :-

Product ID            Weight
A                     100
B                     100
C                     100
D                     100
E                     100 

I want to change it to:-

Product ID            Weight
A                     501
B                     601
C                     701
D                     801
E                     401 

How can I do that with SQL update command ??

+3  A: 

Use Case expression like this

UPDATE products SET
Weight = 
CASE ProductID
   WHEN 'A' THEN 501
   WHEN 'B' THEN 601
   WHEN 'C' THEN 701
   WHEN 'D' THEN 801
   WHEN 'E' THEN 401
END

More info: CASE (Transact-SQL), Case Oracle, Case MySQL

gyromonotron
Hi, I tried to do that with some extra :-UPDATE products SETWeight =(CASE ProductID WHEN 'A' THEN 501 WHEN 'B' THEN 601 WHEN 'C' THEN 701 WHEN 'D' THEN 801 WHEN 'E' THEN 401END)where Store = 'HHH' and Company = 'ABC'The system prompted with the following error message:-Error: SQL0199 - Keyword WHERE not expected. Valid tokens: SET. (State:37000, Native Code: FFFFFF39)
Bob
What RDBMS is used? (SQL SERVER, MySQL, Oracle, PostgreSQL)
gyromonotron
SQL SERVER......
Bob
Try to remove round brackets...
gyromonotron
According to Error SQL0199 looks like you are using DB2
gyromonotron
After i hv removed the brackets, it prompted with another error code:-Error SQL0407 - Null values not allowed in column or variable Weight
Bob
Ok, table products contains some additional productIDs (not only A, B, C, D, E). So you should add ELSE statement for this case. (Example, CASE ProductID WHEN 'A' THEN 501 WHEN 'B' THEN 601 WHEN 'C' THEN 701 WHEN 'D' THEN 801 WHEN 'E' THEN 401 ELSE 100END)
gyromonotron
Thanks a lot. It works well. :-)
Bob
It works well (for the ELSE) based on the assumption that the other productsIDs are 100. If I wish not to change to 100 but to maintain its origin value of the other productIDs, what should I do then ?
Bob
You should leave old weight ( CASE ProductID WHEN 'A' THEN 501 WHEN 'B' THEN 601 WHEN 'C' THEN 701 WHEN 'D' THEN 801 WHEN 'E' THEN 401 ELSE Weight END)
gyromonotron
A: 
UPDATE [TABLE] SET Weight = 501 WHERE [Product ID] = 'A'
GO
UPDATE [TABLE] SET Weight = 601 WHERE [Product ID] = 'B'
GO
UPDATE [TABLE] SET Weight = 701 WHERE [Product ID] = 'C'
GO
UPDATE [TABLE] SET Weight = 801 WHERE [Product ID] = 'D'
GO
UPDATE [TABLE] SET Weight = 401 WHERE [Product ID] = 'E'
GO
rdkleine
This is for single row but I hv multiple rows. How to do that ?
Bob
Add more of them
rdkleine
UPDATE [TABLE] SET Weight = 501 WHERE [Product ID] = 'A'UPDATE [TABLE] SET Weight = 601 WHERE [Product ID] = 'B'UPDATE [TABLE] SET Weight = 701 WHERE [Product ID] = 'C'UPDATE [TABLE] SET Weight = 801 WHERE [Product ID] = 'D'UPDATE [TABLE] SET Weight = 401 WHERE [Product ID] = 'E'I hv run one at a time. I cant execute them together ? Is there a way to run them concurrently.
Bob
They will never run concurrently, update performs one row at a time. Why is that important?
rdkleine
I hv thousand of rows to update. There must be a way to shorten the process. I cant be sitting here the whole day just to run row by row.
Bob
Thousand of rows shouldn't take more than seconds or minutes. There must be something else whats wrong. How much time does one update take? What does the Execution Plan look like?
rdkleine
What I meant earlier was after inserted the SQL statements :-UPDATE [TABLE] SET Weight = 501 WHERE [Product ID] = 'A'UPDATE [TABLE] SET Weight = 601 WHERE [Product ID] = 'B'UPDATE [TABLE] SET Weight = 701 WHERE [Product ID] = 'C'I clicked Execute button. It prompted an error msg:-Error: SQL0199 - Keyword UPDATE not expected.Next, I ran the above statement one at a time ie.UPDATE [TABLE] SET Weight = 501 WHERE [Product ID] = 'A'clicked execute buttonUPDATE [TABLE] SET Weight = 601 WHERE [Product ID] = 'B'clicked execute buttonThat's what I meant, one at a time.
Bob
Place GO in between
rdkleine
It doesn't work for the GO in between. Prompted with the same error msg.
Bob
Which version of SQL Server are you using? Do you use the management studio of SQL server?
rdkleine
I'm using WinSQL Lite.
Bob
rdkleine