Hands-On Lab: Stored Procedures: Software Used in This Lab
Hands-On Lab: Stored Procedures: Software Used in This Lab
Hands-On Lab: Stored Procedures: Software Used in This Lab
In this lab, you will create and execute stored procedures on IBM Db2 using SQL. A stored procedure is a set of SQL statements that are stored
and executed on the database server. So instead of sending multiple SQL statements from the client to the server, you encapsulate them in a
stored procedure on the server and send one statement from the client to execute them. Also, stored procedures can be useful if you have an
SQL query that you write over and over again. You can save it as a stored procedure, and then just call it to execute it. In stored procedures,
you can also pass parameters so that a stored procedure can act based on the passed parameter values.
To complete this lab you will utilize a Db2 database service on IBM Cloud. If you did not already complete this lab task earlier in this module,
you will not yet have access to Db2 on IBM Cloud, and you will need to follow the lab below first:
Hands-on Lab : Sign up for IBM Cloud, Create Db2 service instance and Get started with the Db2 console
This lab requires you to have the PETSALE table populated with sample data on Db2. You might have created and populated a PETSALE table
in a previous lab. But for this lab, it is recommended you download the PETSALE-CREATE-v2.sql script below, upload it to Db2 console and run
it. The script will create a new PETSALE table dropping any previous PETSALE table if exists, and will populate it with the required sample data.
PETSALE-CREATE-v2.sql
Please go through the lab below to learn how to upload and run a script on Db2 console (for this case, you need don't need to know anything
else other than how to upload and run a script):
Hands-on Lab : Create tables using SQL scripts and Load data into tables
Objectives
After completing this lab, you will be able to:
Instructions
When you approach the exercises in this lab, follow the instructions to run the queries on Db2:
Go to the Resource List of IBM Cloud by logging in where you can find the Db2 service instance that you created in a previous lab under
Services section. Click on the Db2-xx service. Next, open the Db2 Console by clicking on Open Console button. Click on the 3-bar
menu icon in the top left corner and go to the Run SQL page. The Run SQL tool enables you to run SQL statements.
If needed, follow Hands-on Lab : Sign up for IBM Cloud, Create Db2 service instance and Get started with the Db2 console
Exercise 1
In this exercise, you will create and execute a stored procedure to read data from a table on Db2 using SQL.
1. Make sure you have created and populated the PETSALE table following the steps in the "Data Used in this Lab" section of this lab.
--#SET TERMINATOR @
READS SQL DATA -- This routine will only read data from the table
DYNAMIC RESULT SETS 1 -- Maximum possible number of result-sets to be returned to the caller query
BEGIN
DECLARE C1 CURSOR -- CURSOR C1 will handle the result-set by retrieving records row by row from the table
WITH RETURN FOR -- This routine will return retrieved records as a result-set to the caller query
SELECT * FROM PETSALE; -- Query to retrieve all the records from the table
OPEN C1; -- Keeping the CURSOR C1 open so that result-set can be returned to the caller query
END
3. To call the RETRIEVE_ALL routine, copy the code below in a new blank script and paste it to the textbox of the Run SQL page. Click Run
all. You will have all the records retrieved from the PETSALE table.
4. You can view the created stored procedure routine RETRIEVE_ALL. Click on the 3-bar menu icon in the top left corner and click EXPLORE
> APPLICATION OBJECTS > Stored Procedures. Find the procedure routine RETRIEVE_ALL from Procedures by clicking Select All. Click
on the procedure routine RETRIEVE_ALL.
5. If you wish to drop the stored procedure routine RETRIEVE_ALL, copy the code below and paste it to the textbox of the Run SQL page.
Click Run all.
CALL RETRIEVE_ALL;
Exercise 2
In this exercise, you will create and execute a stored procedure to write/modify data in a table on Db2 using SQL.
1. Make sure you have created and populated the PETSALE table following the steps in the "Data Used in this Lab" section of this lab.
2. You will create a stored procedure routine named UPDATE_SALEPRICE with parameters Animal_ID and Animal_Health.
This UPDATE_SALEPRICE routine will contain SQL queries to update the sale price of the animals in the PETSALE table depending
on their health conditions, BAD or WORSE.
This procedure routine will take animal ID and health conditon as parameters which will be used to update the sale price of animal
in the PETSALE table by an amount depending on their health condition. Suppose -
For animal with ID XX having BAD health condition, the sale price will be reduced further by 25%.
For animal with ID YY having WORSE health condition, the sale price will be reduced further by 50%.
For animal with ID ZZ having other health condition, the sale price won't change.
To create the stored procedure routine, copy the code below and paste it to the textbox of the Run SQL page. Click Run all.
--#SET TERMINATOR @
MODIFIES SQL DATA -- This routine will only write/modify data in the table
BEGIN
UPDATE PETSALE
WHERE ID = Animal_ID;
UPDATE PETSALE
WHERE ID = Animal_ID;
ELSE
UPDATE PETSALE
WHERE ID = Animal_ID;
END
3. Let's call the UPDATE_SALEPRICE routine. We want to update the sale price of animal with ID 1 having BAD health condition in the
PETSALE table. Copy the code below in a new blank script and paste it to the textbox of the Run SQL page. Click Run all. You will have
all the records retrieved from the PETSALE table.
CALL RETRIEVE_ALL;
CALL RETRIEVE_ALL;
4. Let's call the UPDATE_SALEPRICE routine once again. We want to update the sale price of animal with ID 3 having WORSE health
condition in the PETSALE table. Copy the code below and paste it to the textbox of the Run SQL page. Click Run all. You will have all the
records retrieved from the PETSALE table.
CALL RETRIEVE_ALL;
CALL RETRIEVE_ALL;
5. You can view the created stored procedure routine UPDATE_SALEPRICE. Click on the 3-bar menu icon in the top left corner and click
EXPLORE > APPLICATION OBJECTS > Stored Procedures. Find the procedure routine UPDATE_SALEPRICE from Procedures by clicking
Select All. Click on the procedure routine UPDATE_SALEPRICE.
6. If you wish to drop the stored procedure routine UPDATE_SALEPRICE, copy the code below and paste it to the textbox of the Run SQL
page. Click Run all.
Congratulations! You have completed this lab, and you are ready for the next
topic.
Author(s)
Sandip Saha Joy
Other Contributor(s)
Changelog
Date Version Changed by Change Description