Stored and Procedure … (part.2)
Berikut beberapa kutipan yang diambil dari ebook MySQL 5.0 Refernce Manual mengenai Stored Procedure and Function :
Stored routines (procedures and functions) are supported in MySQL 5.0. A stored procedure is a set of SQL statements that can be stored in the server. Once this has been done, clients don’t need to keep reissuing the individual statements but can refer to the stored procedure instead.
Some situations where stored routines can be particularly useful:
- When multiple client applications are written in different languages or work on different platforms, but need to perform the same database operations.
- When security is paramount. Banks, for example, use stored procedures and functions for all common operations. This provides a consistent and secure environment, and routines can ensure that each operation is properly logged. In such a setup, applications and users would have no access to the database tables directly, but can only execute specific stored routines.
Stored routines can provide improved performance because less information needs to be sent between the server and the client. The tradeoff is that this does increase the load on the database server because more of the work is done on the server side and less is done on the client (application) side. Consider this if many client machines (such as Web servers) are serviced by only one or a few database servers. (From : MySQL 5.0 Reference Manual)
Sorry, lagi malas terjemahin , semoga dapat dipahami (^-^)
Gambar di atas adalah salah satu tool dari fitur di MySQL GUI Tool Query Browser (versi 1.2.5beta) dalam membuat stored routine, yang mana dibagi lagi menjadi dua macam metode yaitu Procedure dan Function. (perhatikan tanda yang dilingkari garis merah).
Perbedaan paling mendasar dari dua metode tersebut adalah pada function mengembalikan sebuah nilai (umumnya diakhiri sintaks RETURN) sedangkan procedure mengambil sebuah atau beberapa nilai dan tidak diakhiri sintaks RETURN . Sintaks pemanggilan dua metode stored routine itupun berbeda. Pada function pemanggilannya masih dengan menggunakan perintah SELECT sedangkan procedure menggunakan perintah CALL .
Pada artikel pertama telah diberikan contoh tentang function, berikut contoh kode procedure (masih dengan tabel “provinsi” , tapi dengan kasus memanggil satu atau beberapa provinsi dengan kasus tertentu).
DELIMITER $$ DROP PROCEDURE IF EXISTS get_provinsi $$ CREATE PROCEDURE get_provinsi (code1 INT, code2 INT) BEGIN IF code2 = '' THEN SELECT * FROM provinsi WHERE kode_prov = code1; ELSEIF code1 < code2 THEN SELECT * FROM provinsi WHERE kode_prov >= code1 AND kode_prov <= code2 ; ELSE SELECT * FROM provinsi ; END IF ; END$$ DELIMITER ;
perintah pemanggilan procedure :
-- UNTUK MEMANGGIL SATU PROVINSI CALL get_provinsi(11,0); -- UNTUK MEMANGGIL BEBERAPA PROVINSI CALL get_provinsi(11,15) ; -- UNTUK MEMANGGIL SEMUA PROVINSI CALL get_provinsi(11,11) ;
Good Luck and Correct Me IF I Was Wrong (CMIIW)



CALL get_provinsi(11,0);
11 itu maksudnya …?
@picas : 11 itu maksudnya nomor ID provinsi (kode_prov) yang dimulai, pada artikel sebelumnya udah ditampilkan bentuk tabel “provinsi”. nomor ID atau kode_prov sifatnya auto_increment .
Wah bagus sekali tutorialnya.
Tapi kalau mau menampung data hasil prosedurnya bagaimana ya mengingat data keluarannya bisa berupa sebuah field, sebuah record, atau malah beberapa record?