You are on page 1of 7

Stored Procedures

● 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.

❖ What are delimiters in database?


● Delimiters are used when we need to define the stored procedures as well as to create triggers.
● Default delimiter is semicolon.
● But, if we are considering multiple statements, then we need to use different delimiters like $$ or //.

1. Procedure with no parameters:


A procedure without parameters does not take any input or casts an output indirectly.

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 //

To call the procedure: call get_stud(); //


2. Procedure with ‘in’ parameters:

● It is the default mode.


● It takes a parameter as input, such as an attribute.
● When we define it, the calling program has to pass an argument to the stored procedure.

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); //

To see the output:

delimiter ;
select *from stud_info;
3.) Procedure with OUT parameter:

It is used to pass a parameter as output.


Its value can be changed inside the stored procedure, and the changed (new) value is passed back to the
calling program.

E.g.:

delimiter &&
create procedure max_marks(out highestmarks int)
-> begin
-> select max(marks) into highestmarks from stud_info;
-> end &&

call disp_max(@p); && —-> @p session variable


select @M; &&
3.) Procedure with INOUT parameter:

It is a combination of IN and OUT parameters.


It means the calling program can pass the argument, and the procedure can modify the INOUT parameter, and then passes the new value
back to the calling program.

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

3. datatype: Mysql Statements


It is datatype of value returned by function.

4. characteristics: RETURN expression;


A deterministic function always returns the same result for the END
same input parameters whereas a non-deterministic function
returns different results for the same input parameters.
If you don’t use DETERMINISTIC or NOT DETERMINISTIC,
MySQL uses the NOT DETERMINISTIC option by default.

You might also like