DB2-StoredProcedure2

Embed Size (px)

Citation preview

  • 7/28/2019 DB2-StoredProcedure2

    1/7

    Stored procedures

    Stored procedures can help improve application performance and reduce database access traffic. Alldatabase access must go across the network, which, in some cases, can result in poor performance.For each SQL statement, a database manager application must initiate a separate communication withDB2. To improve application performance, you can create stored procedures that run on your

    database server. A client application can then simply call the stored procedures to obtain results of theSQL statements that are contained in the procedure. Because the stored procedure runs the SQLstatement on the server for you, database performance is improved. In addition, stored procedurescan help to centralize business logic. If you make changes to a stored procedure, the changes areimmediately available to all client applications that use it.

    Stored procedures are programs that have the following characteristics:

    Contain procedural constructs with SQL statements

    Are stored in databases and run on DB2 servers

    Can be called by name by an application that is using SQL

    Allow an application program to run in two parts: the application on the client and the stored

    procedure on the serverThe following figures show how two client applications access a database located on a database

    server. A client application that does not use stored procedures to access a database can requiremore network traffic. A client application that takes advantage of a stored procedure can help toreduce network traffic and the number of times the database is accessed. A typical stored procedurecontains one or more SQL statements and some manipulative or logical processing in a programminglanguage. The client application program uses the SQL CALL statement to start the stored procedure.When the stored procedure runs, it locally accesses the database on the database server and returnsrequested information to the client application.Figure 1. A client application that does not use a stored procedure

    Figure 2. A client application that uses a stored procedure

  • 7/28/2019 DB2-StoredProcedure2

    2/7

    To use a stored procedure, you need to write two programs: the stored procedure, which runs on adatabase server, and a client application, which runs on a client workstation or a middleware server(such as a Web server). The client application calls the stored procedure by using one of the availableAPI methods. A stored procedure follows certain conventions for exchanging data with a clientapplication.

    A stored procedure does not connect to the database, but relies on the database connection alreadyestablished by the client. The call across the network includes parameters that are required by thestored procedure. The stored procedure uses the parameters to complete its logic when it runs. It canreturn a set of values, rows, or modified parameters to the calling client application.

    The Development Center is designed to give you a full development environment for working withstored procedures, from creating to deploying.

    Related tasksBuilding routinesExporting routinesImporting routinesEditing source codeFiltering in the Server ViewDropping routines or objects from a databaseRemoving routines or objects from a project

    Related informationCreating stored proceduresRunning routinesDebuggingDeploying routines

    Benefits of using stored procedures

    Applications that use stored procedures have the following advantages:

    Reduced network usage between clients and servers

    A client application passes control to a stored procedure on the database server. The storedprocedure performs intermediate processing on the database server, without transmittingunnecessary data across the network. Only the records that are actually required by the client

    http://publib.boulder.ibm.com/infocenter/db2help/topic/com.ibm.db2.udb.dc.doc/dc/t_build.htm#t_buildhttp://publib.boulder.ibm.com/infocenter/db2help/topic/com.ibm.db2.udb.dc.doc/dc/t_export.htm#t_exporthttp://publib.boulder.ibm.com/infocenter/db2help/topic/com.ibm.db2.udb.dc.doc/dc/t_import.htm#t_importhttp://publib.boulder.ibm.com/infocenter/db2help/topic/com.ibm.db2.udb.dc.doc/dc/t_editcode.htm#t_editcodehttp://publib.boulder.ibm.com/infocenter/db2help/topic/com.ibm.db2.udb.dc.doc/dc/t_filter.htm#t_filterhttp://publib.boulder.ibm.com/infocenter/db2help/topic/com.ibm.db2.udb.dc.doc/dc/t_drop.htm#t_drophttp://publib.boulder.ibm.com/infocenter/db2help/topic/com.ibm.db2.udb.dc.doc/dc/t_remove.htm#t_removehttp://publib.boulder.ibm.com/infocenter/db2help/topic/com.ibm.db2.udb.dc.doc/dc/t_spcreate.htm#t_spcreatehttp://publib.boulder.ibm.com/infocenter/db2help/topic/com.ibm.db2.udb.dc.doc/dc/t_run.htm#t_runhttp://publib.boulder.ibm.com/infocenter/db2help/topic/com.ibm.db2.udb.dc.doc/dc/t_debug.htm#t_debughttp://publib.boulder.ibm.com/infocenter/db2help/topic/com.ibm.db2.udb.dc.doc/dc/t_deploy.htm#t_deployhttp://publib.boulder.ibm.com/infocenter/db2help/topic/com.ibm.db2.udb.dc.doc/dc/t_build.htm#t_buildhttp://publib.boulder.ibm.com/infocenter/db2help/topic/com.ibm.db2.udb.dc.doc/dc/t_export.htm#t_exporthttp://publib.boulder.ibm.com/infocenter/db2help/topic/com.ibm.db2.udb.dc.doc/dc/t_import.htm#t_importhttp://publib.boulder.ibm.com/infocenter/db2help/topic/com.ibm.db2.udb.dc.doc/dc/t_editcode.htm#t_editcodehttp://publib.boulder.ibm.com/infocenter/db2help/topic/com.ibm.db2.udb.dc.doc/dc/t_filter.htm#t_filterhttp://publib.boulder.ibm.com/infocenter/db2help/topic/com.ibm.db2.udb.dc.doc/dc/t_drop.htm#t_drophttp://publib.boulder.ibm.com/infocenter/db2help/topic/com.ibm.db2.udb.dc.doc/dc/t_remove.htm#t_removehttp://publib.boulder.ibm.com/infocenter/db2help/topic/com.ibm.db2.udb.dc.doc/dc/t_spcreate.htm#t_spcreatehttp://publib.boulder.ibm.com/infocenter/db2help/topic/com.ibm.db2.udb.dc.doc/dc/t_run.htm#t_runhttp://publib.boulder.ibm.com/infocenter/db2help/topic/com.ibm.db2.udb.dc.doc/dc/t_debug.htm#t_debughttp://publib.boulder.ibm.com/infocenter/db2help/topic/com.ibm.db2.udb.dc.doc/dc/t_deploy.htm#t_deploy
  • 7/28/2019 DB2-StoredProcedure2

    3/7

    application are transmitted. Using a stored procedure can result in reduced network usageand better overall performance.

    Applications that execute SQL statements one at a time typically cross the network twice foreach SQL statement. A stored procedure can group SQL statements together, making itnecessary to only cross the network twice for each group of SQL statements. The more SQLstatements that you group together in a stored procedure, the more you reduce networkusage and the time that database locks are held. Reducing network usage and the length ofdatabase locks improves overall network performance and reduces lock contention problems.

    Applications that process large amounts of SQL-generated data, but present only a subset ofthe data to the user, can generate excessive network usage because all of the data isreturned to the client before final processing. A stored procedure can do the processing on theserver, and transmit only the required data to the client, which reduces network usage.

    Enhanced hardware and software capabilities

    Applications that use stored procedures have access to increased memory and disk space onthe server computer. These applications also have access to software that is installed only onthe database server. You can distribute the executable business logic across machines thathave sufficient memory and processors.

    Improved security

    By including database privileges with stored procedures that use static SQL, the databaseadministrator (DBA) can improve security. The DBA or developer who builds the storedprocedure must have the database privileges that the stored procedure requires. Users of theclient applications that call the stored procedure do not need such privileges. This can reducethe number of users who require privileges.

    Reduced development cost and increased reliability

    In a database application environment, many tasks are repeated. Repeated tasks mightinclude returning a fixed set of data, or performing the same set of multiple requests to adatabase. By reusing one common procedure, a stored procedure can provide a highlyefficient way to address these recurrent situations.

    Centralized security, administration, and maintenance for common routines

    By managing shared logic in one place at the server, you can simplify security, administration,and maintenance . Client applications can call stored procedures that run SQL queries withlittle or no additional processing.

    Parent topic:Stored procedures

    Related conceptsLanguages for stored proceduresSQL stored proceduresJava stored procedures

    Languages for stored procedures

    Using the Development Center, you can create stored procedures in Java or SQL. Both languagescreate stored procedures that are highly portable from platform to platform. The language that youchoose depends on your development environment.

    SQL stored procedures

    http://publib.boulder.ibm.com/infocenter/db2help/topic/com.ibm.db2.udb.dc.doc/dc/c_sp.htmhttp://publib.boulder.ibm.com/infocenter/db2help/topic/com.ibm.db2.udb.dc.doc/dc/c_splanguage.htmhttp://publib.boulder.ibm.com/infocenter/db2help/topic/com.ibm.db2.udb.dc.doc/dc/c_spsql.htmhttp://publib.boulder.ibm.com/infocenter/db2help/topic/com.ibm.db2.udb.dc.doc/dc/c_spjava.htmhttp://publib.boulder.ibm.com/infocenter/db2help/topic/com.ibm.db2.udb.dc.doc/dc/c_sp.htmhttp://publib.boulder.ibm.com/infocenter/db2help/topic/com.ibm.db2.udb.dc.doc/dc/c_splanguage.htmhttp://publib.boulder.ibm.com/infocenter/db2help/topic/com.ibm.db2.udb.dc.doc/dc/c_spsql.htmhttp://publib.boulder.ibm.com/infocenter/db2help/topic/com.ibm.db2.udb.dc.doc/dc/c_spjava.htm
  • 7/28/2019 DB2-StoredProcedure2

    4/7

    SQL provides procedural constructs for writing stored procedures. Writing stored procedures with theSQL procedure language has the following advantages:

    You can support database modules that are written completely in an SQL language.

    You can quickly learn to write stored procedures with the SQL procedure language. This ease

    of learning is especially true if you have experience with other database languages.

    SQL stored procedures are fast because the SQL routines run as compiled routines.

    You can debug SQL stored procedures on Windows, AIX, Sun, and Linux on DB2 UniversalDatabase servers and on DB2 for z/OS Version 8 server.

    With SQL stored procedures, you can call other SQL procedures, nesting calls up to 16

    layers.SQL stored procedures have size and parameter limitations, depending on the version of DB2 that youare running:

    For DB2 for OS/390 and z/OS versions 6 and 7, the maximum size for SQL stored procedures

    is 32 KB.

    For DB2 for z/OS version 8, the maximum size for SQL stored procedures is 2MB

    For DB2 for Windows and UNIX versions 7 and 8.1, the maximum size for SQL stored

    procedures in is 64 KB.

    For DB2 version 8.2, the maximum size for SQL stored procedures is 2 MB

    Java stored procedures

    Writing stored procedures with Java has the following advantages:

    You can establish a common development environment and common language. This

    commonality can be shared by the stored procedures on the database server and the clientapplication that runs on a client workstation or a middleware server (such as a Web server).

    You can reuse code from Java methods that you have already written.

    The input/output of files is possible with Java stored procedures, within the security limits of

    Java. SQL stored procedures do not support the input/output of files.Java stored procedures have size and parameter limitations, depending on the version of DB2 that youare running:

    For DB2 for z/OS version 7, the maximum size for Java stored procedures is 32 KB.

    For DB2 for z/OS version 8, the maximum size for Java stored procedures is 10MB

    Restriction: For iSeries version 5 release 3, the following are not supported for Java storedprocedures:

    PARAMETER STYLE DB2GENERAL

    BINARY and VARBINARY built-in data types

    Parent topic:Stored procedures

    Related conceptsBenefits of using stored proceduresSQL stored proceduresJava stored procedures

    Related referenceName lengths and restrictions for identifiers and parameters

    SQL stored procedures

    An SQL stored procedure is a stored procedure in which the source code is part of the CREATEPROCEDURE statement. The part of the CREATE PROCEDURE statement that contains the code iscalled the stored procedure body.

    http://publib.boulder.ibm.com/infocenter/db2help/topic/com.ibm.db2.udb.dc.doc/dc/c_sp.htmhttp://publib.boulder.ibm.com/infocenter/db2help/topic/com.ibm.db2.udb.dc.doc/dc/c_spbenefits.htmhttp://publib.boulder.ibm.com/infocenter/db2help/topic/com.ibm.db2.udb.dc.doc/dc/c_spsql.htmhttp://publib.boulder.ibm.com/infocenter/db2help/topic/com.ibm.db2.udb.dc.doc/dc/c_spjava.htmhttp://publib.boulder.ibm.com/infocenter/db2help/topic/com.ibm.db2.udb.dc.doc/dc/r_lengths.htmhttp://publib.boulder.ibm.com/infocenter/db2help/topic/com.ibm.db2.udb.dc.doc/dc/c_sp.htmhttp://publib.boulder.ibm.com/infocenter/db2help/topic/com.ibm.db2.udb.dc.doc/dc/c_spbenefits.htmhttp://publib.boulder.ibm.com/infocenter/db2help/topic/com.ibm.db2.udb.dc.doc/dc/c_spsql.htmhttp://publib.boulder.ibm.com/infocenter/db2help/topic/com.ibm.db2.udb.dc.doc/dc/c_spjava.htmhttp://publib.boulder.ibm.com/infocenter/db2help/topic/com.ibm.db2.udb.dc.doc/dc/r_lengths.htm
  • 7/28/2019 DB2-StoredProcedure2

    5/7

    SQL stored procedure definitions provide the following information:

    The stored procedure name

    Parameter attributes

    The language in which the stored procedure is written. For an SQL stored procedure, the

    language is SQL

    Information about the SQL stored procedure that is used when the stored procedure is called.

    This information can include run-time options and whether the stored procedure returns resultsets

    Unlike a CREATE PROCEDURE statement for an external stored procedure, the CREATEPROCEDURE statement for an SQL stored procedure does not specify the EXTERNAL clause.Instead, an SQL stored procedure has a stored procedure body, which contains the source statementsfor the stored procedure.

    The following figures show example CREATE PROCEDURE statements for simple SQL storedprocedures. The second example shows a simple SQL stored procedure for z/OS. The storedprocedure name, the list of parameters that are passed to or from the stored procedure, and theLANGUAGE parameter are common to all stored procedures. The LANGUAGE value of SQL isparticular to an SQL stored procedure. Line numbers are included here for convenience.

    Figure 1. CREATE PROCEDURE for a simple SQL stored procedure1 CREATE PROCEDURE SCHEMA.Procedure6 ( INOUT var0 varchar(9) )

    2 LANGUAGE SQL

    --------------------------------------------------------------

    3 -- SQL stored procedure SCHEMA.Procedure6

    --------------------------------------------------------------

    4 P1: BEGIN

    -- Declare cursor

    DECLARE cursor1 CURSOR WITH RETURN FOR

    SELECT * FROM STAFF;

    END P1

    Line Description

    1The SQL stored procedure name isSCHEMA.Procedure6. The InOut parameter has data type varchar(9).

    2 LANGUAGE SQL indicates that this is an SQL stored procedure.

    3 Shows a comment for the SQL stored procedure.

    4 Begins the body of the SQL stored procedure. All SQL stored procedure bodies consist of one or more statements nested withina BEGIN and an END keyword.

    Figure 2. CREATE PROCEDURE for an SQL stored procedure for z/OS1 CREATE PROCEDURE SCHEMA.Proc1111 ( )

    RESULT SETS 1

    2 LANGUAGE SQL

    MODIFIES SQL DATA

    3 COLLID TEST

    4 WLM ENVIRONMENT WLMENV1

    5 ASUTIME NO LIMIT

    RUN OPTIONS 'NOTEST(NONE,*,*,*)'

    ----------------------------------------------------------

  • 7/28/2019 DB2-StoredProcedure2

    6/7

    6 -- SQL Stored Procedure

    ----------------------------------------------------------

    7 P1: BEGIN

    -- Declare cursor

    DECLARE cursor1 CURSOR WITH RETURN FOR

    SELECT SCHEMA, NAME FROM SYSIBM.SYSROUTINES;

    -- Cursor left open for client application

    OPEN cursor1;END P1

    Line Description

    1The SQL stored procedure name isSCHEMA.Proc1111.

    2 LANGUAGE SQL indicates that this is an SQL stored procedure.

    3 Spec ifies a collect ion ID of TEST.

    4 Specifies a Workload Manager (WLM) environment.

    5 Specifies no processor time limit for running the routine.

    6 Shows a comment for the SQL stored procedure.

    7 Begins the body of the SQL stored procedure. All SQL stored procedure bodies consist of one or more statements nested withina BEGIN and an END keyword.

    Parent topic:Stored procedures

    Related conceptsBenefits of using stored proceduresLanguages for stored proceduresJava stored procedures

    Related referenceSQL stored procedure

    Java stored procedures

    Java stored procedures built by the Development Center conform to the SQLJ (SQL in Java) Routinesspecification. Java stored procedures must follow these guidelines:

    The method that is mapped to the stored procedure must be defined as a public

    static void method.

    Output and InOut parameters must be set up as single element arrays.

    Java stored procedures can access DB2 data using either Java Database Connectivity (JDBC) APIcalls or SQLJ statements. The Development Center can create Java stored procedures that use eitherformat.

    http://publib.boulder.ibm.com/infocenter/db2help/topic/com.ibm.db2.udb.dc.doc/dc/c_sp.htmhttp://publib.boulder.ibm.com/infocenter/db2help/topic/com.ibm.db2.udb.dc.doc/dc/c_spbenefits.htmhttp://publib.boulder.ibm.com/infocenter/db2help/topic/com.ibm.db2.udb.dc.doc/dc/c_splanguage.htmhttp://publib.boulder.ibm.com/infocenter/db2help/topic/com.ibm.db2.udb.dc.doc/dc/c_spjava.htmhttp://publib.boulder.ibm.com/infocenter/db2help/topic/com.ibm.db2.udb.dc.doc/dc/r_samplespsql.htmhttp://publib.boulder.ibm.com/infocenter/db2help/topic/com.ibm.db2.udb.dc.doc/dc/c_sp.htmhttp://publib.boulder.ibm.com/infocenter/db2help/topic/com.ibm.db2.udb.dc.doc/dc/c_spbenefits.htmhttp://publib.boulder.ibm.com/infocenter/db2help/topic/com.ibm.db2.udb.dc.doc/dc/c_splanguage.htmhttp://publib.boulder.ibm.com/infocenter/db2help/topic/com.ibm.db2.udb.dc.doc/dc/c_spjava.htmhttp://publib.boulder.ibm.com/infocenter/db2help/topic/com.ibm.db2.udb.dc.doc/dc/r_samplespsql.htm
  • 7/28/2019 DB2-StoredProcedure2

    7/7

    The Development Center defines Java stored procedures in the catalog table with LANGUAGE JAVAand PARAMETER STYLE JAVA.Restriction: For iSeries version 5 release 3, the following are not supported for Java storedprocedures:

    PARAMETER STYLE DB2GENERAL

    BINARY and VARBINARY built-in data types

    Parent topic:Stored procedures

    Related conceptsBenefits of using stored proceduresLanguages for stored proceduresSQL stored procedures

    Related referenceJava stored procedure with dynamic SQL using JDBC database accessJava stored procedure with static SQL using SQLJ database access

    http://publib.boulder.ibm.com/infocenter/db2help/topic/com.ibm.db2.udb.dc.doc/dc/c_sp.htmhttp://publib.boulder.ibm.com/infocenter/db2help/topic/com.ibm.db2.udb.dc.doc/dc/c_spbenefits.htmhttp://publib.boulder.ibm.com/infocenter/db2help/topic/com.ibm.db2.udb.dc.doc/dc/c_splanguage.htmhttp://publib.boulder.ibm.com/infocenter/db2help/topic/com.ibm.db2.udb.dc.doc/dc/c_spsql.htmhttp://publib.boulder.ibm.com/infocenter/db2help/topic/com.ibm.db2.udb.dc.doc/dc/r_samplespjava1.htm#r_samplespjava1http://publib.boulder.ibm.com/infocenter/db2help/topic/com.ibm.db2.udb.dc.doc/dc/r_samplespjava2.htm#r_samplespjava2http://publib.boulder.ibm.com/infocenter/db2help/topic/com.ibm.db2.udb.dc.doc/dc/c_sp.htmhttp://publib.boulder.ibm.com/infocenter/db2help/topic/com.ibm.db2.udb.dc.doc/dc/c_spbenefits.htmhttp://publib.boulder.ibm.com/infocenter/db2help/topic/com.ibm.db2.udb.dc.doc/dc/c_splanguage.htmhttp://publib.boulder.ibm.com/infocenter/db2help/topic/com.ibm.db2.udb.dc.doc/dc/c_spsql.htmhttp://publib.boulder.ibm.com/infocenter/db2help/topic/com.ibm.db2.udb.dc.doc/dc/r_samplespjava1.htm#r_samplespjava1http://publib.boulder.ibm.com/infocenter/db2help/topic/com.ibm.db2.udb.dc.doc/dc/r_samplespjava2.htm#r_samplespjava2