DB2 Multi Fetch and Insert

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 7

()* MULTIPLE-RO+ INSERT AN( ,ETC.*/.//*.

0*

<INSURANCE>/ <AXA-US> <KIRANMAYI GOGULAPATI> kiranmayi.g g!"a#a$i%$&'.& m

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

*. In'2r$ing m!"$i#"2 r 3'


You can use a form of the INSERT statement to insert multiple rows from (alues that are pro(ided in host (ariable arrays% Each array contains (alues for a column of the target table% The first (alue in an array corresponds to the (alue for that column for the first inserted row& the second (alue in the array corresponds to the (alue for the column in the second inserted row& and so on% "#$ determines the attributes of the (alues based on the declaration of the array% E3ample) You can insert the number of rows that are specified in the host (ariable N45-R-6S by using the following INSERT statement) E7EC S*+ INSERT INT- "SN889:%1CT 01CTN-& 1CT;6"& 1CT"ESC2 <1+4ES 0) <19& ) <1$& ) <1= )I<1=2 F-R )N45-R-6S R-6S EN"-E7EC. 1ssume that the host (ariable arrays <19& <1$& and <1= ha(e been declared and populated with the (alues that are to be inserted into the 1CTN-& 1CT;6"& and 1CT"ESC columns% The N45-R-6S host (ariable specifies the number of rows that are to be inserted& which must be less than or e!ual to the dimension of each host (ariable array% 1ssume also that the indicator (ariable array I<1= has been declared and populated to indicate whether null (alues are inserted into the 1CT"ESC column% 4se indicator (ariable arrays with host (ariable arrays in the same way that you use indicator (ariables with host (ariables% 1n indicator (ariable array must ha(e at least as many entries as its host (ariable array% You can use the multiple-row INSERT statement both statically and dynamically% If you prepare and e3ecute the INSERT statement& you can code the E7EC4TE statement to use either host (ariable arrays or an S*+ descriptor 0S*+"12% If you use host (ariable arrays& each host (ariable array in the 4SIN> clause of the

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.,2$&5ing m!"$i#"2 r 3'


You can retrie(e multiple rows of data by using a row-set positioned cursor% 1 row-set positioned cursor retrie(es ?ero& one& or more rows at a time& as a row set& from the result table of the cursor into host (ariable arrays% You can reference all of the rows in the row set& or only one row in the row set& when you use a positioned "E+ETE or positioned 4."1TE statement after a FETC statement that retrie(es row sets% 1 multiple-row FETC statement can be used to copy a row set of column (alues into either of the following data areas) ost (ariable arrays that are declared in your program "ynamically allocated arrays whose storage addresses are put into an S*+ descriptor area 0S*+"12& along with the attributes of the columns to be retrie(ed

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

8.U#1a$ing a"" r 3' 9 $52 &!rr2n$ r 3 '2$


The following positioned 4."1TE statement uses the 6 ERE C4RRENT -F clause: E7EC S*+ 4."1TE "SN889:%E5. SET S1+1RY @ A:::: 6 ERE C4RRENT -F C9 EN"-E7EC% 6hen the 4."1TE 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 updated% If the cursor is positioned on a row set& all of the rows in the row set are updated.

:.U#1a$ing a '#2&i9i& r 3 9 $52 &!rr2n$ r 3 '2$


The following positioned 4."1TE statement uses the 6 ERE C4RRENT -F cursor F-R R-6 n -F R-6SET clause) E7EC S*+ 4."1TE "SN889:%E5. SET S1+1RY @ A:::: 6 ERE C4RRENT -F C9 F-R R-6 A -F R-6SET EN"-E7EC% 6hen the 4."1TE statement is e3ecuted& the cursor must be positioned on a row set of the result table% The specified row 0in the e3ample& row A2 of the current row set is updated% :.0 U'ing r 3-'2$ # 'i$i n21 (ELETE '$a$2m2n$' 1fter your program e3ecutes a FETC statement to establish the current row set& you can use a positioned "E+ETE statement with either of the following clauses) 6 ERE C4RRENT -F cursor-name to delete)

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

;. (2"2$ing a"" r 3' 9 $52 &!rr2n$ r 3 '2$


clause) The following positioned "E+ETE statement uses the 6 ERE C4RRENT -F

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.

/.(2"2$ing a 'ing"2 r 3 9 $52 &!rr2n$ r 3 '2$


The following positioned "E+ETE statement uses the 6 ERE C4RRENT -F cursor F-R R-6 n -F R-6SET clause: E7EC S*+ "E+ETE FR-5 "SN889:%E5. 6 ERE C4RRENT -F C9 F-R R-6 A -F R-6SET EN"-E7EC. 6hen the "E+ETE statement is e3ecuted& the cursor must be positioned on a row set of the result table% The specified row of the current row set is deleted& and the cursor remains positioned on that row set% The deleted row 0in the e3ample& row A of the row set2 cannot be retrie(ed or updated%

You might also like