0% found this document useful (0 votes)
2 views

ex6

The document outlines the creation and execution of user-defined functions and stored procedures in SQL, detailing the syntax for PL/SQL blocks, control structures, and the definitions of procedures and functions. It includes examples of creating a table, altering it, and implementing a stored procedure for calculating interest based on account types, as well as a function to determine customer levels based on the duration of account ownership. The document concludes with successful execution results of the defined procedures and functions.

Uploaded by

mukeshsk1702
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

ex6

The document outlines the creation and execution of user-defined functions and stored procedures in SQL, detailing the syntax for PL/SQL blocks, control structures, and the definitions of procedures and functions. It includes examples of creating a table, altering it, and implementing a stored procedure for calculating interest based on account types, as well as a function to determine customer levels based on the duration of account ownership. The document concludes with successful execution results of the defined procedures and functions.

Uploaded by

mukeshsk1702
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Ex. No.

6
STORED PROCEDURES AND STORED FUNCTIONS

AIM
To write user defined functions and stored procedures in SQL.

BLOCKS in PL/SQL:
A block (or sub-block) lets you group logically related declarations and statements.

Syntax for defining a block:


[DECLARE
-- declarations]
BEGIN
-- statements
[EXCEPTION
-- handlers]
END;

Syntax for declarations :


variable_name data_type_declaration;

CONTROL STRUCTURES
IF Statements It is used to take alternative actions depending on circumstances. The IF statement lets
you execute a sequence of statements conditionally. There are three forms of IF statements: IF-THEN,
IF-THEN-ELSE, and IF-THEN-ELSIF.

Syntax:
IF condition1 THEN
sequence_of_statements1;
ELSEIF condition2 THEN
sequence_of_statements2;
ELSE
sequence_of_statements3;
END IF;

CASE Statements
The CASE statement is used to create complex conditional construct within stored programs. The
CASE statement cannot have an ELSE NULL clause, and it is terminated with END CASE instead of
END.
Syntax:
CASE case_value
WHEN when_value THEN
statement_list
[WHEN when_value THEN statement_list]
...
[ELSE statement_list]
END CASE

Iterative Control:

LOOP

LOOP is used to create repeated execution of the statement list.

Syntax:
LOOP
sequence_of_statements;
END LOOP;

The statements within the loop are repeated until the loop is terminated. Usually LEAVE statement is
used to exit the loop construct.

LEAVE statement
It is used to exit the flow control construct that has the given label. If the label is for the outermost
stored program block, LEAVE exits the program. LEAVE can be used within BEGIN ... END or loop
constructs (LOOP, REPEAT, WHILE).

Syntax:
LEAVE label

REPEAT statement
The REPEAT statement executes the statement(s) repeatedly as long as the condition is true. The
condition is checked every time at the end of the statements.

Syntax:
[begin_label:] REPEAT
statement_list
UNTIL search_condition
END REPEAT [end_label]

WHILE-LOOP
The WHILE-LOOP statement associates a condition with a sequence of statements enclosed by the
keywords LOOP and END LOOP.

Syntax:
WHILE condition
DO sequence_of_statements; ...
END WHILE;

STORED PROCEDURE
A procedure is a subprogram that performs a specific action. It procedure has a header and a body.
The header consists of the name of the procedure and the parameters or variables passed to the
procedure. The body consists or declaration section, execution section and exception section similar
to a general PL/SQL Block. A procedure is similar to an anonymous PL/SQL Block but it is named
for repeated usage. A procedure may or may not return any value.
We can pass parameters to procedures in three ways.
 IN-parameters
 OUT-parameters
 IN OUT-parameters

IN means that the parameter can be referenced inside the procedure body, but it cannot be changed.

OUT means that a value can be assigned to the parameter in the body, but the parameter‟s value cannot
be referenced.

IN OUT allows both assigning values to the parameter and referencing the parameter.

Syntax to create a procedure:


DELIMITER //

