Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Update table using case statement?? 1

Status
Not open for further replies.

awise

IS-IT--Management
Joined
Dec 11, 2001
Messages
85
I have the following sql script that I am trying to use to update a field:

update rm00101
case a.code
when 1 set b.phone3='0'
when 5 set b.phone3='A'
when 3 set b.phone3='U'
when 4 set b.phone3='U'
when 0 set b.phone3='1'
end as b.phone3
from ageverifyresult a, rm00101 b
where a.custnmbr = b.custid

It fails on the second line, right after I start the case statement.

First question - is it possible to update a table's field using a case statement in this manner? If so, what is the proper syntax to accomplish this? If not, what is the recommended alternative?

Thanks,

Andrew
 
what do you mean it fails? Does it not compile, or does if fail during exectution?
 
Try to add this line:

update rm00101
SET FIELD_NAME =
case a.code
when 1 set b.phone3='0'
when 5 set b.phone3='A'
when 3 set b.phone3='U'
when 4 set b.phone3='U'
when 0 set b.phone3='1'
end as b.phone3
from ageverifyresult a, rm00101 b
where a.custnmbr = b.custid

This is what you've missed...

M.
 
This is the exact message when I execute the query:

Server: Msg 156, Level 15, State 1, Line 2
Incorrect syntax near the keyword 'case'.
 
I'm not real sure about the syntax, but try this instead.

Code:
update b 
Set b.Phone3 =  case a.code
                when 1 then '0'
                when 5 then 'A'
                when 3 then 'U'
                when 4 then 'U'
                when 0 then '1'
                end 
from ageverifyresult a, rm00101 b
    where a.custnmbr = b.custid

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Micha123,

After I tried your addition, it got past line 2, but then gave the following on "set" in line 4:

Server: Msg 156, Level 15, State 1, Line 4
Incorrect syntax near the keyword 'set'.

 
George, many thanks that worked flawlessly.

Andrew
 
glad to help.

-George

Strong and bitter words indicate a weak cause. - Fortune cookie wisdom
 
Yeah... I missed the "set" in the case too....
(Need my glasses...?)

Mmmm... [shadessad]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top