ex6
ex6
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.
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
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.
DELIMITER ;
DELIMITER ;
Worksheet -6
1. Create a table with the following schema and insert values into it.
Account(accno, name, date_of_opening, acc_type, balance)
DELIMITER //
CLOSE cur1;
END;
DELIMITER ;
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 //
DELIMITER ;
Result:
Thus user defined functions and stored procedures were created and executed successfully.