Sunteți pe pagina 1din 6

Web Maps News Orkut Books Translate Gmail more ▼

style_sri15@yahoo.com | settings | older version | sign out


HomeProfilewith meCommunities

search

ORACLE
(72 members)

forum

polls
members
view profile
SQL Material
Home > Communities > Computers & Internet > ORACLE > Forum > Messages
first | < previous | next > | last showing 21-30 of 65
9 Oct
▐►♥ ఓయ™ ♥◄▌

The result-set will look like this:

E_Name

Hansen, Ola
Svendson, Tove
Svendson, Stephen
Pettersen, Kari
Turner, Sally
Kent, Clark
Scott, Stephen

Note: This command cannot be used to list all employees in Norway and USA. In the example
above we have two employees with equal names, and only one of them will be listed. The UNION
command selects only distinct values.

SQL UNION ALL Example

Now we want to list all employees in Norway and USA:

SELECT E_Name FROM Employees_Norway


UNION ALL
SELECT E_Name FROM Employees_USA

Result

E_Name

Hansen, Ola
Svendson, Tove
Svendson, Stephen
Pettersen, Kari
Turner, Sally
Kent, Clark
Svendson, Stephen
Scott, Stephen
10 Oct
▐►♥ ఓయ™ ♥◄▌

Stored Procedures

A procedure (often called a stored procedure) is a program that can be called to perform
operations that can include both host language statements and SQL statements. Procedures in
SQL provide the same benefits as procedures in a host language.

DB2® SQL for iSeries stored procedure support provides a way for an SQL application to define
and then call a procedure through SQL statements. Stored procedures can be used in both
distributed and non-distributed DB2 SQL for iSeries applications. One of the big advantages in
using stored procedures is that for distributed applications, the execution of one CALL statement
on the application requester, or client, can perform any amount of work on the application server.

You may define a procedure as either an SQL procedure or an external procedure. An external
procedure can be any supported high level language program (except System/36* programs and
procedures) or a REXX procedure. The procedure does not need to contain SQL statements, but
it may contain SQL statements. An SQL procedure is defined entirely in SQL, and can contain
SQL statements that include SQL control statements.

Coding stored procedures requires that the user understand the following:

* Stored procedure definition through the CREATE PROCEDURE statement


* Stored procedure invocation through the CALL statement
* Parameter passing conventions
* Methods for returning a completion status to the program invoking the procedure.

You may define stored procedures by using the CREATE PROCEDURE statement. The
CREATE PROCEDURE statement adds procedure and parameter definitions to the catalog
tables SYSROUTINES and SYSPARMS. These definitions are then accessible by any SQL CALL
statement on the system.

To create an external procedure or an SQL procedure, you can use the SQL CREATE
PROCEDURE statement.
10 Oct
▐►♥ ఓయ™ ♥◄▌

The following sections describe the SQL statements used to define and call the stored procedure,
information about passing parameters to the stored procedure, and examples of stored procedure
usage.

* Defining an external procedure


* Defining an SQL procedure
* Invoking a stored procedure
* Returning Result Sets from Stored Procedures
* Parameter passing conventions for stored procedures and UDFs
* Indicator variables and stored procedures
* Returning a completion status to the calling program

For a description of stored procedures coded in Java™, see Java SQL Routines in the IBM®
Developer Kit for Java topic.

For information about using stored procedures with DRDA®, see DRDA stored procedure
considerations.
Note:
See Code disclaimer information for information pertaining to code examples.

10 Oct
▐►♥ ఓయ™ ♥◄▌

Defining an external procedure

The CREATE PROCEDURE statement for an external procedure:

* Names the procedure


* Defines the parameters and their attributes
* Gives other information about the procedure which the system uses when it calls the procedure.

Consider the following example:

CREATE PROCEDURE P1
(INOUT PARM1 CHAR(10))
EXTERNAL NAME MYLIB.PROC1
LANGUAGE C
GENERAL WITH NULLS

