Home All Groups Group Topic Archive Search About

Case statement question

Author
21 Jul 2006 2:03 PM
Goran Djuranovic
Hi All,
Is it possible to update a specific field based on the case statement?
Something like this, for example:

UPDATE Person SET
CASE
    WHEN HB = '1' THEN HasBrother = '1'
    WHEN HS = '1' THEN HasSister = '1'
END
FROM <Some other table>
......


I know one way is to specify both fields and have CASE statement for both of them, something like:
UPDATE Person SET
HasBrother = CASE
                        WHEN HB = '1' THEN '1'
                        ELSE HasBrother
                    END
HasSister = CASE
                        WHEN HB = '1' THEN '1'
                        ELSE HasSister
                  END
FROM <Some other table>
.......

But I am curious if first examle was possible.

Thanks
Goran Djuranovic

Author
21 Jul 2006 2:13 PM
ivan
Vidim po imenu da ces me razumeti i ako ti napisem na srpskom. Mozes da
napises nesto kao:

UPDATE Person
SET HasBrother = CASE HB = '1' THEN '1' ELSE HasBrother END,
     HasSister = CASE HS = '1' THEN '1' ELSE HasSister END
FROM ... ...

Tako ce ti promeniti polje u 1 ako je zadovoljen uslov, a inace ce
ostati isto. Nije bas to sto si trazio, ali zavrsava posao.

Pozdrav,
Ivan

Goran Djuranovic wrote:
Show quote
> Hi All,
> Is it possible to update a specific field based on the case statement?
> Something like this, for example:
>
> UPDATE Person SET
> CASE
>     WHEN HB = '1' THEN HasBrother = '1'
>     WHEN HS = '1' THEN HasSister = '1'
> END
> FROM <Some other table>
> .....
>
>
> I know one way is to specify both fields and have CASE statement for both of them, something like:
> UPDATE Person SET
> HasBrother = CASE
>                         WHEN HB = '1' THEN '1'
>                         ELSE HasBrother
>                     END
> HasSister = CASE
>                         WHEN HB = '1' THEN '1'
>                         ELSE HasSister
>                   END
> FROM <Some other table>
> ......
>
> But I am curious if first examle was possible.
>
> Thanks
> Goran Djuranovic
>
> ------=_NextPart_000_0006_01C6ACAC.E6750960
> Content-Type: text/html; charset=iso-8859-1
> Content-Transfer-Encoding: quoted-printable
> X-Google-AttachSize: 3247
>
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
> <HTML><HEAD>
> <META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
> <META content="MSHTML 6.00.2900.2912" name=GENERATOR>
> <STYLE></STYLE>
> </HEAD>
> <BODY bgColor=#ffffff>
> <DIV><FONT face=Arial size=2>Hi All,</FONT></DIV>
> <DIV><FONT face=Arial size=2>Is it possible to update a specific field based on
> the case statement?</FONT></DIV>
> <DIV><FONT face=Arial size=2>Something like this, for example:</FONT></DIV>
> <DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
> <DIV><FONT face=Arial size=2>UPDATE Person SET</FONT></DIV>
> <DIV><FONT face=Arial size=2>CASE</FONT></DIV>
> <DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp; WHEN HB = '1'
> THEN&nbsp;HasBrother = '1'</FONT></DIV>
> <DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp; WHEN HS = '1'
> THEN&nbsp;HasSister = '1'</FONT></DIV>
> <DIV><FONT face=Arial size=2>END</FONT></DIV>
> <DIV><FONT face=Arial size=2>FROM &lt;Some other table&gt;</FONT></DIV>
> <DIV><FONT face=Arial size=2>.....</FONT></DIV>
> <DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
> <DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
> <DIV><FONT face=Arial size=2>I know one way is to specify both fields and have
> CASE statement for both of them, something like:</FONT></DIV>
> <DIV><FONT face=Arial size=2>
> <DIV><FONT face=Arial size=2>UPDATE Person SET</FONT></DIV>
> <DIV>HasBrother = <FONT face=Arial size=2>CASE</FONT>
> <DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; WHEN
> HB = '1' THEN&nbsp;'1'</FONT></DIV>
> <DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; ELSE
> HasBrother</FONT></DIV>
> <DIV><FONT face=Arial
> size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;END</FONT></DIV>
> <DIV>HasSister = <FONT face=Arial size=2>CASE</FONT>
> <DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; WHEN
> HB = '1' THEN&nbsp;'1'</FONT></DIV>
> <DIV><FONT face=Arial
> size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
> ELSE HasSister</FONT></DIV>
> <DIV><FONT face=Arial
> size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;END</FONT></DIV></DIV></DIV>
> <DIV><FONT face=Arial size=2>FROM &lt;Some other
> table&gt;</FONT></DIV></FONT></DIV>
> <DIV><FONT face=Arial size=2>......</FONT></DIV>
> <DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
> <DIV><FONT face=Arial size=2>But I am curious&nbsp;if first examle was
> possible.</FONT></DIV>
> <DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
> <DIV><FONT face=Arial size=2>Thanks</FONT></DIV>
> <DIV><FONT face=Arial size=2>Goran Djuranovic</FONT></DIV>
> <DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV></BODY></HTML>
>
> ------=_NextPart_000_0006_01C6ACAC.E6750960--
Author
21 Jul 2006 6:06 PM
Goran Djuranovic
Hvala Ivane. Srpski odlicno pricam i jos bolje razumijem :-).
Ako procitas ponovo moje pitanje, vidjeces da sam tvoju soluciju vec naveo u
istom, i da me interesuje da li ista solucija moze da se napise u nekom
drugom smislu. Mada, cisto sumnjam.

