V cover. Front cover. DB2 Stored Procedures Programming Workshop. (Course Code CF71) Student Exercises ERC 4.0. IBM Certified Course Material

Similar documents
Front cover. DB2 Universal Database Programming Workshop for Linux, UNIX, and Windows (Course Code CF10) Lab Set Up Guide

IBM DB2 Query Patroller. Administration Guide. Version 7 SC

V1.1. AIX 5L System Administration I: Implementation. (Course Code AU14) Student Exercises ERC 7.0. IBM Learning Services Worldwide Certified Material

Business Intelligence Tutorial

Configuration Manager

IBM. Database Database overview. IBM i 7.1

Understanding Advanced Workflow

Visual Explain Tutorial

IBM i Version 7.2. Database Database overview IBM

IBM Networking OS. BBI Quick Guide. for the EN2092 1Gb Ethernet Scalable Switch, Second edition (replaces 88Y7949)

BC100. Introduction to Programming with ABAP COURSE OUTLINE. Course Version: 15 Course Duration: 2 Day(s)

Overview. CHAPTER 2 Using the SAS System and SAS/ ASSIST Software

SAS Model Manager 2.3

SAS. Information Map Studio 3.1: Creating Your First Information Map

BC404. ABAP Programming in Eclipse COURSE OUTLINE. Course Version: 15 Course Duration: 3 Day(s)

Event Server Configuration Manager

BC400. ABAP Workbench Foundations COURSE OUTLINE. Course Version: 15 Course Duration: 5 Day(s)

HA150 SQL Basics for SAP HANA

Java Programming Language

BC400 Introduction to the ABAP Workbench

Lotus Learning Management System R1

Tivoli Access Manager for Enterprise Single Sign-On

DB2 for z/os Stored Procedure support in Data Server Manager

IBM ILOG OPL IDE Reference

IBM. User's Guide. IBM Explorer for z/os. Version 3 Release 0 SC

Tivoli Access Manager for Enterprise Single Sign-On

SAS Studio: A New Way to Program in SAS

Creating Your First Web Dynpro Application

Rational Developer for IBM i (RDI) Distance Learning hands-on Labs IBM Rational Developer for i. Maintain an ILE RPG application using.

ADM920 SAP Identity Management

BC410. Programming User Dialogs with Classical Screens (Dynpros) COURSE OUTLINE. Course Version: 10 Course Duration: 3 Day(s)

ADM950. Secure SAP System Management COURSE OUTLINE. Course Version: 15 Course Duration: 2 Day(s)

BOC310. SAP Crystal Reports: Fundamentals of Report Design COURSE OUTLINE. Course Version: 15 Course Duration: 2 Day(s)

MDG100 Master Data Governance

V7.0. cover. Front cover. IBM Connections 4.5 Deployment Scenarios. Deployment Scenarios ERC 1.0

Composite Application Tutorial I: NSF Components

Version Monitoring Agent User s Guide SC

Building a Composite Business Process from Scratch with SAP NetWeaver BPM Guide 2

SAS Data Integration Studio 3.3. User s Guide

Data Express 4.0. Data Subset Extraction

Tivoli Access Manager for Enterprise Single Sign-On

NET311. Advanced Web Dynpro for ABAP COURSE OUTLINE. Course Version: 10 Course Duration: 4 Day(s)

DB2 for z/os: Programmer Essentials for Designing, Building and Tuning

SAS Factory Miner 14.2: User s Guide

DISCLAIMER COPYRIGHT List of Trademarks

Installation Guide. Tivoli Decision Support 2.0

Tivoli Decision Support 2.1

Introduction to IBM Data Studio, Part 1: Get started with IBM Data Studio, Version and Eclipse

Introduction to IBM Data Studio, Part 1: Get started with IBM Data Studio, Version and Eclipse

ADM100 AS ABAP - Administration

Front cover. IBM WebSphere Commerce V7 System Administration. Student Exercises. WebSphere Education V cover. (Course code 6T222-6L222)

Console Guide. Version 4.4

ADM950. Secure SAP System Management COURSE OUTLINE. Course Version: 10 Course Duration: 2 Day(s)

Enterprise Generation Language (EGL) for IBM i operating system Create a web application using EGL and the Data Access Application Wizard

ADM900 SAP System Security Fundamentals

Oracle Enterprise Manager. Description. Versions Supported. Prerequisites

BIT460. SAP Process Integration Message Mapping COURSE OUTLINE. Course Version: 15 Course Duration: 3 Day(s)

PeopleTools Adapter User Guide

GRC100. GRC Principles and Harmonization COURSE OUTLINE. Course Version: 10 Course Duration: 2 Day(s)

AFA461 SAP Afaria 7.0 System Administration (SP03)

How to Package and Deploy SAP Business One Extensions for Lightweight Deployment

IBM Initiate Master Data Extract Version 10 Release 0. User's Guide GI

Installing and Administering a Satellite Environment

IBM Workplace TM Collaboration Services

EWM125. Labor Management in SAP EWM COURSE OUTLINE. Course Version: 16 Course Duration: 4 Hours

BOCE20. SAP Crystal Reports for Enterprise: Advanced Report Design COURSE OUTLINE. Course Version: 15 Course Duration: 3 Day(s)

DS50. Managing Data Quality with SAP Information Steward COURSE OUTLINE. Course Version: 10 Course Duration: 2 Day(s)

WINDOWS NT BASICS

BOC320. SAP Crystal Reports - Business Reporting and Report Processing Strategies COURSE OUTLINE. Course Version: 15 Course Duration: 3 Day(s)

CA CloudMinder. Identity Management User Console Design Guide 1.51

TBIT40 SAP NetWeaver Process Integration

DEV523 Customizing and Extending PowerDesigner

MetaMap Manager User Guide

PLM210. Master Data Configuration in SAP Project System COURSE OUTLINE. Course Version: 15 Course Duration: 2 Day(s)

Visual Composer for SAP NetWeaver Composition Environment - Connectors

How To... Configure Drill Through Functionality

TBIT44 PI Mapping and ccbpm

BC405 Programming ABAP Reports

BC430 ABAP Dictionary

Administrator for Enterprise Clients: User s Guide. Second Edition

Reducing MIPS Using InfoSphere Optim Query Workload Tuner TDZ-2755A. Lloyd Matthews, U.S. Senate

Tutorial: Maintaining an ILE COBOL application: Introducing a new level of server tool integration for iseries application development

BC401. ABAP Objects COURSE OUTLINE. Course Version: 15 Course Duration: 5 Day(s)

EP350. Innovated Content Management and Collaboration COURSE OUTLINE. Course Version: 10 Course Duration: 5 Day(s)

GUARD1 PLUS Documentation. Version TimeKeeping Systems, Inc. GUARD1 PLUS and THE PIPE are registered trademarks

