0% found this document useful (0 votes)
40 views20 pages

Program Structure II

Uploaded by

Unknown
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views20 pages

Program Structure II

Uploaded by

Unknown
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Welcome to:

Program Structure II

© Copyright IBM Corporation 2005


3.1.1
Course materials may not be reproduced in whole or in part without the prior written permission of IBM.
Unit Objectives
After completing this unit, you should be able to:
Use DECLARE, OPEN, FETCH, and CLOSE CURSOR statements
to handle select criteria that may return multiple rows in application
programs
Issue positioned UPDATE and DELETE statements

© Copyright IBM Corporation 2005


Processing Multiple Rows
SELECT

EMPNO LASTNAME
000030 KWAN
000290 PARKER
000300 SMITH
empno name
EMPNO LASTNAME FETCH
000030 KWAN
000290 PARKER
000300 SMITH 000030 KWAN
empno name
EMPNO LASTNAME
FETCH
000030 KWAN
000290 PARKER
000300 SMITH 000290 PARKER
empno name
© Copyright IBM Corporation 2005
Structured
Programming - Sequential File Read

1. ?
2. ?
file
3. ?
while (not EOF)
{
< process file >
start
4. ?
} process
5. ? end

© Copyright IBM Corporation 2005


SELECT with FETCH - C Program
DEFINE A CURSOR
EXEC SQL

DEFINITION
DECLARE K9 CURSOR FOR
SELECT EMPNO, LASTNAME
FROM TEMPL
WHERE DEPTNO = :dpt;

OPEN the CURSOR


EXEC SQL OPEN K9;

FETCH RESULT ROWS ONE AT A TIME

EXECUTION
EXEC SQL
FETCH K9 INTO :empno, :name;

CLOSE CURSOR when finished


EXEC SQL CLOSE K9;
© Copyright IBM Corporation 2005
Answer Set Materialization
BUFFER OPEN NOMAT

JONES 10000 FETCH NOMAT #1 DECLARE NOMAT


CURSOR FOR
ADAMS 15000 FETCH NOMAT #2 SELECT NAME, SALARY
SMITH 12000 FROM . . .
BAKER 16000
GATES 20000 FETCH NOMAT #5

OPEN MAT

ADAMS 15000 FETCH MAT #1 DECLARE MAT


CURSOR FOR
BAKER 16000 FETCH MAT #2 SELECT NAME, SALARY
GATES 20000 FROM . . .
JONES 10000 ORDER BY NAME

SMITH 12000 FETCH MAT #3


WORKFILE
© Copyright IBM Corporation 2005
DELETE Via a Cursor - C Program
EXEC SQL DECLARE CE CURSOR FOR
SELECT . . . FROM TEMPL . . .;

:
EXEC SQL OPEN CE ;

EXEC SQL FETCH CE INTO . . . ;

EXEC SQL DELETE FROM TEMPL


WHERE CURRENT OF CE ;

EXEC SQL CLOSE CE ;


© Copyright IBM Corporation 2005
UPDATE Via a Cursor - C Program
EXEC SQL
DECLARE CX CURSOR FOR
SELECT EMPNO, LASTNAME
FROM TEMPL
WHERE DEPTNO = :dpt
FOR UPDATE OF LASTNAME;
:
:
EXEC SQL OPEN CX;

EXEC SQL
FETCH CX INTO :empno, :name;

EXEC SQL UPDATE TEMPL


SET LASTNAME=:newname
WHERE CURRENT OF CX;
:
:
EXEC SQL CLOSE CX;
© Copyright IBM Corporation 2005
UPDATE/DELETE Via a Cursor

-RESTRICTIONS-

DECLARE CURSOR statement may not contain


"FOR UPDATE OF", nor may the cursor be used for
"DELETE WHERE CURRENT OF",
if the SELECT statement contains:
ORDER BY
GROUP BY
DISTINCT
Set operators (UNION)
Function
Join
FOR FETCH ONLY/FOR READ ONLY
© Copyright IBM Corporation 2005
OPTIMIZE FOR n ROWS

DECLARE cursor-name CURSOR FOR


select-statement
OPTIMIZE FOR 20 ROWS

I can only hold 20


on one screen

Show me all
people named
"Brown"

DB2
There are 500
people with that
name

© Copyright IBM Corporation 2005


