472,122 Members | 1,463 Online

# Case statement

Hi guys,
I have a doubt regarding the CASE statement. It might sound silly, but me being new to DB2, it is kind of a genuine doubt. Try helping me with this..

When we use a case statement (simple-case-statement-when-clause), in this -
the value of the expression prior to the first WHEN keyword is tested for equality with the value of each expression that follows the WHEN keyword. Right?
Then if the search condition is true, the THEN statement is executed. If the result is unknown or false, processing continues to the next search condition. If the result does not match any of the search conditions, and an ELSE clause is present, the statements in the ELSE clause are processed.

Now my doubt is, suppose the expression satisfies two conditions of the case statement, then will it execute the first one that satisfies? or will it execute all of them that do?
for eg.
A = 57;
B=0;
case when A <= 60 then B = 1
when A <= 80 then B = 2
else B = 3
end
In this case, B would first be set to 1. But then would it also reset to 2 after the case statement has been processed?

Apr 25 '07 #1
1 4850
kalexin
20
Hi guys,
I have a doubt regarding the CASE statement. It might sound silly, but me being new to DB2, it is kind of a genuine doubt. Try helping me with this..

When we use a case statement (simple-case-statement-when-clause), in this -
the value of the expression prior to the first WHEN keyword is tested for equality with the value of each expression that follows the WHEN keyword. Right?
Then if the search condition is true, the THEN statement is executed. If the result is unknown or false, processing continues to the next search condition. If the result does not match any of the search conditions, and an ELSE clause is present, the statements in the ELSE clause are processed.

Now my doubt is, suppose the expression satisfies two conditions of the case statement, then will it execute the first one that satisfies? or will it execute all of them that do?
for eg.
A = 57;
B=0;
case when A <= 60 then B = 1
when A <= 80 then B = 2
else B = 3
end
In this case, B would first be set to 1. But then would it also reset to 2 after the case statement has been processed?

Hi,
You may want to use a BREAK to exit the CASE statement. If you don't have a BREAK, the loop will continue all the way down.
Apr 26 '07 #2