EDB116. Fast Track to SAP Adaptive Server Enterprise COURSE OUTLINE. Course Version: 15 Course Duration: 5 Day(s)

IBM Infrastructure Suite for z/vm and Linux: Introduction IBM Tivoli OMEGAMON XE on z/vm and Linux

Shell Programming for System Administrators Student Guide

IBM VisualAge for Java,Version3.5. External Version Control

Query Management Facility. Using QMF. Version7Release2 SC

Reporting and Graphing

Tivoli Management Solution for Domino. Installation and Setup Guide. Version GC

Lab DSE Designing User Experience Concepts in Multi-Stream Configuration Management

IBM Client Security Solutions. Client Security Software Version 1.0 Administrator's Guide

Lesson 13 Transcript: User-Defined Functions

Craig Pelkie Bits & Bytes Programming, Inc.

End User s Guide Release 5.0

Work offline with i Projects

AC507. Additional Functions of Product Cost Planning COURSE OUTLINE. Course Version: 15 Course Duration: 2 Day(s)

Demand Management. Job Processing Guide for SAP DM. Release 6.4

Transcription:

V2.0.0.1 cover Front cover DB2 Stored Procedures Programming Workshop (Course Code CF71) Student Exercises ERC 4.0 IBM Certified Course Material

Student Exercises Trademarks IBM is a registered trademark of International Business Machines Corporation. The following are trademarks of International Business Machines Corporation in the United States, or other countries, or both: AIX DB2 DB2 Universal Database iseries OS/2 OS/390 RACF SP SQL/DS System/36 VisualAge z/os Java and all-java based trademarks are trademarks of Sun Microsystems, Inc. in the United States, other countries, or both. Microsoft, Windows and Windows NT are trademarks of Microsoft Corporation in the United States, other countries, or both Intel is a trademark of Intel Corporation in the United States, other countries, or both UNIX is a registered trademark of The Open Group in the United States and other countries Linux is a registered trademark of Linus Torvalds in the United States and other countries. Other company, product, and service names may be trademarks or service marks of others. May 2003 Edition The information contained in this document has not been submitted to any formal IBM test and is distributed on an as is basis without any warranty either express or implied. The use of this information or the implementation of any of these techniques is a customer responsibility and depends on the customer s ability to evaluate and integrate them into the customer s operational environment. While each item may have been reviewed by IBM for accuracy in a specific situation, there is no guarantee that the same or similar results will result elsewhere. Customers attempting to adapt these techniques to their own environments do so at their own risk. Copyright International Business Machines Corporation 1996, 2003. All rights reserved. This document may not be reproduced in whole or in part Note to U.S. Government Users Documentation related to restricted rights Use, duplication or disclosure is subject to restrictions set forth in GSA ADP Schedule Contract with IBM Corp.

V2.0 Student Exercises TOC Contents Trademarks...................................................... vii Exercises Description............................................... ix Lab Introduction: About This Lab Guide................................ xi Exercise 1. The Exercise Environment.................................... 1-1 Exercise Instructions................................................... 1-2 Section 1: Exploring the Exercise Environment........................ 1-2 Exercise Solutions..................................................... 1-5 Section 1: Exploring the Exercise Environment........................ 1-5 Exercise 2. The DB2 Development Center................................. 2-1 Exercise Instructions................................................... 2-2 Section 1: Register a New Project.................................. 2-2 Section 2: Stored Procedure with CREATE TABLE..................... 2-4 Section 3: Stored Procedure Source Code........................... 2-6 Exercise Solutions..................................................... 2-7 Section 1: Register a New Project.................................. 2-7 Section 2: Stored Procedure with CREATE TABLE..................... 2-9 Section 3: Stored Procedure Source Code.......................... 2-11 Exercise 3. SQL Procedure Language.................................... 3-1 Exercise Instructions................................................... 3-3 Section 1: CASE Construct....................................... 3-3 Section 2: IF Construct.......................................... 3-5 Section 3: REPEAT Construct..................................... 3-7 Section 4: LOOP Construct....................................... 3-8 Section 5: Median Salary........................................ 3-9 Section 6: Creating a Result Set.................................. 3-10 Section 7: Stored Procedure Source Code.......................... 3-12 Exercise Solutions.................................................... 3-13 Section 1: CASE Construct...................................... 3-13 Section 2: IF Construct......................................... 3-16 Section 3: REPEAT Construct.................................... 3-18 Section 4: LOOP Construct...................................... 3-20 Section 5: Median Salary....................................... 3-22 Section 6: Creating a Result Set.................................. 3-24 Section 7: Stored Procedure Source Code.......................... 3-26 Exercise 4. Building Java Stored Procedures.............................. 4-1 Exercise Instructions................................................... 4-3 Section 1: JDBC Stored Procedure Returning Result Set................ 4-3 Copyright IBM Corp. 1996, 2003 Contents iii

Student Exercises Section 2: SQLJ Stored Procedure Returning Choice of Results Sets....... 4-5 Section 3: JDBC Stored Procedure Returning Choice of Result Sets........ 4-8 Section 4: SQLJ Stored Procedure Returning Choice of COUNTs......... 4-11 Section 5: JDBC Stored Procedure Returning Choice of COUNTs......... 4-14 Section 6: Generate JDBC Stored Procedure for Further Coding.......... 4-17 Section 7: Stored Procedure Source Code........................... 4-18 Exercise Solutions.....................................................4-19 Section 1: JDBC Stored Procedure Returning Result Set................ 4-19 Section 2: SQLJ Stored Procedure Returning Choice of Results Sets...... 4-21 Section 3: JDBC Stored Procedure Returning Choice of Result Sets....... 4-24 Section 4: SQLJ Stored Procedure Returning Choice of COUNTs......... 4-27 Section 5: JDBC Stored Procedure Returning Choice of COUNTs......... 4-30 Section 6: Generate JDBC Stored Procedure for Further Coding.......... 4-33 Section 7: Stored Procedure Source Code........................... 4-34 Exercise 5. Calling Procedures..........................................5-1 Exercise Instructions....................................................5-2 Section 1: CALL with Parameters.................................. 5-2 Section 2: Processing a Result Set from a CALL....................... 5-4 Section 3: Stored Procedure Source Code............................ 5-6 Exercise Solutions......................................................5-7 Section 1: CALL with Parameters.................................. 5-7 Section 2: Processing a Result Set from a CALL....................... 5-9 Section 3: Stored Procedure Source Code........................... 5-11 Exercise 6. Create Stored Procedures.....................................6-1 Exercise Instructions....................................................6-3 Section 1: Register Previously Defined Java Stored Procedure............ 6-3 Section 2: Import Previously Defined SQL Procedure Language Stored Procedure 6-5 Section 3: Import SQL Procedure Language Stored Procedure Source Code. 6-7 Section 4: Deployment of SQL Procedure Language Stored Procedure...... 6-8 Exercise Solutions......................................................6-9 Section 1: Register Previously Defined Java Stored Procedure............ 6-9 Section 2: Import Previously Defined SQL Procedure Language Stored Procedure 6-11 Section 3: Import SQL Procedure Language Stored Procedure Source Code 6-13 Section 4: Deployment of SQL Procedure Language Stored Procedure..... 6-14 Exercise 7. Debugging Stored Procedures.................................7-1 Exercise Instructions....................................................7-2 Section 1: Debugging an SQL Stored Procedure....................... 7-2 Section 2: Debugging a Java Stored Procedure........................ 7-4 Exercise Solutions......................................................7-6 Section 1: Debugging an SQL Stored Procedure....................... 7-6 iv Stored Procedures Copyright IBM Corp. 1996, 2003