CREATE PROCEDURE sp_name(parameter_list)


BEGIN
statements;
END;

DELIMITER ;

Parameter_list: [ IN | OUT | INOUT ] param_name type


type: Any valid MySQL data type

How to Execute a Stored Procedure:


Call procedurename(parameter[,...])
STORED FUNCTIONS
A function is a subprogram that computes a value. Functions and procedure are structured alike, except
that functions have a RETURN clause.

Syntax to create a function:


DELIMITER $$

CREATE FUNCTION function_name(


param1,
param2,…
)
RETURNS datatype
[NOT] DETERMINISTIC
BEGIN
-- statements
END $$

DELIMITER ;

Calling a stored function in an SQL statement

The following statement illustrates how to call a stored function:


SELECT function_name(parameters) FROM tablename;

Worksheet -6

1. Create a table with the following schema and insert values into it.
Account(accno, name, date_of_opening, acc_type, balance)

create table account(accno int primary key, name varchar(20),


date_of_opening date, acc_type varchar(10), balance float(2));

insert into account values(12345, "Ravi", "2000-03-


01","savings",30000);
insert into account values(12346, "Arun", "2010-10-
22","savings",20000);
insert into account values(12347, "Baskar", "2018-07-
23","fixed",100000);

select * from account;


+-------+--------+-----------------+----------+---------+
| accno | name | date_of_opening | acc_type | balance |
+-------+--------+-----------------+----------+---------+
| 12345 | Ravi | 2000-03-01 | savings | 30000 |
| 12346 | Arun | 2010-10-22 | savings | 20000 |
| 12347 | Baskar | 2018-07-23 | fixed | 100000 |
+-------+--------+-----------------+----------+---------+
2. Alter the table to add a new column named interest.
alter table account add column(interest float(2));

3. Create a stored procedure to update the interest as follows.


If the acc_type is
savings – 2% of balance
current – 4% of balance
fixed – 7% of balance

DELIMITER //

CREATE PROCEDURE sp_int_cal()


BEGIN
DECLARE a_type varchar(10);
DECLARE accnum int(11);
DECLARE bal float(2);
DECLARE int_cal float(2);
DECLARE done BOOL DEFAULT false;
DECLARE cur1 CURSOR FOR SELECT accno,acc_type,balance FROM
account;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = true;
OPEN cur1;
read_loop: LOOP
FETCH cur1 INTO accnum, a_type,bal;
IF done=true THEN
LEAVE read_loop;
END IF;
IF a_type="savings" THEN
update account set interest=balance*2/100 where
accno=accnum;
ELSEIF a_type="current" THEN
update account set interest=balance*5/100 where
accno=accnum;
ELSE
update account set interest=balance*7/100 where
accno=accnum;
END IF;
END LOOP;

CLOSE cur1;

END;

DELIMITER ;

4. Call the stored procedure and update the interest

call sp_int_cal();
5. Create a stored function named that returns the level of customer.
Below one year time – silver
One to three year time – gold
Three to five year time – diamond
Above five years - platinum

DELIMITER //

CREATE FUNCTION CustomerLevel(doj date)


RETURNS VARCHAR(20)
DETERMINISTIC
BEGIN
DECLARE customerLevel VARCHAR(20);
DECLARE x float(2);
SET x=(datediff(curdate(),doj)/365);
IF x> 5 THEN
SET customerLevel = 'PLATINUM';
ELSEIF (x > 3 AND x <= 5) THEN
SET customerLevel = 'DIAMOND';
ELSEIF (x >= 1 AND x <= 3) THEN
SET customerLevel = 'GOLD';
ELSEIF (x < 1) THEN
SET customerLevel = 'SILVER';
END IF;
RETURN (customerLevel);
END//

DELIMITER ;

6. Display the accno, name and level of the customer.

select name,CustomerLevel(date_of_opening) from account;

Result:
Thus user defined functions and stored procedures were created and executed successfully.

You might also like