0% found this document useful (0 votes)
152 views6 pages

Ex No: 6 Program For If, Nested If and CASE Expression

The document discusses different conditional constructs in SQL including IF, nested IF, CASE, and various functions like COALESCE and NULLIF. It provides syntax examples and sample code to demonstrate how to use IF/ELSE, nested IF, CASE statements, and the COALESCE and NULLIF functions to implement conditional logic in SQL. Sample data is also inserted into a PLAYERS table to showcase the use of these constructs.

Uploaded by

Chidambaram
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
152 views6 pages

Ex No: 6 Program For If, Nested If and CASE Expression

The document discusses different conditional constructs in SQL including IF, nested IF, CASE, and various functions like COALESCE and NULLIF. It provides syntax examples and sample code to demonstrate how to use IF/ELSE, nested IF, CASE statements, and the COALESCE and NULLIF functions to implement conditional logic in SQL. Sample data is also inserted into a PLAYERS table to showcase the use of these constructs.

Uploaded by

Chidambaram
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Ex no : 6

Program for If, nested if and CASE expression

implements a basic conditional construct. If the search_condition evaluates to true,

IF

the corresponding SQL statement list is executed. If no search_condition matches, the


statement list in the ELSE clause is executed. Each statement_list consists of one or
more statements.

IF Syntax
IF search_condition THEN statement_list
[ELSEIF search_condition THEN statement_list] ...
[ELSE statement_list]
END IF
Example 1:
DELIMITER //
CREATE FUNCTION SimpleCompare(n INT, m INT)
RETURNS VARCHAR(20)
BEGIN
DECLARE s VARCHAR(20);
IF n > m THEN SET s = '>';
ELSEIF n = m THEN SET s = '=';
ELSE SET s = '<';
END IF;
SET s = CONCAT(n, ' ', s, ' ', m);
RETURN s;
END //
DELIMITER ;

Example 2 : Nested If
//
create function iffunction(p_comp VARCHAR(10),p_divi VARCHAR(10))
returns VARCHAR(10)
begin
declare l_value VARCHAR(30);
if p_comp = ACME then
if p_divi = SALES then
set l_value := You entered ACME Sales;
elseif p_divi = HR then
set l_value := You entered ACME HR;
elseif p_divi = IT then
set l_value := You entered ACME IT;
end if;
elseif p_comp = CORP then
if p_divi = SALES then
set l_value := You entered CORP Sales;
elseif p_divi = HR then
set l_value := You entered CORP HR;
elseif p_divi = IT then
set l_value := You entered CORP IT;
end if;
end if;
return l_value;
end
//

CASE Syntax
The CASE statement for stored programs implements a complex conditional construct.
CASE case_value
WHEN when_value THEN statement_list
[WHEN when_value THEN statement_list] ...
[ELSE statement_list]
END CASE
[or]
CASE
WHEN search_condition THEN statement_list
[WHEN search_condition THEN statement_list] ...
[ELSE statement_list]
END CASE

Example 3:
mysql> DELIMITER //
mysql> CREATE FUNCTION myFunction(delivery_day INT(1),preferred INT(1))
-> RETURNS INT(2)
-> BEGIN
->
-> DECLARE shipping_cost INT(2) DEFAULT 0;
->
-> CASE
-> WHEN preferred = 1 THEN
->
SET shipping_cost = 2;
-> WHEN delivery_day = 1 THEN
->
SET shipping_cost = 20;
-> WHEN delivery_day = 2 THEN
->
SET shipping_cost = 15;
-> WHEN delivery_day = 3 THEN
->
SET shipping_cost = 10;
-> ELSE
->
SET shipping_cost = 5;
-> END CASE;
-> RETURN shipping_cost;
->
-> END
-> //
Query OK, 0 rows affected (0.01 sec)
mysql> DELIMITER ;
mysql> select myFunction(1,1);
+-----------------+
| myFunction(1,1) |
+-----------------+
|
2 |
+-----------------+
1 row in set (0.00 sec)

mysql> select myFunction(2,2);


+-----------------+
| myFunction(2,2) |
+-----------------+
|
15 |
+-----------------+
1 row in set (0.00 sec)

mysql> select myFunction(3,3);


+-----------------+
| myFunction(3,3) |
+-----------------+
|
10 |
+-----------------+
1 row in set (0.00 sec)

mysql> drop function myFunction;


Query OK, 0 rows affected (0.00 sec)

mysql> CREATE TABLE PLAYERS