This CREATE PROCEDURE statement:

* Names the procedure P1


* Defines one parameter which is used both as an input parameter and an output parameter. The
parameter is a character field of length ten. Parameters can be defined to be type IN, OUT, or
INOUT. The parameter type determines when the values for the parameters get passed to and
from the procedure.
* Defines the name of the program which corresponds to the procedure, which is PROC1 in
MYLIB. MYLIB.PROC1 is the program which is called when the procedure is called on a CALL
statement.
* Indicates that the procedure P1 (program MYLIB.PROC1) is written in C. The language is
important since it impacts the types of parameters that can be passed. It also affects how the
parameters are passed to the procedure (for example, for ILE C procedures, a NUL-terminator is
passed on character, graphic, date, time, and timestamp parameters).
* Defines the CALL type to be GENERAL WITH NULLS. This indicates that the parameter for the
procedure can possibly contain the NULL value, and therefore will like an additional argument
passed to the procedure on the CALL statement. The additional argument is an array of N short
integers, where N is the number of parameters that are declared in the CREATE PROCEDURE
statement. In this example, the array contains only one element since there is only parameter.
10 Oct
▐►♥ ఓయ™ ♥◄▌

It is important to note that it is not necessary to define a procedure in order to call it. However, if
no procedure definition is found, either from a prior CREATE PROCEDURE or from a DECLARE
PROCEDURE in this program, certain restrictions and assumptions are made when the
procedure is called on the CALL statement. For example, the NULL indicator argument cannot be
passed. See Using embedded CALL statement where no procedure definition exists for an
example of a CALL statement without a corresponding procedure definition.
10 Oct
▐►♥ ఓయ™ ♥◄▌

Using embedded CALL statement where no procedure definition exists

A static CALL statement without a corresponding CREATE PROCEDURE statement is processed


with the following rules:

* All host variable arguments are treated as INOUT type parameters.


* The CALL type is GENERAL (no indicator argument is passed).
* The program to call is determined based on the procedure name specified on the CALL, and, if
necessary, the naming convention.
* The language of the program to call is determined based on information retrieved from the
system about the program.

Example: Embedded CALL Statement Where No Procedure Definition Exists

The following is a PL/I example of an embedded CALL statement where no procedure definition
exists:

DCL HV2 CHAR(10);


:
EXEC SQL CALL P2 (:HV2);
:

When the CALL statement is issued, DB2® SQL for iSeries attempts to find the program based
on standard SQL naming conventions. For the above example, assume that the naming option of
*SYS (system naming) is used and that a DFTRDBCOL parameter was not specified on the
CRTSQLPLI command. In this case, the library list is searched for a program named P2. Since
the call type is GENERAL, no additional argument is passed to the program for indicator
variables.
Note:
If an indicator variable is specified on the CALL statement and its value is less than zero when
the CALL statement is executed, an error results because there is no way to pass the indicator to
the procedure.

Assuming program P2 is found in the library list, the contents of host variable HV2 are passed in
to the program on the CALL and the argument returned from P2 is mapped back to the host
variable after P2 has completed execution.
Note:
See Code disclaimer for details pertaining to code examples.

10 Oct
▒▓Sravan

10 Oct
▐►♥ ఓయ™ ♥◄▌

Code disclaimer

This document contains programming examples.

Start of changeSUBJECT TO ANY STATUTORY WARRANTIES WHICH CANNOT BE


EXCLUDED, IBM®, ITS PROGRAM DEVELOPERS AND SUPPLIERS MAKE NO
WARRANTIES OR CONDITIONS EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OR CONDITIONS OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT, REGARDING THE
PROGRAM OR TECHNICAL SUPPORT, IF ANY.End of change

Start of changeUNDER NO CIRCUMSTANCES IS IBM, ITS PROGRAM DEVELOPERS OR


