stored procedure

Stored procedure merupakan suatu object dari database  yang terletak dalam instances/ schemas .

Definisi

Pengertian dari stored procedure itu sendiri adalah suatu object yang dapat menyimpan query yang kita buat sehingga query tersebut dapat digunakan kembali.

Contoh sederhana dalam pembuatan stored procedure pada command window atau pada linux adalah sebagai berikut :
db2 create procedure P1 begin end
db2 call P1

Ketentuan

Ketentuan dalam penggunaan stored procedure adalah karena disimpan dalam instances maka stored procedure tidak boleh memiliki nama yang sama dalam sebuah instances.

Kegunaan / Manfaat dari stored procedure

1. Improve performance, stored procedure dapat meningkatkan performa aplikasi dan mengurangi akses database. Pada umumnya akses database adalah melalui jaringan, dimana pada saat tertentu dapat menyebabkan performa yang buruk dalam hal akses aplikasi ke database, dengan menggunakan stored procedure hal ini dapat dihindari. Ilustrasi dari penjelasan tersebut dapat dilihat pada gambar di bawah ini

dapat dilihat pada gambar di atas, stored procedure akan menjalankan SQL statements pada database server.

2. Improve security

3. Reusable

Informasi tambahan adalah stored procedure juga sangat bermanfaat pada saat penghapusan, dan proses update sejumlah besar data. Pada db2 dimungkinkan pembuatan cursor dalam stored procedure.

Parameter pada stored procedure

Pada proses pembuatan stored procedure kita dapat memberikkan parameter yang memiliki kegunaan tertentu pada stored procedure yang kita buat, tergantung dengan tipe dari parameternya

Tipe parameter pada stored procedure dapat dikelompokkan menjadi tiga, yaitu

1. Parameter in

IN parameter (parameter in) merupakan parameter yang menyediakan nilai bagi stored procedure, dengan kata lain parameter tersebut menerima nilai inputan, untuk digunakan dalam stored procedure yang kita buat.

2. Parameter out

out parameter (parameter out) merupakan parameter yang menyimpan hasil atau nilai yang diperoleh dari kerja stored procedure, lalu nilai tersebut lah yang dikembalikkan setelah stored procedure selesai di-execute

3. Parameter inout

Parameter ini memiliki fungsi:

ketika stored procedure pertama kali dijalankan, parameter ini akan bertindak sebagai pemberi inputan bagi stored procedure tersebut setelah stored procedure di-execute nilai dari parameter tersebut akan direturn sebagai nilai yang dihasilkan dari proses eksekusi stored procedure.

Pembuatan Stored Procedure

stored procedure dapat dibuat pada command window seperti pada contoh yang telah disebutkan, namun cara pembuatan stored procedure yang lebih mudah adalah menggunakan IBM data studio. Pada kali ini akan dibaha mengenai langkah – langkah pembuatan stored procodure menggunakan IBM data studio.

Berikut gambaran umum dalam langkah atau proses pembuatan stored procedure pada db2

Setelah melihat overview dalam tahapan pembuatan stored procedure, berikut akan dijelaskan tahapan – tahapan dalam pembuatan stored procedure dalam IBM data studio

Berikut stored procedure yang akan kita buat

sebelum masuk ke IBM data studio, kita harus memastikan bahwa kita telah ter-connect dengan database, dengan cara

db2 connect to [NAMA DATABASE]
1.    Open the DB2 Command Window
start -> Run -> db2cmd

2.    Create your first SQL PL stored procedure by typing the following:
db2 connect to sample
db2 create procedure p1 begin end

The procedure you created does nothing, but it shows you how easy it is to create one.  To call the procedure from the DB2 Command Window, issue:
db2 call p1()

3.    Now let’s create SQL PL Stored Procedures using IBM Data Studio.  As shown from the previous example, IBM Data Studio is not required to develop stored procedures; however, we recommend this tool as it makes development easier.

4.    Open the IBM Data Studio (Hint: it is accessible through the Start menu).

5.    Click on the Data Project Explorer window, then choose:
File -> New -> Project -> Data Development Project

6.    After clicking next, give the name MyProject to your project, and click next.

7.    Click on the Create a new connection radio button, and then on “next”

8.    The database name should be SAMPLE.  The rest of default values should be correct.  Provide your user ID/psw and click on Test Connection. If the connection is successful, click on Next, and then on Finish.

9.    On the Data Project Explorer you should now be able to see your project.  Drill down your project tree as shown below to list folders for SQL Scripts, Stored Procedures, etc.

10.    Right-click the Stored Procedures folder, and choose New  Stored Procedure

11.    Provide a name for your stored procedure, for example use “myprocedure”, then click Next.

12.    In the SQL Statements window, IBM Data Studio provides you with a sample statement SELECT PROCSCHEMA, PROCNAME FROM SYSCAT.PROCEDURES. Replace this statement with: SELECT * FROM EMPLOYEE, and click on Finish.  You will then be presented with a template code as shown below.

14.    In the Database Explorer panel, expand the tree to show your connection, database, schema, and stored procedures.  Note the stored procedure myprocedure is not shown.  The Database Explorer shows the objects in the database.  At this moment, the myprocedure stored procedure is not in the database since it has not been deployed.  To deploy the procedure, right-click on it from the Data Project Explorer window, and choose Deploy. On the Deploy options window, let’s choose all default, and click on Finish.

15.    If the deployment was not successful, review the error codes received at deployment time, and fix them. If the deployment is successful, you are ready to run the procedure. Right-click on the procedure from the Data Project Explorer panel, and choose Run.  At the bottom right-corner you will see the results of the procedure.

16.    Now that the procedure has been successfully coded, deployed and run, you can test running it from the DB2 Command Window or DB2 Command Editor. Test this from the DB2 Command Editor:

start -> run -> db2ce

From the DB2 Command Editor, connect to the SAMPLE database, and issue:
call myprocedure()

untuk pemanggilan stored procedure kita dapat mengisi parameter ataupun tidak, jika parameter tidak diisi defaultnya IN