Professional Documents
Culture Documents
DB2 Multi Fetch and Insert
DB2 Multi Fetch and Insert
DB2 Multi Fetch and Insert
0*
Contents
CONTENTS..................................................................................................................2 1.INTRODUCTION.....................................................................................................2 2. INSERTING MULTIPLE ROWS..........................................................................3
3.FETCHING MULTIPLE ROWS.................................................................................................................4 3.1DECLARING A ROW-SET POSITIONED CURSOR..................................................................................4 3.2USING A MULTIPLE-ROW FETCH STATEMENT WITH HOST VARIABLE ARRAYS...............................4 3.3USING A MULTIPLE-ROW FETCH STATEMENT WITH A DESCRIPTOR................................................5 3.4USING ROW-SET POSITIONED UPDATE STATEMENTS......................................................................5
0.In$r 1!&$i n
You can enhance the performance of your application programs by using multiple-row INSERT and FETC statements to re!uest that "#$ send multiple rows of data at one time to and from the database% For local applications& using these multiple-row statements results in fewer accesses of the database% For distributed applications& using these multiple-row statements results in fewer networ' operations and a significant impro(ement in performance% This topic pro(ides an o(er(iew of how you can) Insert multiple rows of data from host (ariable arrays that ha(e been declared and populated in your application program into the database%
Fetch multiple rows of data from the database into host (ariable arrays that ha(e been declared or dynamically allocated in your program%
To use a host (ariable array in an S*+ statement& specify a host (ariable array that is declared according to host language rules% You can specify host (ariable arrays in C& C,,& C-#-+& and .+/I application programs% You must declare the array in the host program before you use it in an S*+ statement%You can also use a storage area that you allocate dynamically when you use a descriptor to describe the data areas that you want "#$ to use to insert or place the data% You can specify a descriptor in assembler& C& C,,& C-#-+& and .+/I application programs% You must include an S*+ descriptor area 0S*+"12 in the host program
E7EC4TE statement represents a parameter mar'er in the INSERT statement% If you use an S*+"1& the host (ariable in the 4SIN> clause of the E7EC4TE statement names the S*+"1 that describes the parameter mar'ers in the INSERT statement%
4.0 (2&"aring a r 3-'2$ # 'i$i n21 &!r' r You must first declare a row-set positioned cursor before you can retrie(e row sets of data% To enable a cursor to fetch row sets& use the 6IT R-6SET .-SITI-NIN> clause in the "EC+1RE C4RS-R statement% E3ample) The following statement declares a row set cursor) E7EC S*+ "EC+1RE C9 C4RS-R 6IT R-6SET .-SITI-NIN> F-R SE+ECT E5.N-& +1STN15E& S1+1RY FR-5 "SN889:%E5. EN"-E7EC% To tell "#$ that you are ready to process the first row set of the result table& e3ecute the -.EN statement in your program% "#$ then uses the SE+ECT statement within the "EC+1RE C4RS-R statement to identify the rows in the result table . 4.* U'ing a m!"$i#"2-r 3 ,ETC- '$a$2m2n$ 3i$5 5 '$ 6aria7"2 array' 6hen your program e3ecutes a FETC statement with the R-6SET 'eyword& the cursor is positioned on a row set in the result table% That row set is called the current row set% "eclare the dimension of each of the host (ariable arrays to be greater than or e!ual to the number of rows that are to be retrie(ed .
E3ample) The following FETC statement retrie(es $: rows into host (ariable arrays that are declared in your program: E7EC S*+ FETC NE7T R-6SET FR-5 C9 F-R $: R-6S INT- ) <1-E5.N-& ) <1-+1STN15E& ) <1-S1+1RY )IN"1-S1+1RY EN"-E7EC% 4.4 U'ing a m!"$i#"2-r 3 ,ETC- '$a$2m2n$ 3i$5 a 12'&ri#$ r Suppose that you want to dynamically allocate the necessary storage for the arrays of column (alues that are to be retrie(ed from the employee table% You must do the following steps) "eclare an S*+"1 structure% "ynamically allocate the S*+"1 and the necessary arrays for the column (alues% Set the fields in the S*+"1 for the column (alues that are to be retrie(ed% -pen the cursor% Fetch the rows%
1fter allocating the S*+"1 and the necessary arrays for the column (alues& you must set the fields in the S*+"1% E3ample) 1fter the -.EN statement& the program fetches the ne3t row set by using the following statement) E7EC S*+ FETC NE7T R-6SET FR-5 C9 F-R $: R-6S 4SIN> "ESCRI.T-R )outs!lda; The 4SIN> clause of the FETC statement names the S*+"1 that describes the columns that are to be retrie(ed. 4.8 U'ing r 3-'2$ # 'i$i n21 UP(ATE '$a$2m2n$' 1fter your program e3ecutes a FETC statement to establish the current row set& you can use a positioned 4."1TE statement with either of the following clauses) 6 ERE C4RRENT -F cursor-name to update)
a single row if the cursor is on a single row all the rows of a row set if the cursor is on a row set
6 ERE C4RRENT -F cursor-name F-R R-6 n -F R-6SET to update only row n of the current row set
a single row if the cursor is on a single row all the rows of a row set if the cursor is on a row set
6 ERE C4RRENT -F cursor-name F-R R-6 n -F R-6SET to delete only row n of the current row set
E7EC S*+ "E+ETE FR-5 "SN889:%E5. 6 ERE C4RRENT -F C9 EN"-E7EC. 6hen the "E+ETE statement is e3ecuted& the cursor must be positioned on a row or row set of the result table% If the cursor is positioned on a row& that row is deleted& and the cursor is positioned before the ne3t row of its result table% If the cursor is positioned on a row set& all of the rows in the row set are deleted& and the cursor is positioned before the ne3t row set of its result table.