V2.0 Student Exercises TOC Section 2: Debugging a Java Stored Procedure....................... 7-8 Copyright IBM Corp. 1996, 2003 Contents v

Student Exercises vi Stored Procedures Copyright IBM Corp. 1996, 2003

V2.0 Student Exercises TMK Trademarks The reader should recognize that the following terms, which appear in the content of this training document, are official trademarks of IBM or other companies: IBM is a registered trademark of International Business Machines Corporation. The following are trademarks of International Business Machines Corporation in the United States, or other countries, or both: AIX DB2 DB2 Universal Database iseries OS/2 OS/390 RACF SP SQL/DS System/36 VisualAge z/os Java and all Java-based trademarks are trademarks of Sun Microsystems, Inc. in the United States, other countries, or both. Microsoft, Windows and Windows NT are trademarks of Microsoft Corporation in the United States, other countries, or both. Intel is a trademark of Intel Corporation in the United States, other countries, or both. UNIX is a registered trademark of The Open Group in the United States and other countries. Linux is a registered trademark of Linus Torvalds in the United States and other countries. Other company, product and service names may be trademarks or service marks of others. Copyright IBM Corp. 1996, 2003 Trademarks vii

Student Exercises viii Stored Procedures Copyright IBM Corp. 1996, 2003

V2.0 Student Exercises pref Exercises Description These lab exercises were designed to have the student run on a workstation with Windows 2000 and DB2/UDB for NT V8 installed. Copyright IBM Corp. 1996, 2003 Exercises Description ix

Student Exercises x Stored Procedures Copyright IBM Corp. 1996, 2003

V2.0 Student Exercises pref Lab Introduction: About This Lab Guide Before you start: For all labs, ensure that you are logged in to the system using ADMIN as the user ID with a password of ADMIN. About code segments: Because the width of a line of these printed labs is restricted by the physical size of the paper and font, many lines of code in the segments presented cannot fit on a single printed line. These lines of code are split into two or more printed lines. When entering these split lines, type them as a single line of code. Copyright IBM Corp. 1996, 2003 Lab Introduction: About This Lab Guide xi

Student Exercises xii Stored Procedures Copyright IBM Corp. 1996, 2003

V2.0 Student Exercises EXempty Exercise 1. The Exercise Environment What This Exercise Is About This exercise provides an opportunity for the students to examine the lab exercise environment, DB2 UDB for Windows, by using some of the database manager's provided graphical user interfaces. What You Should Be Able to Do Introduction Requirements At the end of the lab, you should be able to: Start the DB2 Control Center application Start the DB2 Command Center application List the objects associated with the lab exercise database, SAMPLE Limit the objects by establishing a view of a subset of objects in the database List the tables in the SAMPLE database Display the contents of SAMPLE database tables that will be used in the labs This lab consists of one section. Section 1: Exploring the Exercise Environment In Section 1, you will maneuver through the DB2 supplied Control Center and Command Center to list objects in the SAMPLE database and display the contents of two of the SAMPLE database tables, ORG and STAFF. Basic understanding of navigating through a Windows desktop. Copyright IBM Corp. 1996, 2003 Exercise 1. The Exercise Environment 1-1

Student Exercises Exercise Instructions The lab solutions can be found in Exercise Solutions on page 1-5. Annotation convention: When your instruction contains the word click, you may see a symbol ->. This means that the first thing you click will result in a pop-up or drop-down selection. For example: Click START -> PROGRAMS first, click START, then click PROGRAMS. Note: Anytime that you are going to close your Development Center, be sure to save your project. There is not an automatic save. Note: DB2 UDB V8 was installed on the workstation prior to class start. Section 1: Exploring the Exercise Environment In this section, you will be examining the SAMPLE database and some of the objects in the SAMPLE database using the Control Center and Command Center user interfaces. Note: DB2 UDB V8 was installed on the workstation prior to class start. 1. 2. 3. 4. 5. Start DB2 Control Center. Click Windows Start -> Programs -> IBM DB2 -> General Administration Tools ->Control Center. This will show the DB2 instance we will work with for our class exercises. In the Control Center window, expand the tree structure labelled DB2... Click + DB2... -> + Instances -> + DB2 -> + Databases -> + SAMPLE. This will show all objects associated with the SAMPLE database that will be used during our class exercises. Click Tables folder icon. This will show all tables in the SAMPLE database. Limit your list of tables to exclude those in SCHEMA: SYSIBM. The command list at the top of the Control Center window contains the command View. Click View -> Filter. In Filter window under Comparison for Schema: Click drop-down arrow -> Not in Under Values for Schema, type SYSIBM (uppercase) Make sure that Enable filter is checked. Click OK button to process. Examine ADMIN.ORG. Double-click icon next to ORG. In 'Alter Table - ORG' window, click Columns tab. 1-2 Stored Procedures Copyright IBM Corp. 1996, 2003

V2.0 Student Exercises EXempty 6. 7. 8. What columns are found for ORG? Click Cancel to exit Alter Table - ORG window. Examine ADMIN.STAFF. Double-click icon next to STAFF. In Alter Table - STAFF window, click Columns tab. What columns are found for STAFF? Click Cancel to exit Alter Table - STAFF window. Let's connect to the SAMPLE database for a quick look at our tables' contents. The command list at the top of the Control Center window contains the command Tools. Click Tools -> Command Center. In Command Center window, click Interactive tab (below icons line). In the Interactive tab Command window: Type in: Connect to SAMPLE Execute this DB2 command Click Gears icon on the icons line. The window below the Command window should show the following: connect to sample ; ---------------------------------------------------- Database Connection Information Database server = DB2/NT 8.1.0 SQL authorization ID = ADMIN Local database alias = SAMPLE Let's look at the ORG table. Click SQL ASSIST button. Select FROM (Source tables). Select ORG table by clicking icon next to ORG under ADMIN. Click > button to move ORG to Selected tables. SQL statement should read: SELECT * FROM ADMIN.ORG. AS ORG Click Run button. How many rows are there currently in table ORG? Copyright IBM Corp. 1996, 2003 Exercise 1. The Exercise Environment 1-3