SUPPLIERS LIABLE FOR ANY OF THE FOLLOWING, EVEN IF INFORMED OF THEIR
POSSIBILITY:End of change

1. Start of change LOSS OF, OR DAMAGE TO, DATA;


2. SPECIAL, INCIDENTAL, OR INDIRECT DAMAGES, OR FOR ANY ECONOMIC
CONSEQUENTIAL DAMAGES; OR
3. LOST PROFITS, BUSINESS, REVENUE, GOODWILL, OR ANTICIPATED SAVINGS.
End of change

Start of changeSOME JURISDICTIONS DO NOT ALLOW THE EXCLUSION OR LIMITATION


OF INCIDENTAL OR CONSEQUENTIAL DAMAGES, SO SOME OR ALL OF THE ABOVE
LIMITATIONS OR EXCLUSIONS MAY NOT APPLY TO YOU.End of change

10 Oct
▐►♥ ఓయ™ ♥◄▌

Defining an SQL procedure

The CREATE PROCEDURE statement for SQL procedures:

* Names the procedure


* Defines the parameters and their attributes
* Provides other information about the procedure which will be used when the procedure is called
* Defines the procedure body. The procedure body is the executable part of the procedure and is
a single SQL statement.

Consider the following simple example that takes as input an employee number and a rate and
updates the employee's salary:

CREATE PROCEDURE UPDATE_SALARY_1


(IN EMPLOYEE_NUMBER CHAR(10),
IN RATE DECIMAL(6,2))
LANGUAGE SQL MODIFIES SQL DATA
UPDATE CORPDATA.EMPLOYEE
SET SALARY = SALARY * RATE
WHERE EMPNO = EMPLOYEE_NUMBER

This CREATE PROCEDURE statement:

* Names the procedure UPDATE_SALARY_1.


* Defines parameter EMPLOYEE_NUMBER which is an input parameter and is a character data
type of length 6 and parameter RATE which is an input parameter and is a decimal data type.
* Indicates the procedure is an SQL procedure that modifies SQL data.
* Defines the procedure body as a single UPDATE statement. When the procedure is called, the
UPDATE statement is executed using the values passed for EMPLOYEE_NUMBER and RATE.
10 Oct
▐►♥ ఓయ™ ♥◄▌

Instead of a single UPDATE statement, logic can be added to the SQL procedure using SQL
control statements. SQL control statements consist of the following:

* an assignment statement
* a CALL statement
* a CASE statement
* a compound statement
* a FOR statement
* a GET DIAGNOSTICS statement
* a GOTO statement
* an IF statement
* an ITERATE statement
* a LEAVE statement
* a LOOP statement
* a REPEAT statement
* a RESIGNAL statement
* a RETURN statement
* a SIGNAL statement
* a WHILE statement

The following example takes as input the employee number and a rating that was received on the
last evaluation. The procedure uses a CASE statement to determine the appropriate increase and
bonus for the update:

CREATE PROCEDURE UPDATE_SALARY_2


(IN EMPLOYEE_NUMBER CHAR(6),
IN RATING INT)
LANGUAGE SQL MODIFIES SQL DATA
CASE RATING
WHEN 1 THEN
UPDATE CORPDATA.EMPLOYEE
SET SALARY = SALARY * 1.10,
BONUS = 1000
WHERE EMPNO = EMPLOYEE_NUMBER;
WHEN 2 THEN
UPDATE CORPDATA.EMPLOYEE
SET SALARY = SALARY * 1.05,
BONUS = 500
WHERE EMPNO = EMPLOYEE_NUMBER;
ELSE
UPDATE CORPDATA.EMPLOYEE
SET SALARY = SALARY * 1.03,
BONUS = 0
WHERE EMPNO = EMPLOYEE_NUMBER;
END CASE
first | < previous | next > | last
report spam reply
« back to topics
powered by go to orkut.com | about orkut | blog | developers | safety centre | privacy | terms |
advertise | help

S-ar putea să vă placă și