16.10.2015 Views

Getting Started with IBM Data Studio for DB2

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

240 <strong>Getting</strong> <strong>Started</strong> <strong>with</strong> <strong>IBM</strong> <strong>Data</strong> <strong>Studio</strong> <strong>for</strong> <strong>DB2</strong><br />

4. View the output or results from your test run. When you run the stored procedure, you<br />

can determine whether it ran successfully, and whether its result sets are what you<br />

expect. You can also test the logic of the routine and the accuracy of output arguments<br />

and result sets. When you run a stored procedure from <strong>Data</strong> <strong>Studio</strong>, the results of the<br />

stored procedure are displayed in the SQL Results view.<br />

5. At this point, you could optionally use the routine editor to make changes to the stored<br />

procedure depending on your business requirements. The routine editor is a tool to<br />

view and edit the source code. You need to redeploy the stored procedure whenever<br />

there are any changes.<br />

6. Finally, the last step is to optionally debug the stored procedure, which requires that<br />

you actually deploy the stored procedure <strong>for</strong> debugging. In other words, there is an<br />

option on deployment that you must specify, to enable the integrated debugger. By<br />

stepping through your code while you are running in debug mode and viewing the<br />

results, you can discover problems <strong>with</strong> your stored procedure or better understand the<br />

functional behavior of your stored procedure in certain scenarios.<br />

8.3 Developing a stored procedure: An example<br />

In the following example, you will walk through the steps to create, test, deploy, debug and<br />

edit a <strong>DB2</strong> SQL stored procedure in <strong>Data</strong> <strong>Studio</strong>. In this example, we will create the stored<br />

procedure illustrated in Listing 8.1. This stored procedure accepts one input parameter and<br />

returns one output parameter <strong>with</strong> the objective of testing a conditionalIF statement.<br />

CREATE PROCEDURE SP1 (IN p_in INT, OUT p_out INT)<br />

-- DECLARE an input and output parameter<br />

P1: BEGIN<br />

END P1<br />

-- Code an IF statement<br />

IF p_in = 1 THEN<br />

SET p_out = 2;<br />

ELSEIF p_in = 2 THEN<br />

ELSE<br />

END IF;<br />

SET p_out = 3;<br />

SET p_out = 4;<br />

Listing 8.1 – A sample SQL stored procedure<br />

8.3.1 Create a data development project<br />

In this example, we use the same workspace and database that was used in previous<br />

chapters.<br />

<br />

Open <strong>IBM</strong> SQL and Routine Development perspective by selecting Window -> Open<br />

Perspective -> Other, then select <strong>IBM</strong> SQL and Routine Development in the Open<br />

Perspective window.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!