FETCH FIRST k ROWS ONLY

DECLARE cursor-name CURSOR FOR


select-statement
FETCH FIRST 100 ROWS ONLY

Show me all
people named
"Brown", but I am
not going to look
at more than 100
DB2
There are 500
people with that
name

© Copyright IBM Corporation 2005


Cursor Manipulation
DECLARE C72 CURSOR FOR
SELECT EMPNO, LASTNAME FROM TEMPL
WHERE DEPTNO = :DPT

POSITIONING
OPEN
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .

FETCH FETCH FETCH


. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .

CLOSE

To REUSE a CURSOR
OPEN
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .
. . . . . . . . . . . . .

COMMIT MAY CLOSE OPEN CURSORS


CLOSE DOES NOT COMMIT
© Copyright IBM Corporation 2005
WITH HOLD Cursor

DECLARE E1 CURSOR WITH HOLD FOR


SELECT EMPNO, LASTNAME, PHONE, SALARY
FROM TEMPL WHERE WORKDEPT < :dpt
ORDER BY EMPNO

Cursor held until:


Close cursor statement
ROLLBACK
CONNECT (if connect type 1)

When commit is issued, open cursors


declared WITH HOLD will remain open
and associated table locks will be held.
Row locks are released.
© Copyright IBM Corporation 2005
Cursor Repositioning
REQUIREMENT:
UPDATE of a large number of rows, using a CURSOR.
To avoid accumulating an excessive number of ROW
LOCKS, issue a COMMIT after every 500 UPDATEs.
Consider:
If restart capabilities must be included in the program,
position of the cursor must be reestablished.
SOLUTION:
After ABEND, reestablish position based upon a saved
column value.

© Copyright IBM Corporation 2005


Cursor Repositioning - C (1 of 2)

EXEC SQL
DECLARE C1 CURSOR Unique INDEX on
X required.
WITH HOLD FOR
SELECT X,Y,Z FROM T1 Cannot use
WHERE X> :storx FOR UPDATE OF
ORDER BY X;

EXEC SQL Obtain restart or


SELECT VALUE INTO :storx initial value
FROM RESTART;

CTR=0;

EXEC SQL Position of C1


based on storx
OPEN C1;

EXEC SQL
FETCH C1
INTO :storx,:story,:storz;

© Copyright IBM Corporation 2005


Cursor Repositioning - C (2 of 2)
while (SQLCODE == 0)
{ if (some_condition)
{ EXEC SQL UPDATE T1
SET Y=:newy, Z=:newz /* UPDATE Current Row */
WHERE X=:storx;
ctr = ctr+1;
if (ctr == 500)
{ EXEC SQL UPDATE RESTART
SET VALUE=:storx; /* Save last value */
EXEC SQL COMMIT;
ctr = 0;
}
}
EXEC SQL FETCH C1 INTO :storx,:story,:storz;
}
EXEC SQL
UPDATE RESTART SET VALUE = :MinValue;
EXEC SQL COMMIT;

© Copyright IBM Corporation 2005


Compound SQL
Several SQL substatements in a single executable block
Reduce DBM overhead
Reduce network traffic
No response return before
All indicated substatements have completed successfully
One sub-statement ends in error (unless NOT ATOMIC is used)

© Copyright IBM Corporation 2005


Compound SQL - C Program
EXEC SQL BEGIN COMPOUND ATOMIC STATIC

UPDATE SAVINGS SET


BALANCE = BALANCE -:transfer
WHERE ATMCARD = :atmcard;

UPDATE CHECKING SET


BALANCE = BALANCE +:transfer
WHERE ATMCARD = :atmcard;

INSERT INTO ATMTRANS


(TTSTMP,CODE,AMOUNT) VALUES
(CURRENT TIMESTAMP,:code,:transfer);

COMMIT;

END COMPOUND;
© Copyright IBM Corporation 2005
Compound SQL Restrictions
Only NOT ATOMIC supported with DB2 Connect
Can only include static SQL statements
Must be Embedded Static SQL

© Copyright IBM Corporation 2005


Unit Summary
Having completed this unit, you should be able to:
Use DECLARE, OPEN, FETCH, and CLOSE CURSOR statements
to handle select criteria that may return multiple rows in application
programs
Issue positioned UPDATE and DELETE statements

© Copyright IBM Corporation 2005

You might also like