Student Exercises 9. What are the DEPTNUMB values in ORG at this time? Click OK to return to SQL Assist window. Click OK to return to Command Center window. Let's look at the STAFF table. Click SQL ASSIST button. Select FROM (Source tables). Select the ORG table in the Selected tables section and click the < button to remove it. Select STAFF table by clicking the icon next to STAFF under ADMIN. Click > button to move STAFF to Selected tables. SQL statement should read: SELECT * FROM ADMIN.STAFF AS STAFF. Click Run button. How many rows does STAFF currently consist of? Which employee has the highest value for YEARS in the STAFF table? Is this employee the highest salaried employee? Click OK to return to SQL Assist window. Click OK to return to Command Center window. 10. Minimize the Command Center window. This completes the last section of this exercise. END OF LAB 1-4 Stored Procedures Copyright IBM Corp. 1996, 2003

V2.0 Student Exercises EXempty Exercise Solutions Section 1: Exploring the Exercise Environment In this section, you will be examining the SAMPLE database and some of the objects in the SAMPLE database using the Control Center and Command Center user interfaces. Note: Anytime that you are going to close your Development Center, be sure to save your project. There is not an automatic save. Note: DB2 UDB V8 was installed on the workstation prior to class start. 1. 2. 3. 4. 5. 6. Start DB2 Control Center. Click Windows START -> PROGRAMS -> IBM DB2 -> CONTROL CENTER. This will show the DB2 instance we will work with for our class exercises. In the Control Center window, expand the tree structure labelled DB2... Click + DB2... -> + Instances -> + DB2 -> + Databases -> + SAMPLE. This will show all objects associated with the SAMPLE database that will be used during our class exercises. Click Tables folder icon. This will show all tables in the SAMPLE database. Limit your list of tables to those in your SCHEMA: ADMIN. The command list at the top of the Control Center window contains the command View. Click View -> Filter. In Filter window under Comparison for Schema: Click drop-down arrow -> Equal to Under Values for Schema, type ADMIN. Click OK button to process. Examine ADMIN.ORG. Double-click icon next to ORG. In 'Alter Table - ORG' window, click Columns tab. What columns are found for ORG? DEPTNUMB, DEPTNAME, MANAGER, DIVISION, LOCATION Click Cancel to exit Alter Table - ORG window. Examine ADMIN.STAFF. Double-click icon next to STAFF. In Alter Table - STAFF window, click Columns tab. Copyright IBM Corp. 1996, 2003 Exercise 1. The Exercise Environment 1-5

Student Exercises 7. What columns are found for STAFF? ID, NAME, DEPT, JOB, YEARS, SALARY, COMM Click Cancel to exit Alter Table - STAFF window. Let's connect to the SAMPLE database for a quick look at our tables' contents. The command list at the top of the Control Center window contains the command Tools. Click Tools -> Command Center. In Command Center window, click Interactive tab (below icons line). In the Interactive tab Command window: Type in: Connect to SAMPLE Execute this DB2 command Click Gears icon on the icons line. The window below the Command window should show the following: connect to sample ; ------------------------------------------------- Database Connection Information 8. 9. Database server = DB2/NT 8.1.0 SQL authorization ID = ADMIN Local database alias = SAMPLE Let's look at the ORG table. Click SQL ASSIST button. Select ORG table by clicking icon next to ORG under ADMIN. Click > button to move ORG to Selected tables. Click Review tab in the SQL Assist window. SQL statement should read: SELECT * FROM ADMIN.ORG. Click Run button. How many rows are there currently in table ORG? ADMIN.ORG has 8 rows What are the DEPTNUMB values in ORG at this time? DEPTNUMB contains: 10, 15, 20, 38, 42, 51, 66, 84 Click OK to return to SQL Assist window. Click OK to return to Command Center window. Let's look at the STAFF table. Click SQL ASSIST button. Select STAFF table by clicking the icon next to STAFF under ADMIN. Click > button to move STAFF to Selected tables. Click Review tab in the SQL Assist window. SQL statement should read: SELECT * FROM ADMIN.STAFF. Click Run button. 1-6 Stored Procedures Copyright IBM Corp. 1996, 2003

V2.0 Student Exercises EXempty How many rows does STAFF currently consist of? ADMIN.STAFF has 35 rows Which employee has the highest value for YEARS in the STAFF table? Graham, employee ID 310 Is this employee the highest salaried employee? No, Graham is not the highest salaried employee. Click OK to return to SQL Assist window. Click OK to return to Command Center window. 10. Minimize the Command Center window. This completes the last section of this exercise. END OF LAB Copyright IBM Corp. 1996, 2003 Exercise 1. The Exercise Environment 1-7

Student Exercises 1-8 Stored Procedures Copyright IBM Corp. 1996, 2003

V2.0 Student Exercises EXempty Exercise 2. The DB2 Development Center What This Exercise Is About This exercise will have the students create stored procedures using the DB2 Development Center. What You Should Be Able to Do Introduction At the end of the lab, you should be able to: Maneuver through the Command Center and DB2 Development Center user interfaces Register a new project within the DB2 Development Center Create a simple stored procedure using SQL Assist Create a stored procedure without SQL Assist This exercise has several sections. Section 1: Registering a new project and creating a stored procedure Section 2: Using a stored procedure to create tables Section 3: Stored procedure source code listings In Section 1, you will register a project with the DB2 Development Center. After setting up a project, you will create a simple stored procedure using SQL Assist to SELECT from a table. In Section 2, you will create a stored procedure that will use SQL to create tables and load values into those tables. Section 3 provides a listing of the stored procedures source. Copyright IBM Corp. 1996, 2003 Exercise 2. The DB2 Development Center 2-1

Student Exercises Exercise Instructions The lab solutions can be found in Exercise Solutions on page 2-7. Section 1: Register a New Project 1. 2. 3. Start the DB2 Development Center. Starting with the WIN/NT Start icon (lower left on DESKTOP): Click Start ->Programs ->IBM DB2 ->Development Tools->Development Center. If the Launch Pad is displayed, close it. (You might want to check Do not show this again ). Register a new project with the DB2 Development Center. In IBM DB2 Development Center window menu: Click Project->New Project. Set the following fields as indicated: - Project name: CF71 - Project path: x:\cf71_project (where x is the your drive letter. This would be the D drive if you have one, otherwise C ). Click the OK button to complete project registration. Register a database connection. Right-click Database Connections, then select Add Connection. The connection Type should be Online. Click Next button. On the Connection panel, Driver should be IBM DB2 alias. Database Alias should be SAMPLE. Under User Information, check Use your current user id and password. Click the Finish button. Let's build an SQL stored procedure. Right-click Stored Procedures folder icon. Click New -> Stored Procedure Using Wizard. In the right hand pane, select SQL and click the OK button. In the Create SQL Stored Procedure Wizard window: - You should be in the Name panel; if not, click 1.Name. - Set Name: CF71.LAB2_1. On the Definition panel: - Click the tiny push button in the value field for Result set and select None. - Click the tiny push button in the value field for Statement. This takes you to the SQL Statement window. - Select Generate one SQL statement and click the SQL Assist... push button. In SQL Assistant window: - In the outline, click FROM. - Expand ADMIN. - Find STAFF table and click its icon. 2-2 Stored Procedures Copyright IBM Corp. 1996, 2003

