MS SQL 2005 Update/Set/Case

I am trying to do away with dynamic sql that i have been using in a stored procedure to update columns dynamicly
 i have converted the selects to use case without any problems, but the updates are proving a little more difficult

i am getting a syntax error near the keyword Case with the following code

can someone help me out with this
Thank you
I
declare @col varchar (max)
set @col = 'one'
update activity_history set  CASE @col
 
           WHEN 'one'  THEN one
           WHEN 'two'  THEN two
           WHEN 'three'  THEN three
           WHEN 'four'  THEN four
           WHEN 'five'  THEN five
           WHEN 'six'  THEN six
           WHEN 'seven'  THEN seven
           WHEN 'eight'  THEN eight
           WHEN 'nine'  THEN nine
           WHEN 'ten'  THEN ten
           WHEN 'eleven'  THEN eleven
           WHEN 'twelve'  THEN twelve
           WHEN 'thirteen'  THEN thirteen
           WHEN 'fourteen'  THEN fourteen
           WHEN 'fifteen'  THEN fifteen
           WHEN 'sixteen'  THEN sixteen
           WHEN 'seventeen'  THEN seventeen
           WHEN 'eighteen'  THEN eighteen
           WHEN 'nineteen'  THEN nineteen
           WHEN 'twenty'  THEN twenty
           WHEN 'twentyone'  THEN twentyone
           WHEN 'twentytwo'  THEN twentytwo
           WHEN 'twentythree'  THEN twentythree
           else  twentyfour
end

= 4

Open in new window

LVL 3
QuinnDesterAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Guy Hengel [angelIII / a3]Billing EngineerCommented:
you will need to do like this:
max)
set @col = 'one'

update activity_history 
set one = CASE @col WHEN 'one' then 4 else one end
  , two = CASE @col WHEN 'two' then 4 else two end
 ... etc ..

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
QuinnDesterAuthor Commented:
how do i do the

else  twentyfour

there is only one value i cant easily identify, the else twentyfour is the perfect solution to it
QuinnDesterAuthor Commented:
can i update 2 columns like that, as they are related and the count column will need incrementing when ever the main column is updated..

would this be the right syntax

        one  = CASE @col WHEN 'one'  THEN one  + @status, count1 = count1 + 1 else one, count1 end
        ,two  = CASE @col WHEN 'two'  THEN two  + @status, count2 = count2 + 1  else two, count2 end
The 7 Worst Nightmares of a Sysadmin

Fear not! To defend your business’ IT systems we’re going to shine a light on the seven most sinister terrors that haunt sysadmins. That way you can be sure there’s nothing in your stack waiting to go bump in the night.

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
No. Case is like a function, not a procedure or control statement (no IF THEN ELSE), and you can only have one expression evaluated per THEN or ELSE. Maybe you should give more background, so we can suggest something appropriate.
JoeNuvoCommented:
if update 2 columns
then it will be

update activity_history
set
	one		= one + CASE @col WHEN 'one' THEN @status ELSE 0 END,
	two		= two + CASE @col WHEN 'two' THEN @status ELSE 0 END,
	...
	count1	= count1 + CASE @col WHEN 'one' THEN 1 ELSE 0 END,
	count2	= count2 + CASE @col WHEN 'two' THEN 1 ELSE 0 END,
	...

Open in new window

QuinnDesterAuthor Commented:
thanks... i have used the case on the selects, but the update statment using case was turning into much more code then using if and if statment like this

        if @col = 'one'  begin update activity_history set one = one + @status, count1 = count1 + 1 where Coordinates = @Coordinates end
        if @col = 'two'  begin update activity_history set two  = two  + @status, count2 = count2 + 1 where Coordinates = @Coordinates end

out of curiosity, am i correct in thinking that using a case like that in an update statment it will execute every line of code, if it doesnt match it updates with the else..

where using the if, if it doesnt match it skips the code and goes on to the next if.
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Correct. Since the case only results in expressions, all cases (but not all when and else branches) need to be processed.
In your case, where @col can only be of one single state, the IF approach is better. Or using a dynamic SQL - but that can get a mess if you need to insert string literals as literals and numerical values as string ...
QuinnDesterAuthor Commented:
my bigest problem with dynaimic sql was being able to use variables that needed setting using the dynamic sql... or even reusing ones already set after dynaimc sql had executed,
QuinnDesterAuthor Commented:
shared the points as all comments helped me arrive at my solution, thank you for your input
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.