Download Stored Procedure

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Stored Procedure
MySQL 5
© Ilmu Komputer UGM
[email protected]
Definisi
• Stored procedure adalah kumpulan dari statemen SQL
yang digunakan secara bersama-sama.
• Stored procedure menginjinkan penggunaan statemen
SQL lebih dari sekedar statemen SQL biasa untuk
meretrieve dan mengupdate database.
• Stored procedure mendukung penggunaan variable,
kondisi, flow control dan cursor.
• Stored procedure dapat terdiri dari paling tidak satu
statemen sampai dengan ratusan/ribuan baris.
© Ilmu Komputer UGM
[email protected]
Keuntungan
• Memungkinkan kita menggunakan beberapa statemen
query sekaligus.
• Efisiensi hasil query yang diproses/dikirimkan ke klien.
• Memberikan interface yang sederhana terhadap data
dari klien.
• Pengelolaan query menjadi lebih mudah dan terpusat.
• Memudahkan ketika adanya proses optimasi dan
perubahan pada business proses
• Meningkatkankeamanan
© Ilmu Komputer UGM
[email protected]
Kerugian
• Membebani database server
• Lebih kelihatan kompleks
© Ilmu Komputer UGM
[email protected]
© Ilmu Komputer UGM
[email protected]
Sample Database
• Database yang digunakan adalah database
Northwind.
© Ilmu Komputer UGM
[email protected]
Parameter
• <create procedure statement> ::=
– CREATE PROCEDURE <procedure name> ( [
<parameter list> ] )
– <routine body>
• <parameter list> ::=
– <parameter specification> [ , <parameter
specification> ]...
• <parameter specification> ::=
– [ IN | OUT | INOUT ] <parameter> <data type>
© Ilmu Komputer UGM
[email protected]
• CREATE PROCEDURE sp_name
([proc_parameter[,...]]) [characteristic ...] routine_body
• proc_parameter: [ IN | OUT | INOUT ] param_name type
• characteristic:
–
–
–
–
–
–
–
LANGUAGE SQL |
[NOT] DETERMINISTIC |
{ CONTAINS SQL | NO SQL |
READS SQL DATA |
MODIFIES SQL DATA } |
SQL SECURITY { DEFINER | INVOKER } |
COMMENT 'string'
• routine_body: Valid SQL procedure statements or
statements
© Ilmu Komputer UGM
[email protected]
Contoh
• Create procedure tampil_customers()
• Select * from customers;
• Digunakan untuk menampilkan seluruh data
customer;
• Call tampil_customers();
© Ilmu Komputer UGM
[email protected]
delimiter //
create procedure cari_customer(in cust_id
varchar(5), out comp_name varchar(40))
begin
select companyname from customers where
customerid = cust_id into comp_name;
end
//
© Ilmu Komputer UGM
[email protected]
• Call cari_customer(‘ANTON’,@name)
• Select @name
© Ilmu Komputer UGM
[email protected]
Query data dgn Select Into
<select into statement> ::=
<select clause>
<into clause>
[ <from clause>
[ <where clause> ]
[ <group by clause> ]
[ <having clause> ]
[ <select block tail> ] ]
<select block tail> ::=
<order by clause> |
<limit clause> |
<order by clause> <limit clause>
<into clause> ::=
INTO <local variable> [ , <local variable> ]...
© Ilmu Komputer UGM
[email protected]
create procedure kali_belanja
(in cust_id varchar(5),
out kali_belanja int)
begin
select count(cust_id)
into kali_belanja
from orders
where customerid = cust_id;
end
© Ilmu Komputer UGM
[email protected]
• Call kali_belanja(‘ANTON’,@A);
• Select @A;
© Ilmu Komputer UGM
[email protected]
Tugas
• Tampilkan nama employee dengan id tertentu dan berapa kali
mereka melayani customernya.
• Tampilkan nama customer dengan id tertentu dan besarnya
discount yang diperoleh.
• Tampilkan nama customer dengan id tertentu dan berapa kali
mereka bertransaksi.
• Tampilkan nama barang dengan kode tertentu dan berapa kali
ditransaksikan.
• Tampilkan nama customer dengan id tertentu dan berapa besar nilai
yang sudah mereka transaksikan.
© Ilmu Komputer UGM
[email protected]
• mysql> delimiter //
mysql> create procedure empproc(in name
char(12),in fathername char(12),in password
char(12))
-> begin
-> insert into emp
values(name,fathername,password);
-> end;
-> //
© Ilmu Komputer UGM
[email protected]
• mysql> delimiter //
• mysql> CREATE PROCEDURE Proc(OUT p VARCHAR(20), OUT
p1 VARCHAR(20),IN p2 INT)
• -> BEGIN
• -> INSERT INTO Emp VALUES(p2,'Suman','Pune','Web
Designer',20000,965);
• -> SELECT Ename,City INTO p,p1 FROM Emp WHERE Eid=p2; > END -> // Query OK, 0 rows affected (0.01 sec)
• mysql> delimiter ;
• mysql> CALL Proc(@Name,@City,8);
• Query OK, 0 rows affected (0.03 sec)
• mysql> SELECT @Name,@City;
© Ilmu Komputer UGM
[email protected]
•
•
•
•
•
mysql> CREATE FUNCTION func(str CHAR(20))
-> RETURNS CHAR(50)
-> RETURN CONCAT('WELCOME TO, ',str,'!');
Query OK, 0 rows affected (0.00 sec)
mysql> SELECT func('RoseIndia');
© Ilmu Komputer UGM
[email protected]
© Ilmu Komputer UGM
[email protected]
Related documents