V2.0 Student Exercises EXempty - Click > button to move STAFF to selected tables area. - In the Selected tables area, select SYSCAT.PROCE... and click the < button. - In the outline, click SELECT. - Expand ADMIN.STAFF. Click ID and then hold down the Shift key and click COMM. Now click the > button. - In the outline, click ORDER BY. - Expand ADMIN.STAFF. Click SALARY and then click the > button. The SQL statement generated is displayed in the SQL code area. Click OK button to return to SQL Statement panel and then click the OK button. In the Create SQL Stored Procedure Wizard - CF71.LAB2_1 window, click Finish button. (The default is to Build the stored procedure when you finish. This can be changed on the Option pane in which case you are responsible for Building the stored procedure). 4. 5. 6. 7. 8. 9. What messages did you receive in the Messages tab? Right-click your stored procedures and select Edit. You might have to resize your Edit window. Return to the Development Center desktop (click it in the Windows taskbar at the bottom of the screen). Right-click your stored procedure and select Run. You should see three messages as a result in the Message tab. To the left of the messages are all of the actions and their status that you have done. If you click the Parameter or Result tab (at the bottom of your message pane), you will see that they are empty. Why do you think this is so? Congratulations! You've just completed your first stored procedure. Copyright IBM Corp. 1996, 2003 Exercise 2. The DB2 Development Center 2-3

Student Exercises Section 2: Stored Procedure with CREATE TABLE 1. 2. 3. 4. Build a stored procedure to create tables CF71.STAFF and CF71.ORG. In the Project View, right-click Stored Procedures folder icon. Click New -> SQL Stored Procedure. Look for CREATE PROCEDURE DDL statement. Change the procedure name to CF71.LAB2_2. Position your cursor at end of line containing BEGIN. Press Enter to begin insert of new lines. Code in the following SQL statements: CREATE TABLE CF71.ORG LIKE ADMIN.ORG; CREATE TABLE CF71.STAFF LIKE ADMIN.STAFF; From the menu, click File->Save Object. Return to the Development Center desktop. Right-click your stored procedure (CF71.LAB2_2) and select Build. What messages did you receive in the Messages tab? Execute the new stored procedure. Right-click your stored procedure and select Run. What messages did you receive in the Result tab? Verify that the tables were created. From the Development Center desktop menu, select Tools->Control Center. Expand your DB2 system until the SAMPLE database is displayed. Now expand it as well. Click Tables under the SAMPLE database. In the list of tables displayed, you should see the two tables that your stored procedure created. A second verification. Return to the Edit window for your stored procedure. If you did not close the editor, then access it from the taskbar. If you closed it, then right-click your stored procedure and select Edit. Position the cursor anywhere in the display stored procedure. Click the SQL icon in the toolbar at the top of the window. The SQL Assist displays. 2-4 Stored Procedures Copyright IBM Corp. 1996, 2003

V2.0 Student Exercises EXempty Click FROM and expand CF71. You should see your two tables listed. Click the Cancel button. 5. 6. 7. Initialize your new tables with a stored procedure. Return to the Development Center desktop. Right-click the Stored Procedures folder icon. Click New -> SQL Stored Procedure. Look for CREATE PROCEDURE DDL statement. Change the procedure name to CF71.LAB2_3. Position your cursor at end of line containing BEGIN. Press Enter to begin insert of new lines. Code in the following SQL statements: INSERT INTO CF71.ORG SELECT * FROM ADMIN.ORG; INSERT INTO CF71.STAFF SELECT * FROM ADMIN.STAFF; On the Editor s menu, select File->Save Object. Return to the Development Center desktop. Right-click your stored procedure (CF71.LAB2_3) and select Build. You should once again see the successful build messages. Execute the new stored procedure by right-clicking it and selecting Run. Once again you should see three messages indicating a successful run. Validate data in your new tables. Return to the Editor for your stored procedure. Position the cursor in your stored procedure and click the SQL icon in the toolbar. In SQL Assist window: - Click FROM and expand CF71. - Double-click ORG icon under CF71 icon in Available tables list to move to Selected tables list. - Select the SYSCAT.PROCE... in the Selected Tables and click the < button. - Click Run button to execute. Did the Results windows show all 8 rows for CF71.ORG? Click OK button to exit Results window. You can use the same technique to verify the data in the CF71.STAFF table. You could also return to the Control Center and right-click the desired table and select Sample Contents. Saving your project. Return to the Development Center desktop. Right-click your project - CF71 and select Save Project. Copyright IBM Corp. 1996, 2003 Exercise 2. The DB2 Development Center 2-5

Student Exercises Section 3: Stored Procedure Source Code The source for the stored procedures created for this lab exercise will be found in Section 3 of the solutions. END OF LAB 2-6 Stored Procedures Copyright IBM Corp. 1996, 2003

V2.0 Student Exercises EXempty Exercise Solutions Section 1: Register a New Project 1. 2. 3. Start the DB2 Development Center. Starting with the WIN/NT Start icon (lower left on DESKTOP): Click Start ->Programs ->IBM DB2 ->Development Tools->Development Center. If the Launch Pad is displayed, close it. (You might want to check Do not show this again ). Register a new project with the DB2 Development Center. In IBM DB2 Development Center window menu: Click Project->New Project. Set the following fields as indicated: - Project name: CF71 - Project path: x:\cf71_project (where x is the your drive letter. This would be the D drive if you have one, otherwise C ). Click the OK button to complete project registration. Register a database connection. Right-click Database Connections, then select Add Connection. The connection Type should be Online. Click Next button. On the Connection panel, Driver should be IBM DB2 alias. Database Alias should be SAMPLE. Under User Information, check Use your current user id and password. Click the Finish button. Let's build an SQL stored procedure. Right-click Stored Procedures folder icon. Click New -> Stored Procedure Using Wizard. In the right hand pane, select SQL and click the OK button. In the Create SQL Stored Procedure Wizard window: - You should be in the Name panel; if not, click 1.Name. - Set Name: CF71.LAB2_1. On the Definition panel. - Click the tiny push button in the value field for Result set and select None. - Click the tiny push button in the value field for Statement. This takes you to the SQL Statement window. - Select Generate one SQL statement and click the SQL Assist... push button. In SQL Assistant window: - In the outline, click FROM. - Expand ADMIN. - Find STAFF table and click its icon. - Click > button to move STAFF to selected tables area. - In the Selected tables area, select SYSCAT.PROCE... and click the < button. Copyright IBM Corp. 1996, 2003 Exercise 2. The DB2 Development Center 2-7

