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
 SQL Server 2000 Forums
 Transact-SQL (2000)
 UPDATE table using CASE?

Author  Topic 

jp2code
Posting Yak Master

175 Posts

Posted - 2009-03-06 : 12:38:14
Can I update a table with a CASE statement in it? Either I can't do it or I have a syntax error.
declare @WorkOrder_Number char(10), @WorkOrder_Seq int
set @WorkOrder_Number='xr1200'
set @WorkOrder_Seq=0
UPDATE ACP_Parts SET WorkOrder_Qty=WorkOrder_Qty - 1,
CASE WHEN 0 < @WorkOrder_Seq THEN WorkOrder_Seq=WorkOrder_Seq - 1 ELSE WorkOrder_Seq=WorkOrder_Seq END
WHERE CASE WHEN 0 < @WorkOrder_Seq
THEN (WorkOrder_Number=@WorkOrder_Number) AND (System_ID Like '%Label%') AND (@WorkOrder_Seq < WorkOrder_Seq) END
ELSE (WorkOrder_Number=@WorkOrder_Number) AND (System_ID Like '%Label%') END

Query Analyzer only says, "Incorrect syntax near the keyword 'CASE'." and points me to the first use of the CASE (in the Update clause, not the Where clause).

Thanks in advance for your help!
~Joe


Avoid Sears Home Improvement

guptam
Posting Yak Master

161 Posts

Posted - 2009-03-06 : 12:46:08
Try ..

DECLARE @WorkOrder_Number char(10), @WorkOrder_Seq int
SET @WorkOrder_Number='xr1200'
SET @WorkOrder_Seq=0

UPDATE ACP_Parts
SET WorkOrder_Qty=WorkOrder_Qty - 1,
WorkOrder_Seq=(CASE WHEN 0 < @WorkOrder_Seq THEN
WorkOrder_Seq - 1
ELSE
WorkOrder_Seq
END)
WHERE CASE WHEN 0 < @WorkOrder_Seq
THEN (WorkOrder_Number=@WorkOrder_Number) AND (System_ID Like '%Label%') AND (@WorkOrder_Seq < WorkOrder_Seq) END
ELSE (WorkOrder_Number=@WorkOrder_Number) AND (System_ID Like '%Label%') END


--
Mohit K. Gupta
B.Sc. CS, Minor Japanese
MCITP: Database Administrator
MCTS: SQL Server 2005
http://sqllearnings.blogspot.com/
Go to Top of Page

jp2code
Posting Yak Master

175 Posts

Posted - 2009-03-06 : 13:06:42
Thanks!


Avoid Sears Home Improvement
Go to Top of Page
   

- Advertisement -