-> (
->
PLAYERNO INTEGER NOT NULL,
->
NAME CHAR(15) NOT NULL,
->
INITIALS CHAR(3) NOT NULL,
->
BIRTH_DATE DATE ,
->
SEX CHAR(1) NOT NULL,
->
JOINED SMALLINT NOT NULL,
->
STREET VARCHAR(30) NOT NULL,
->
HOUSENO CHAR(4) ,
->
POSTCODE CHAR(6) ,
->
TOWN VARCHAR(30) NOT NULL,
->
PHONENO CHAR(13) ,
->
LEAGUENO CHAR(4) ,
->
PRIMARY KEY (PLAYERNO)
-> );
Query OK, 0 rows affected (0.00 sec)
mysql> INSERT INTO PLAYERS VALUES (2, 'Everett', 'R', '1948-09-01', 'M',
1975, 'Stoney Road','43', '3575NH', 'Stratford'
, '070-237893', '2411');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO PLAYERS VALUES (6, 'Parmenter', 'R', '1964-06-25', '
M', 1977, 'Haseltine Lane','80', '1234KK', 'Strat
ford', '070-476537', '8467');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO PLAYERS VALUES (7, 'Wise', 'GWS', '1963-05-11', 'M',
1981, 'Edgecombe Way','39', '9758VB', 'Stratford
', '070-347689', NULL);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO PLAYERS VALUES (8, 'Newcastle', 'B', '1962-07-08', '
F', 1980, 'Station Road','4', '6584WO', 'Inglewoo
d', '070-458458', '2983');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO PLAYERS VALUES (27, 'Collins', 'DD', '1964-12-28', '
F', 1983, 'Long DRay','804', '8457DK', 'Eltham',
'079-234857', '2513');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO PLAYERS VALUES (28, 'Collins', 'C', '1963-06-22', 'F
', 1983, 'Old Main Road','10', '1294QK', 'Midhurs
t', '010-659599', NULL);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO PLAYERS VALUES (39, 'Bishop', 'D', '1956-10-29', 'M',

1980, 'Eaton Square','78', '9629CD', 'Stratford


', '070-393435', NULL);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO PLAYERS VALUES (44, 'Baker', 'E', '1963-01-09', 'M',
1980, 'Lewis Street','23', '4444LJ', 'Inglewood'
, '070-368753', '1124');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO PLAYERS VALUES (57, 'Brown', 'M', '1971-08-17', 'M',
1985, 'Edgecombe Way','16', '4377CB', 'Stratford
', '070-473458', '6409');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO PLAYERS VALUES (83, 'Hope', 'PK', '1956-11-11', 'M',
1982, 'Magdalene Road','16A', '1812UP', 'Stratfo
rd', '070-353548', '1608');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO PLAYERS VALUES (95, 'Miller', 'P', '1963-05-14', 'M',
1972, 'High Street','33A', '5746OP', 'Douglas',
'070-867564', NULL);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO PLAYERS VALUES (100, 'Parmenter', 'P', '1963-02-28',
'M', 1979, 'Haseltine Lane','80', '6494SG', 'Str
atford', '070-494593', '6524');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO PLAYERS VALUES (104, 'Moorman', 'D', '1970-05-10', '
F', 1984, 'Stout Street','65', '9437AO', 'Eltham'
, '079-987571', '7060');
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO PLAYERS VALUES (112, 'Bailey', 'IP', '1963-10-01', '
F', 1984, 'Vixen Road','8', '6392LK', 'Plymouth',
'010-548745', '1319');
Query OK, 1 row affected (0.00 sec)

COALESCE
Returns the first nonnull expression among its arguments.
Syntax
COALESCE ( expression [ ,...n ] )
If all arguments are NULL, COALESCE returns NULL.

COALESCE(expression1,...n) is equivalent to this CASE function:


CASE
WHEN (expression1 IS NOT NULL) THEN expression1
...
WHEN (expressionN IS NOT NULL) THEN expressionN
ELSE NULL
mysql> SELECT INITIALS, NAME, COALESCE(LEAGUENO, '1')
-> FROM PLAYERS;
+----------+-----------+-------------------------+
| INITIALS | NAME
| COALESCE(LEAGUENO, '1') |
+----------+-----------+-------------------------+
| R
| Everett
| 2411
|
| R
| Parmenter | 8467
|
| GWS
| Wise
| 1
|
| B
| Newcastle | 2983
|
| DD
| Collins
| 2513
|
| C
| Collins
| 1
|
| D
| Bishop
| 1
|
| E
| Baker
| 1124
|
| M
| Brown
| 6409
|
| PK
| Hope
| 1608
|
| P
| Miller
| 1
|
| P
| Parmenter | 6524
|
| D
| Moorman
| 7060
|
| IP
| Bailey
| 1319
|
+----------+-----------+-------------------------+
14 rows in set (0.00 sec)

The syntax for the NULLIF() function is as follows:


NULLIF(<expression1>, <expression2>)

mysql> SELECT NULLIF(10*20, 20*10);


+----------------------+
| NULLIF(10*20, 20*10) |
+----------------------+
|
NULL |
+----------------------+
1 row in set (0.00 sec)

You might also like