Subject Re: [firebird-support] CASE WHEN error
Author Helen Borrie
At 04:15 AM 25/11/2006, you wrote:
>Thank you for your interest. I don't have enough experience about
>Firebird SQL. My graduation project was about a law office program.
>I didn't have any problem during the presentation. Now I have to
>deal with that program again but it gives an error about CASE WHEN statement.
>
>Tables' names are in Turkish, so I think its better to write the
>table names first, in order to help you to understand.

It's also simpler when using joins to employ table aliases...


>Table Name: Fields:
>
>HESAPLAR HESAP_ID
> HESAP_TARIHI
> HESAP_SEC_TABLO
> HESAP_SEC_TAB_ID
>PERSONEL_KART PER_ID
> PER_ISIM
> PER_SADI
>MUSTERI_KARTI MUS_ID
> MUS_ISIM
> MUR_SADI
>
>
>And this is the code that gives error at CASE WHEN statement:

You don't say what kind of error is occurring. "..gives an error.."
is too unspecific to be useful in troubleshooting anything.

>select DISTINCT HESAPLAR.HESAP_ID,
> HESAPLAR.HESAP_TARIHI,HESAPLAR.hesap_sec_tablo,
> HESAPLAR.HESAP_SEC_TAB_ID,
> CASE when hesaplar.hesap_sec_tablo = 0
> then (select trim(PERSONEL_KART.PER_ISIM) ||'
> '||trim(PERSONEL_KART.PER_SADI)
> from personel_kart
> where personel_kart.per_id=hesaplar.hesap_sec_tab_id)
> else (select trim(MUSTERI_KARTI.MUS_ISIM) ||'
> '||trim(MUSTERI_KARTI.MUS_SADI)
> from musteri_karti
> where musteri_karti.mus_id =hesaplar.hesap_sec_tab_id)
> end as kisi,hesaplar.hesap_aciklama,hesaplar.hesap_dekonttur,
>HESAPLAR.HESAP_TUR_ID,hesaplar.hesap_debit,hesaplar.hesap_credit
>case when hesaplar.hesap_sec_tablo = 0 then ''P'' Else ''M'' end as ST
>from hesaplar

The most obvious errors here are

1) Syntax: a missing comma after the penultimate output field and

2) Syntax: the use of double pairs of single quotes for the outputs
'P' and 'M'. If you want the output to be simply the character, use
just one pair of single quotes, to delimit them as strings. If you
want the output to include literal single quotes (as apostrophes),
you need THREE pairs: the outer pair as the string delimiter plus
one preceding each literal apostrophe (referred to as "doubling")

3) Run-time: you potentially have a run-time error through your use
of subqueries in your CASE predications, too, if your subqueries
happen to return multiple results (subquery expressions MUST return a
singleton). However, if m.MUS_ID and pk.PER_ID are unique, there is
no risk of a run-time error from these subqueries.

4) Run-time: if h.hesap_sec_tablo is not constrained by NOT NULL, you
have a potential for your CASE conditions to return WRONG results

select DISTINCT
h.HESAP_ID,
h.HESAP_TARIHI,
h.hesap_sec_tablo,
h.HESAP_SEC_TAB_ID,
CASE when h.hesap_sec_tablo = 0
then
(select trim(pk.PER_ISIM) ||' '||trim(pk.PER_SADI)
from personel_kart pk
where pk.per_id=h.hesap_sec_tab_id)
else
(select trim(mk.MUS_ISIM) ||' '||trim(mk.MUS_SADI)
from musteri_karti mk
where mk.mus_id =h.hesap_sec_tab_id)
end as kisi,
h.hesap_aciklama,
h.hesap_dekonttur,
h.HESAP_TUR_ID,
h.hesap_debit,
h.hesap_credit, /* <<----- MISSING COMMA */
case when h.hesap_sec_tablo = 0 then 'P' Else 'M' end as ST
from hesaplar h

..or, if you want to return literal quotes on 'P' or 'M':

case when h.hesap_sec_tablo = 0 then '''P''' Else '''M''' end as ST

./heLen