Student Exercises 4. 5. 6. 7. 8. 9. - In the outline, click SELECT. - Expand ADMIN.STAFF. Click ID and then hold down the shift key and click COMM. Now click the > button. - In the outline, click ORDER BY. - Expand ADMIN.STAFF. Click SALARY and then click the > button. The SQL statement generated is displayed in the SQL code area. Click OK button to return to SQL Statement panel and then click the OK button. In the Create SQL Stored Procedure Wizard - CF71.LAB2_1 window, click Finish button. (The default is to Build the stored procedure when you finish. This can be changed on the Option pane in which case you are responsible for Building the stored procedure). What messages did you receive in the Messages tab? CF71.LAB2_1 - Build started. CF71.LAB2_1 - Create stored procedure completed. CF71.LAB2_1 - Build successful. Right-click your stored procedures and select Edit. You might have to resize your Edit window. Return to the Development Center desktop (click it in the Windows taskbar at the bottom of the screen). Right-click your stored procedure and select Run. You should see three messages as a result in the Message tab. To the left of the messages are all of the actions and their status that you have done. If you click the Parameter or Result tab (at the bottom of your message pane), you will see that they are empty. Why do you think this is so? Since there were no input parameters passed to your stored procedure, there is nothing listed on the Parameters tab. And since your stored procedure did not return any results, nothing will be displayed on the Results tab. Congratulations! You've just completed your first stored procedure. 2-8 Stored Procedures Copyright IBM Corp. 1996, 2003

V2.0 Student Exercises EXempty Section 2: Stored Procedure with CREATE TABLE 1. 2. 3. 4. Build a stored procedure to create tables CF71.STAFF and CF71.ORG. In the Project View, right-click Stored Procedures folder icon. Click New -> SQL Stored Procedure. Look for CREATE PROCEDURE DDL statement. Change the procedure name to CF71.LAB2_2. Position your cursor at end of line containing BEGIN. Press Enter to begin insert of new lines. Code in the following SQL statements: CREATE TABLE CF71.ORG LIKE ADMIN.ORG; CREATE TABLE CF71.STAFF LIKE ADMIN.STAFF; From the menu, click File->Save Object. Return to the Development Center desktop. Right-click your stored procedure (CF71.LAB2_2) and select Build. What messages did you receive in the Messages tab? CF71.LAB2_2 - Build started. CF71.LAB2_2 - Create stored procedure completed. CF71.LAB2_2 - Build successful. Execute the new stored procedure. Right-click your stored procedure and select Run. What messages did you receive in the Result tab? CF71.LAB2_2 - Run started. CF71.LAB2_2 - Calling the stored procedure. CF71.LAB2_2 - Run completed. Verify that the tables were created. From the Development Center desktop menu, select Tools->Control Center. Expand your DB2 system until the SAMPLE database is displayed. Now expand it as well. Click Tables under the SAMPLE database. In the list of tables displayed, you should see the two tables that your stored procedure created. A second verification. Return to the Edit window for your stored procedure. If you did not close the editor, then access it from the taskbar. If you closed it, then right-click your stored procedure and select Edit. Position the cursor anywhere in the display stored procedure. Click the SQL icon in the toolbar at the top of the window. The SQL Assist displays. Click FROM and expand CF71. You should see your two tables listed. Click Cancel button. Copyright IBM Corp. 1996, 2003 Exercise 2. The DB2 Development Center 2-9

Student Exercises 5. 6. 7. Initialize your new tables with a stored procedure. Return to the Development Center desktop. Right-click the Stored Procedures folder icon. Click New -> SQL Stored Procedure. Look for CREATE PROCEDURE DDL statement. Change the procedure name to CF71.LAB2_3. Position your cursor at end of line containing BEGIN. Press Enter to begin insert of new lines. Code in the following SQL statements: INSERT INTO CF71.ORG SELECT * FROM ADMIN.ORG; INSERT INTO CF71.STAFF SELECT * FROM ADMIN.STAFF; On the Editor s menu, select File->Save Object. Return to the Development Center desktop. Right-click your stored procedure (CF71.LAB2_3) and select Build. You should once again see the successful build messages. Execute the new stored procedure by right-clicking it and selecting Run. Once again you should see three messages indicating a successful run. Validate data in your new tables. Return to the Editor for your stored procedure. Position the cursor in your stored procedure and click the SQL icon in the toolbar. In SQL Assist window: - Click FROM and expand CF71. - Double-click ORG icon under CF71 icon in Available tables list to move to Selected tables list. - Select the SYSCAT.PROCE... in the Selected Tables and click the < button. - Click Run button to execute. Did the Results windows show all 8 rows for CF71.ORG? It should have. Click OK button to exit Results window. You can use the same technique to verify the data in the CF71.STAFF table. You could also return to the Control Center and right-click the desired table and select Sample Contents. Saving your project. Return to the Development Center desktop. Right-click your project - CF71 and select Save Project. 2-10 Stored Procedures Copyright IBM Corp. 1996, 2003

V2.0 Student Exercises EXempty Section 3: Stored Procedure Source Code 1. LAB2_1 CREATE PROCEDURE CF71.LAB2_1 ( ) ------------------------------------------------------------------------ -- SQL Stored Procedure ------------------------------------------------------------------------ P1: BEGIN -- Declare variable --DECLARE ENDTABLE INT DEFAULT 0; -- Declare cursor DECLARE cursor1 CURSOR FOR SELECT STAFF.ID, STAFF.NAME, STAFF.DEPT, STAFF.JOB, STAFF.YEARS, STAFF.SALARY, STAFF.COMM FROM ADMIN.STAFF AS STAFF ORDER BY STAFF.SALARY ASC; -- Declare handler --DECLARE CONTINUE HANDLER FOR NOT FOUND --SET ENDTABLE = 1; -- Sample code to access results. Goes with the lines -- "DECLARE ENDTABLE..." and "DECLARE CONTINUE..." shown above. --OPEN cursor1; --SET ENDTABLE = 0; --WHILE ENDTABLE = 0 DO --FETCH cursor1 INTO <declared variables> --END WHILE; --CLOSE cursor1; END P1 Copyright IBM Corp. 1996, 2003 Exercise 2. The DB2 Development Center 2-11

Student Exercises 2. LAB2_2 CREATE PROCEDURE CF71.LAB2_2 ( ) ------------------------------------------------------------------------ -- SQL Stored Procedure ------------------------------------------------------------------------ P1: BEGIN CREATE TABLE CF71.ORG LIKE ADMIN.ORG; CREATE TABLE CF71.STAFF LIKE ADMIN.STAFF; END P1 2-12 Stored Procedures Copyright IBM Corp. 1996, 2003

