Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 23

STORED PROCEDURE

DEFINISI

A stored procedure is a segment of


declarative SQL code, which is stored in
the database catalog.
Can be invoked by a program, a trigger
or even another stored procedure.

ADVANTAGES

Stored procedure increases performance of


application.
Compiled and stored in the database catalog, faster than
uncompiled SQL commands sent from application.

Stored procedure reduces the traffic between


application and database server.
Only has to send the stored procedure's name.

Reusable and transparent to any application which


wants to use it.
Secured.

Access without granting any permission on the underlying


database tables.

DISADVANTAGES

Make the database server high load in both


memory and processors.

Only contains declarative SQL.

Server perform a number of logical operations or a


complex of business logic.
Very difficult to write a procedure in certain
complexity

Difficult to debug.
Not easy to write and maintain.

May introduced problems in both application


development and maintain phase.

STORED PROCEDURE

1 DELIMITER //
2 CREATE PROCEDURE GetAllProducts()
3 BEGIN
4 SELECT * FROM products;
5 END //
6 DELIMITER ;

INVOKE

To invoke a stored procedure:

1 CALL STORED_PROCEDURE_NAME();

To invoke the stored procedure


GetAllProducts:

1 CALL GetAllProducts();

DECLARING VARIABLES

Variables are used to store the


immediate result.
1 DECLARE variable_name
datatype(size)
DEFAULT
default_value;
Sample:
1 DECLARE total_sale INT DEFAULT 0
2 DECLARE x, y INT DEFAULT 0
3 DECLARE a, b VARCHAR(20)

ASSIGNING VARIABLES

To assign other value to a variable you can use SET


statement, for example:
1 DECLARE total_count INT DEFAULT 0
2 SET total_count = 10;

Besides SET statement, we can use SELECT INTO


statement to assign result of a query to a variable.

1 DECLARE total_products INT DEFAULT 0


2 SELECT COUNT(*) INTO total_products
3 FROM products

STORED PROCEDURE PARAMETER

Parameter has one of three modes IN, OUT


and INOUT.
IN, a parameter can be passed into stored
procedures but any modification inside stored
procedure does not change parameter.
OUT, stored procedure can change this
parameter and pass back to the calling
program.
INOUT, combined of IN and OUT mode;

The syntax is as follows:

1 MODE param_name param_type(param_size)

MODE could be IN, OUT or INOUT depending on the purpose.

param_name is the name of the parameter.

The name must not be the same as the column name of tables and following
naming convention.

param_type(param_size) is the type of parameter and its size.

Each parameter is separated by a comma if the stored procedure more


than one parameter.

1 DELIMITER //
2 CREATE PROCEDURE
ObyC(IN cName VARCHAR(255))
3
BEGIN
4
SELECT city, phone
5
FROM offices
6
WHERE country = cName;
7
END //
8 DELIMITER ;

1 CALL ObyC('USA')

01 DELIMITER $$
02 CREATE PROCEDURE CountObyS (
03
IN orderStatus VARCHAR(25),
04
OUT total INT)
05
BEGIN
06
SELECT count(orderNumber)
07
INTO total
08
FROM orders
09
WHERE status = orderStatus;
10
END$$
11 DELIMITER ;

To get number of shipped orders:


1 CALL CountObyS ('Shipped',@total);
To get number of in process:
1 CALL CountOrderByStatus('in
process',@total);
To view data
1 SELECT @total AS total_in_process;

CREATE PROCEDURE mhs (


2.
IN prod VARCHAR(25),
3.
OUT total INT)
4.
BEGIN
5.
SELECT COUNT(id_mahasiswa)
6.
INTO total
7.
FROM tb_mahasiswa
8.
WHERE id_prodi = prod;
9.
END $$
10. DELIMITER ;
1.

11. CALL

mhs(1,@ss);
12. SELECT @ss

CONDITIONAL

execute the code based on the value of


an expression or a combination of
expression using logical operators.
The IF Statement
1 IF expression THEN commands
2 [ELSEIF expression THEN commands]
3 [ELSE commands]
4 END IF;

Several combinations.

1 IF expression THEN commands


2 END IF;

1 IF expression THEN commands


2 ELSE commands
3 END IF;
1 IF expression THEN commands
2 ELSEIF expression THEN commands
3 ELSE commands
4 END IF;

1.

DELIMITER $$

CREATE PROCEDURE `ktm`.`mhs`(


3.
IN prod VARCHAR(25),
4.
OUT msg VARCHAR(55))
5.
BEGIN
6.
DECLARE total INT DEFAULT 0;
7.
SELECT COUNT(id_mahasiswa) INTO total FROM tb_mahasiswa
8.
WHERE id_prodi = prod;
9.
IF total<5 THEN
10.
SET msg='Walah, cuman dikit';
11.
ELSE
12.
SET msg='Lumayan';
13.
END IF;
14.
END $$
15. DELIMITER ;
2.

The CASE Statement

1 CASE case_expression
2 WHEN when_expression THEN commands
3 WHEN when_expression THEN commands
4 ...
5 ELSE commands
6 END CASE;

DELIMITER $$
CREATE PROCEDURE `ktm`.`mhs`(
IN prod VARCHAR(25),
OUT msg VARCHAR(55))
BEGIN
DECLARE total INT DEFAULT 0;
SELECT COUNT(id_mahasiswa) INTO total FROM tb_mahasiswa
WHERE id_prodi = prod;
CASE total
WHEN total<5 THEN SET msg='Walah, cuman dikit';
WHEN total>=5 THEN SET msg='Lumayan';
END CASE;
END $$
DELIMITER ;

LOOPS

WHILE loop

1 WHILE expression DO
2 Statements
3 END WHILE;

DELIMITER $$
2. CREATE PROCEDURE `ktm`.`WhileLoopProc`()
3.
BEGIN
4.
DECLARE s INT;
5.
DECLARE str VARCHAR(255);
6.
SET s = 1;
7.
SET str = '';
8.
WHILE s <= 5 DO
9.
SET str = CONCAT(str,s,',');
10.
SET s = s + 1;
11.
END WHILE;
12.
SELECT str;
13.
END$$
14. DELIMITER ;
1.

REPEAT loop

The syntax of repeat loop is as follows:


1 REPEAT
2 Statements;
3 UNTIL expression
4 END REPEAT

DELIMITER $$
2. CREATE PROCEDURE `ktm`.`WhileLoopProc`()
3.
BEGIN
4.
DECLARE s INT;
5.
DECLARE str VARCHAR(255);
6.
SET s = 1;
7.
SET str = '';
8.
REPEAT
9.
SET str = CONCAT(str,x,',');
10.
SET x = x + 1;
11.
UNTIL x > 5
12.
END REPEAT;
13.
SELECT str;
14.
END$$
15. DELIMITER ;
1.

You might also like