Jos jednom hvala.
Goran

Show quote
"ivan" <ivanmilo***@gmail.com> wrote in message
news:1153491208.641745.74970@75g2000cwc.googlegroups.com...
> Vidim po imenu da ces me razumeti i ako ti napisem na srpskom. Mozes da
> napises nesto kao:
>
> UPDATE Person
> SET HasBrother = CASE HB = '1' THEN '1' ELSE HasBrother END,
>     HasSister = CASE HS = '1' THEN '1' ELSE HasSister END
> FROM ... ...
>
> Tako ce ti promeniti polje u 1 ako je zadovoljen uslov, a inace ce
> ostati isto. Nije bas to sto si trazio, ali zavrsava posao.
>
> Pozdrav,
> Ivan
>
> Goran Djuranovic wrote:
>> Hi All,
>> Is it possible to update a specific field based on the case statement?
>> Something like this, for example:
>>
>> UPDATE Person SET
>> CASE
>>     WHEN HB = '1' THEN HasBrother = '1'
>>     WHEN HS = '1' THEN HasSister = '1'
>> END
>> FROM <Some other table>
>> .....
>>
>>
>> I know one way is to specify both fields and have CASE statement for both
>> of them, something like:
>> UPDATE Person SET
>> HasBrother = CASE
>>                         WHEN HB = '1' THEN '1'
>>                         ELSE HasBrother
>>                     END
>> HasSister = CASE
>>                         WHEN HB = '1' THEN '1'
>>                         ELSE HasSister
>>                   END
>> FROM <Some other table>
>> ......
>>
>> But I am curious if first examle was possible.
>>
>> Thanks
>> Goran Djuranovic
>>
>> ------=_NextPart_000_0006_01C6ACAC.E6750960
>> Content-Type: text/html; charset=iso-8859-1
>> Content-Transfer-Encoding: quoted-printable
>> X-Google-AttachSize: 3247
>>
>> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
>> <HTML><HEAD>
>> <META http-equiv=Content-Type content="text/html; charset=iso-8859-1">
>> <META content="MSHTML 6.00.2900.2912" name=GENERATOR>
>> <STYLE></STYLE>
>> </HEAD>
>> <BODY bgColor=#ffffff>
>> <DIV><FONT face=Arial size=2>Hi All,</FONT></DIV>
>> <DIV><FONT face=Arial size=2>Is it possible to update a specific field
>> based on
>> the case statement?</FONT></DIV>
>> <DIV><FONT face=Arial size=2>Something like this, for
>> example:</FONT></DIV>
>> <DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
>> <DIV><FONT face=Arial size=2>UPDATE Person SET</FONT></DIV>
>> <DIV><FONT face=Arial size=2>CASE</FONT></DIV>
>> <DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp; WHEN HB = '1'
>> THEN&nbsp;HasBrother = '1'</FONT></DIV>
>> <DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp; WHEN HS = '1'
>> THEN&nbsp;HasSister = '1'</FONT></DIV>
>> <DIV><FONT face=Arial size=2>END</FONT></DIV>
>> <DIV><FONT face=Arial size=2>FROM &lt;Some other table&gt;</FONT></DIV>
>> <DIV><FONT face=Arial size=2>.....</FONT></DIV>
>> <DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
>> <DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
>> <DIV><FONT face=Arial size=2>I know one way is to specify both fields and
>> have
>> CASE statement for both of them, something like:</FONT></DIV>
>> <DIV><FONT face=Arial size=2>
>> <DIV><FONT face=Arial size=2>UPDATE Person SET</FONT></DIV>
>> <DIV>HasBrother = <FONT face=Arial size=2>CASE</FONT>
>> <DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
>> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
>> &nbsp;&nbsp;&nbsp; WHEN
>> HB = '1' THEN&nbsp;'1'</FONT></DIV>
>> <DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
>> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
>> &nbsp;&nbsp;&nbsp; ELSE
>> HasBrother</FONT></DIV>
>> <DIV><FONT face=Arial
>> size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;END</FONT></DIV>
>> <DIV>HasSister = <FONT face=Arial size=2>CASE</FONT>
>> <DIV><FONT face=Arial size=2>&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
>> &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
>> &nbsp;&nbsp;&nbsp; WHEN
>> HB = '1' THEN&nbsp;'1'</FONT></DIV>
>> <DIV><FONT face=Arial
>> size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
>> ELSE HasSister</FONT></DIV>
>> <DIV><FONT face=Arial
>> size=2>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;END</FONT></DIV></DIV></DIV>
>> <DIV><FONT face=Arial size=2>FROM &lt;Some other
>> table&gt;</FONT></DIV></FONT></DIV>
>> <DIV><FONT face=Arial size=2>......</FONT></DIV>
>> <DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
>> <DIV><FONT face=Arial size=2>But I am curious&nbsp;if first examle was
>> possible.</FONT></DIV>
>> <DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV>
>> <DIV><FONT face=Arial size=2>Thanks</FONT></DIV>
>> <DIV><FONT face=Arial size=2>Goran Djuranovic</FONT></DIV>
>> <DIV><FONT face=Arial size=2></FONT>&nbsp;</DIV></BODY></HTML>
>>
>> ------=_NextPart_000_0006_01C6ACAC.E6750960--
>
Author
21 Jul 2006 4:15 PM
--CELKO--
CASE is an expression and not a statement.  It must return a scalar
value.
Author
21 Jul 2006 6:20 PM
Goran Djuranovic
Celko,
If the CASE function cannot be used for the solution to my previous
question, is there any other way of doing it. Of course, other than
dynamically building the SQL statement.

