Subject Re: [firebird-support] Case statement in PSQL
Author Helen Borrie
At 04:49 p.m. 30/05/2013, Cam wrote:
>On 30/05/2013 2:19 PM, Helen Borrie wrote:
>> At 04:00 p.m. 30/05/2013, Cam wrote:
>>> Using version 2.5.2
>>> Is the case statement available in PSQL - specifically stored procedure.
>> Depends what you're asking for here. The DML CASE operator can be used in a stored procedure, as elsewhere.
>>
>> PSQL has no switching CASE statement construct such as you have in C++, Java, Delphi, etc.
>Thanks Helen
>The info here http://www.firebirdsql.org/refdocs/langrefupd15-case.html
>had me a bit confused on this issue

Is it the second example that's confusing? Let's put a bit of context around that:

create procedure aProc (Age SMALLINT)
returns (CanVote VARCHAR(6))
AS
BEGIN
CanVote = case
when Age >= 18 then 'Yes'
when Age < 18 then 'No'
else 'Unsure';
end

And yep, you could use the [switch] case construct in the host language to do something like that.

But what you can't do with the SQL CASE operator is use it like a host language [switch] CASE statement construct to branch the flow of control, such as:

case
when (Age >= 18) then
BEGIN
....
END
when (Age < 18) then
BEGIN
....
END
etc.

Actually, I think those examples and the description want looking at...it's waaay long ago since that was written (2003) and I have a strong suspicion I was responsible for it, originally...


Helen Borrie, Support Consultant, IBPhoenix (Pacific)
Author of "The Firebird Book" and "The Firebird Book Second Edition"
http://www.firebird-books.net
__________________________________________________________________