V2.0 Student Exercises EXempty 3. LAB2_3 CREATE PROCEDURE CF71.LAB2_3 ( ) ------------------------------------------------------------------------ -- SQL Stored Procedure ------------------------------------------------------------------------ P1: BEGIN INSERT INTO CF71.ORG SELECT * FROM ADMIN.ORG; INSERT INTO CF71.STAFF SELECT * FROM ADMIN.STAFF; END P1 END OF LAB Copyright IBM Corp. 1996, 2003 Exercise 2. The DB2 Development Center 2-13

Student Exercises 2-14 Stored Procedures Copyright IBM Corp. 1996, 2003

V2.0 Student Exercises EXempty Exercise 3. SQL Procedure Language What This Exercise Is About This exercise provides an opportunity for the students to build several stored procedures using SQL Procedure Language constructs. Some stored procedures will solve the same user requirement with different SQL Procedure Language constructs. What You Should Be Able to Do Introduction At the end of the lab, you should be able to: Create a stored procedure written in SQL Procedure Language Solve user requirements in more than one way with SQL Procedure Language Build and execute an SQL Procedure Language procedure with the DB2 Development Center This lab has several sections. Section 1: Create an SQL stored procedure using the CASE SQL Procedure Language statement. Section 2: Create an SQL stored procedure using the IF SQL Procedure Language statement. Section 3: Create an SQL stored procedure using the REPEAT SQL Procedure Language statement. Section 4: Create an SQL stored procedure using the LOOP SQL Procedure Language statement. Section 5: Create an SQL stored procedure that will return the median salary from the CF71.STAFF table. Section 6: Create an SQL stored procedure that will return the median salary from the CF71.STAFF table and a result set with the ID, NAME, and SALARY from all rows where the SALARY is greater than the median salary. Section 7: Stored procedure source code listings In Section 1, you will create a new stored procedure. That stored procedure will use the CASE construct to update the CF71.STAFF table. In Section 2, you will create a new stored procedure. That stored procedure will use the IF construct to update the CF71.STAFF table. Copyright IBM Corp. 1996, 2003 Exercise 3. SQL Procedure Language 3-1

Student Exercises Required Materials In Section 3, you will create a new stored procedure. That stored procedure will use the REPEAT construct to find rows in the CF71.STAFF table. In Section 4, you will create a new stored procedure. That stored procedure will use the LOOP construct to find rows in the CF71.STAFF table. In Section 5, you will create a new stored procedure. That stored procedure will need to determine the median salary of the CF71.STAFF table. This will require finding the number of rows in the CF71.STAFF table, opening a cursor on the table in SALARY order, and returning the median salary to the caller. In Section 6, you will create a new stored procedure. That stored procedure will need to determine the median salary of the CF71.STAFF table. Your procedure will return that median salary to the caller along with information from the CF71.STAFF table from rows where the SALARY is greater than the median salary. Section 7 provides a listing of the stored procedures source. Student notebook for SQL Procedure Language statement syntax. DB2 Info Center for SQL Procedure Language statement syntax (optional). 3-2 Stored Procedures Copyright IBM Corp. 1996, 2003

V2.0 Student Exercises EXempty Exercise Instructions The lab solutions can be found in Exercise Solutions on page 3-13. Section 1: CASE Construct This section will have you build a stored procedure, CF71.LAB3_1, to satisfy a request to update your CF71.STAFF table. The update will be different based on parameters passed to the stored procedure. To accomplish this, you will use the CASE SQL Procedure Language statement. 1. 2. 3. 4. 5. 6. 7. You've been asked to provide a stored procedure that will increase an employee's salary in your CF71.STAFF table based on a rating of 1, 2, or 3. A rating of 1 will warrant a 10% increase. A rating of 2 will warrant a 5% increase. If not a 1 or 2, increase the employee's salary by 2.5%. Use the CASE SQL Procedure Language construct to accomplish this. The new procedure should return the old salary and the updated salary. Right-click Stored Procedures folder icon. Click New -> SQL Stored Procedure. Look for CREATE PROCEDURE DDL statement. Change the procedure name to CF71.LAB3_1. Define the parameters your new procedure will need. First parameter should be SMALLINT to receive an employee's ID Second parameter should be SMALLINT to receive the rating of 1, 2, or 3 Third parameter should be DEC(7,2) to return the old salary Fourth parameter should be DEC(7,2) to return the new salary Include DECLAREs for the following: SQLSTATE A condition named not_found for SQLSTATE '02000' An exit handler for not_found that will return SQLSTATE '70444' Return the old salary in the third parameter you defined. Use the CASE construct for the updates to CF71.STAFF: CASE argument WHEN argumentvalue1 THEN... ; WHEN argumentvalue2 THEN... ; ELSE... ; END CASE; Return the updated salary in the fourth parameter you defined. Click the File->Save Object menu. Copyright IBM Corp. 1996, 2003 Exercise 3. SQL Procedure Language 3-3

Student Exercises 8. 9. Return to the Development Center desktop. Right-click your stored procedure (CF71.LAB3_1) and choose Build. (You could also select your stored procedure and then on the toolbar, click the Wrench icon). 10. Make sure that your stored procedure is selected and click the Run icon from the toolbar (the green triangle). Specify the variables for the procedure. Enter 310 for the employee ID number. Enter 2 for the rating. Click OK. What did you see in the Parameters panel? What did you see in the Messages panel? 11. Click Run icon from the icon list at top of window to execute your procedure. Specify the variables for the procedure. Enter 1776 for the employee ID number. Enter 2 for the rating. Click OK. What did you see in the Messages panel? 3-4 Stored Procedures Copyright IBM Corp. 1996, 2003

V2.0 Student Exercises EXempty Section 2: IF Construct This section will have you build a stored procedure, CF71.LAB3_2, to satisfy a request to update your CF71.STAFF table. The update will be different based on parameters passed to the stored procedure. To accomplish this, you will use the IF SQL Procedure Language statement. 1. 2. 3. 4. 5. 6. 7. 8. 9. You've been asked to provide a stored procedure that will increase an employee's salary in your CF71.STAFF table based on a rating of 1, 2, or 3. A rating of 1 will warrant a 10% increase. A rating of 2 will warrant a 5% increase. If not a 1 or 2, increase the employee's salary by 2.5%. Use the IF SQL Procedure Language construct to accomplish this. The new procedure should return the old salary and the updated salary. Right-click Stored Procedures folder icon. Click New -> SQL Stored Procedure. Look for CREATE PROCEDURE DDL statement. Change the procedure name to CF71.LAB3_2. Define the parameters your new procedure will need. First parameter should be SMALLINT to receive an employee's ID Second parameter should be SMALLINT to receive the rating of 1, 2, or 3 Third parameter should be DEC(7,2) to return the old salary Fourth parameter should be DEC(7,2) to return the old salary Include DECLAREs for the following: SQLSTATE A condition named not_found for SQLSTATE '02000' An exit handler for not_found that will return SQLSTATE '70444' Return the old salary in the third parameter you defined. Use the IF construct for the updates to CF71.STAFF. IF cond1 THEN...; ELSEIF cond2 THEN...; ELSE...; END IF; Return the updated salary in the fourth parameter you defined. Save your stored procedure and return to the Development Center desktop. Select your CF71.LAB3_2 stored procedure and Build it. Now Run the stored procedure. Copyright IBM Corp. 1996, 2003 Exercise 3. SQL Procedure Language 3-5

