SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Want to run update query for all rows in tbl
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

cplusplus
Aged Yak Warrior

536 Posts

Posted - 08/30/2013 :  07:09:21  Show Profile  Reply with Quote
I want to run the below update on all rows in the table lw_pos_chg, it has 9k rows.
want to fill pos_stat_desc column value based on pos_stat value in the same table.


Update lw_pos_chg
if pos_stat=1 then set pos_stat_desc='Open'
else if pos_stat = 2 then set pos_stat_desc= 'in progress'
else if pos_stat = 3 then set pos_stat_desc= 'closed'
else set pos_stat_desc= 'Suspend'


Thank you very much for the helpful info.

James K
Flowing Fount of Yak Knowledge

3589 Posts

Posted - 08/30/2013 :  08:19:51  Show Profile  Reply with Quote
The IF construct is used for control flow. You cannot use that in this context. Instead use a case expression like shown below:
UPDATE lw_poschg SET
	pos_stat_desc =
		CASE 
			WHEN pos_stat = 1 THEN 'Open'
			WHEN pos_stat = 2 THEN 'in progress'
			WHEN pos_stat = 3 THEN 'closed'
			ELSE 'Suspend'
		END;
Go to Top of Page

VeeranjaneyuluAnnapureddy
Posting Yak Master

India
165 Posts

Posted - 08/30/2013 :  09:21:43  Show Profile  Reply with Quote
quote:
Originally posted by James K

The IF construct is used for control flow. You cannot use that in this context. Instead use a case expression like shown below:


UPDATE lw_poschg 
SET pos_stat_desc =     CASE 
			WHEN pos_stat = 1 THEN 'Open'
			WHEN pos_stat = 2 THEN 'in progress'
			WHEN pos_stat = 3 THEN 'closed'
			ELSE 'Suspend'
		END;


Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000