Professional Documents
Culture Documents
SQL 2
SQL 2
● A procedure (often called a stored procedure) is a collection of pre-compiled SQL statements stored inside the
database.
● It is a subroutine or a subprogram in the regular computing language.
● A procedure always contains a name, parameter lists, and SQL statements.
E.g.:
delimiter //
create procedure get_stud()
-> begin
-> select * from stud_info where marks>65;
-> select count(stud_code) as total_student from stud_info;
-> end //
E.g.:
Procedure to update marks of a student by taking stud_code of the book and its new marks as input
delimiter //
create procedure update_marks (IN temp_stud_code int, IN new_marks int)
-> begin
-> update stud_info set marks=new_marks where stud_code=temp_stud_code;
-> end; //
call update_marks(102, 70); //
delimiter ;
select *from stud_info;
3.) Procedure with OUT parameter:
E.g.:
delimiter &&
create procedure max_marks(out highestmarks int)
-> begin
-> select max(marks) into highestmarks from stud_info;
-> end &&
E.g.:
delimiter //
create procedure display_marks(inout var2 int)
-> begin
-> select marks into var2 from stud_info where stud_id=var2;
-> end //
Set @s=’4’;
Call display_marks(@s); //
Select @s; //
Functions
● A stored function is a set of SQL statements that perform some operation and return a single value.
● The CREATE FUNCTION statement is used for creating a stored function and user-defined functions.
Syntax:
CREATE FUNCTION function_name(func_parameter1, func_parameter2, ..)
RETURN datatype [characteristics]
func_body
Parameters used: func_body :
1.function_name:
It is the name by which stored function is called. It is the set of Mysql statements that perform
operation.
2. func_parameter: It’s structure is as follows:
It is the argument whose value is used by the function inside its
body. You can’t specify to these parameters IN, OUT, INOUT. BEGIN