TIA
Goran


Show quote
"--CELKO--" <jcelko***@earthlink.net> wrote in message
news:1153498545.345423.68640@75g2000cwc.googlegroups.com...
> CASE is an expression and not a statement.  It must return a scalar
> value.
>
Author
22 Jul 2006 3:26 AM
--CELKO--
>> If the CASE function cannot be used for the solution to my previous question, is there any other way of doing it. <<

Better schema design and encodings!  SQL is a data language and not a
procedural language.

CREATE TABLE Persons
( ..
sibling_status DEFAULT 0 INTEGER NOT NULL
    CHECK (sibling_status IN (0, 1, 2, 3)),
...);

0=  no siblings
1= has brothers
2= has sisters
3=  has brothers and sisters

>>  Of course, other than dynamically building the SQL statement. <<

Writing dynamic SQL is like playhing a video game where you have no
idea what will happen next, so you just start shooting at the problem
at run time :)
Author
26 Jul 2006 1:33 PM
privatenews
Hello Goran,

I agree with Hugo that it is not possible for the first example and the
workaround you have provided seems to be the best option. If you still have
concerns on this, please feel free to let's know.

Best Regards,

Peter Yang
MCSE2000/2003, MCSA, MCDBA
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications
<http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx>.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
<http://msdn.microsoft.com/subscriptions/support/default.aspx>.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Author
21 Jul 2006 8:55 PM
Hugo Kornelis
On Fri, 21 Jul 2006 10:03:22 -0400, Goran Djuranovic wrote:

>Hi All,
>Is it possible to update a specific field based on the case statement?
>Something like this, for example:
>
>UPDATE Person SET
>CASE
>    WHEN HB = '1' THEN HasBrother = '1'
>    WHEN HS = '1' THEN HasSister = '1'
>END
>FROM <Some other table>
>.....
(snip)

Hi Goran,

I have no idea what Ivan has been writing to you (it looks like a Slavic
language, but there are many of those, and I know none of them). But if
his answer is that this is not possible, then he is correct. You'll have
to use the method that yoou've already found yourself:

>UPDATE Person SET
>HasBrother = CASE
>                        WHEN HB = '1' THEN '1'
>                        ELSE HasBrother
>                    END
>HasSister = CASE
>                        WHEN HB = '1' THEN '1'
>                        ELSE HasSister
>                  END
>FROM <Some other table>
>......


--
Hugo Kornelis, SQL Server MVP

AddThis Social Bookmark Button