1IF (Expression 1)
2BEGIN
3 Statement 1;
4END
5
6ELSE IF (Expression 2)
7BEGIN
8 Statement 2;
9END
10..........
11ELSE
12BEGIN
13 Default Statement;
14END
1-- PL/SQL
2BEGIN
3 IF my_val = 1 THEN [...]
4 ELSE [...]
5 END IF;
6END;
7-- In a query (DUAL is for Oracle)
8SELECT CASE WHEN my_col = 1 THEN 'Ok' ELSE 'Ko' END AS my_result
9FROM DUAL;
1IF Boolean_expression
2BEGIN
3 -- Statement block executes when the Boolean expression is TRUE
4END
5ELSE
6BEGIN
7 -- Statement block executes when the Boolean expression is FALSE
8END
1Case Statement basically
2Like IF - THEN - ELSE statement.
3
4The CASE statement goes through conditions
5and returns a value when the
6first condition is met and
7once a condition is true,
8it will stop reading and return the result.
9If no conditions are true,
10it returns the value in the ELSE clause.
11
12If there is no ELSE part and
13no conditions are true, it returns NULL.
14
15FOR EXAMPLE =
16
17CASE
18 WHEN condition1 THEN result1
19 WHEN condition2 THEN result2
20 WHEN conditionN THEN resultN
21 ELSE result
22END
23
24-- example:
25SELECT
26 CASE
27 WHEN (1+6 = 6) THEN 'A'
28 WHEN (1+6 = 7) THEN 'B'
29 WHEN (1+6 = 8) THEN 'C'
30 ELSE 'D'
31 END
32FROM DUAL;
33
34Result would be 'B' since it is the first
35correct answer
1IF 1=1
2 SELECT 1
3ELSE
4 SELECT 0
5-- returns 1
6
7-- Definition
8IF Boolean_expression
9 { sql_statement | statement_block }
10[ ELSE
11 { sql_statement | statement_block } ]
1SELECT CASE
2 WHEN A + B > C AND B + C > A AND A + C > B THEN
3 CASE
4 WHEN A = B AND B = C THEN 'Equilateral'
5 WHEN A = B OR B = C OR A = C THEN 'Isosceles'
6 ELSE 'Scalene'
7 END
8 ELSE 'Not A Triangle'
9 END
10FROM TRIANGLES;
11