Student Exercises Specify the variables for the procedure. Enter 310 for the employee ID number. Enter 1 for the rating. Click OK. What did you see in the Parameters panel? What did you see in the Messages panel? 3-6 Stored Procedures Copyright IBM Corp. 1996, 2003

V2.0 Student Exercises EXempty Section 3: REPEAT Construct This section will have you build a stored procedure, CF71.LAB3_3, to satisfy a request for information from the CF71.STAFF table. You will use the REPEAT SQL Procedure Language statement to accomplish this. 1. 2. 3. 4. 5. 6. Our database manager has a peculiar glitch; the MAX function is temporarily not working! You have been asked to write a stored procedure to return the ID and salary of the highest paid employee in the CF71.STAFF table. Use the REPEAT construct to find the this employee in your stored procedure. Right-click Stored Procedures folder icon. Click New -> SQL Stored Procedure. Look for CREATE PROCEDURE DDL statement. Change the procedure name to CF71.LAB3_3. Define the parameters your new procedure will need. First parameter should be SMALLINT to return the employee ID Second parameter should be DEC(7,2) to return the employee salary Include the following DECLARES: at_end a small integer initialized to 0. not_found as a condition for SQLSTATE '02000'. The cursor you will need to process the request. Declare a continue handler for not_found that sets at_end to a value of 1. You will need to OPEN and CLOSE your cursor within your stored procedure. Use the REPEAT construct to find the last employee ID for CF71.STAFF. repeat_loop_label: REPEAT... UNTIL condition END REPEAT repeat_loop_label; Click Build icon from the icon list at top of window to build your procedure. Click Run icon from the icon list at top of window to execute your procedure. What did you see in the Parameters panel? What did you see in the Messages panel? Copyright IBM Corp. 1996, 2003 Exercise 3. SQL Procedure Language 3-7

Student Exercises Section 4: LOOP Construct This section will have you build a stored procedure, CF71.LAB3_4, to satisfy a request for information from the CF71.STAFF table. You will use the LOOP SQL Procedure Language statement to accomplish this. 1. 2. 3. 4. 5. 6. 7. 8. 9. Each week one of our staff is picked to be on call. This is done rather randomly by using the first number picked in our state lottery's PICK 3 game. This week the number is 5. You have been asked to write a stored procedure that will accept this number and pick out the employee that is in that row when ordered by name. Right-click Stored Procedures folder icon. Click New -> SQL Stored Procedure. Look for CREATE PROCEDURE DDL statement. Change the procedure name to CF71.LAB3_4. Define the parameters your new procedure will need. First parameter should be INT to receive the PICK 3 number. Second parameter should be SMALLINT to return the employee ID. Third parameter should be varchar(9) to return the employee's name. DECLARE the following: v_ctr an integer initialized to 1 at_end a small integer initialized to 0 not_found a condition for SQLSTATE '02000' DECLARE a cursor. Declare a continue handler for not_found that sets at_end to 1:; Use LOOP construct to find nth employee ID and name. loop_label: LOOP... END LOOP loop_label; Save your stored procedure and return to the Development Center desktop. Select your stored procedure and Build it. Run your stored procedure. Specify the variable for the procedure. For this exercise, enter a value of 5. Click OK. What did you see in the Parameters panel? 3-8 Stored Procedures Copyright IBM Corp. 1996, 2003

V2.0 Student Exercises EXempty Section 5: Median Salary This section will have you build a stored procedure, CF71.LAB3_5, that will return the median salary from the CF71.STAFF table. 1. 2. 3. 4. 5. 6. 7. 8. 9. You've been asked to provide a stored procedure that will return the median salary of employees in the CF71.STAFF table. Right-click Stored Procedures folder icon. Click New -> SQL Stored Procedure. Look for CREATE PROCEDURE DDL statement. Change the procedure name to CF71.LAB3_5. Define the parameters your new procedure will need. First, and only, parameter should be DEC(7,2) to return the median salary. You will need some local variables. Include the following local DECLARES: v_numrecords as integer initialized to 1 v_counter as integer initialized to 0 DECLARE a cursor that will return the salaries in order. Declare a continue handler for the not found condition that will set mediansalary to 6666. Write the code for determining the median salary. Initialize your median salary variable to zero. Determine the number of employees. Open your cursor. Use the WHILE construct to determine the median salary. WHILE cond DO... END WHILE; Good programming technique would dictate closing the cursor you were processing. Save your stored procedure and return to the Development Center desktop. Build the stored procedure and then Run it. What did you see in the Parameters panel? Copyright IBM Corp. 1996, 2003 Exercise 3. SQL Procedure Language 3-9

Student Exercises Section 6: Creating a Result Set This section will have you build a stored procedure, CF71.LAB3_6, that will return a result set. 1. 2. 3. 4. 5. 6. 7. 8. In this section, you will build a stored procedure, CF71.LAB3_6, that will return the median salary from CF71.STAFF. In addition, the ID, name, and salary of all employees in the table with a salary greater than the median will be returned in a result set. The stored procedure CF71.LAB3_5 already has most of what we need. Bring the CF71.LAB3_5 procedure into the Edit window of the DB2 Development Center. Select and copy all statements from CF71.LAB3_5. You will now use that procedure as a basis for your new one. Right-click Stored Procedures folder icon. Click New -> SQL Stored Procedure. Select and delete all statements. Paste the statements you copied from CF71.LAB3_5. Look for CREATE PROCEDURE DDL statement. Change the procedure name to CF71.LAB3_6. Insert the following after the procedure description comment: -- Returns result set of salaries > median salary Specify the correct parameter to allow a median salary to be returned and specify that a single result set will be returned. Change the cursor to include all the columns requested and return a result set. Change the FETCH to handle the ID and name. Don't forget to leave the cursor open after finding the median. Save the stored procedure and return to the Development Center desktop. Build your stored procedure and then Run it. What did you see in the Parameters panel? What did you see in the Results panel? Notice that since this time you are returning a result set, you will see something in the Result panel. 3-10 Stored Procedures Copyright IBM Corp. 1996, 2003