Beruflich Dokumente
Kultur Dokumente
| When you declare a cursor, you tell DB2 whether you want the cursor to be
| scrollable or non-scrollable by including or omitting the SCROLL clause.
| This clause determines whether the cursor moves sequentially forward
| through the result table or can move randomly through the result table.
The simplest type of cursor is a non-scrollable cursor. A non-scrollable
| cursor always moves sequentially forward in the result table. When you
| open the cursor, the cursor is positioned before the first row in the
| result table. When you execute the first FETCH, the cursor is positioned
| on the first row. When you execute subsequent FETCH statements, the
| cursor moves one row ahead for each FETCH. After each FETCH statement, the
| cursor is positioned on the row that you fetched. After you execute a
| positioned UPDATE or positioned DELETE operation, the cursor stays at the
| current row of the result table. You cannot retrieve rows backward or move
| to a specific position in a result table with a non-scrollable cursor.
| The size, the order of the rows, and the values for each row of the
| result table do not change after you open the cursor.
|
|
|
|
| When you execute positioned UPDATE and DELETE statements with the
| cursor, those updates are visible in the result table.
| When a row of the result table is deleted from the underlying table,
| the row is no longer visible in the result table.
| In DB2 Version 7, when you declare a cursor as SENSITIVE, you must also
| declare it as STATIC. Declaring the cursor as STATIC has the following
| effects:
|
|
|
|
|
|
|
|
|
|
|
| The size of the result table does not grow after you open the cursor.
| Rows that are inserted into the underlying table are not added to the
| result table.
| The order of the rows does not change after you open the cursor.
| If the cursor declaration contains an ORDER BY clause, and columns
| that are in the ORDER BY clause are updated after you open the cursor,
| the order of rows in the result table does not change.
cursor
Cursor position when the FETCH is
executed
Before the first row
At the first row
At the last row
After the last row
To an absolute row number, from
before the first row forward or from
after the last row backward
Forward or backward a relative number
|
|
|
|
CURRENT
PRIOR or RELATIVE -1
NEXT or RELATIVE +1
|
|
|
|
of
At
To
To
rows
the current row
the previous row
the next row (default)
The following examples show the SQL statements that you must include in a COBOL program to
define and use a cursor.
Figure 16 shows how to use a non-scrollable cursor to perform FETCH and positioned UPDATE
operations.
**************************************************
* Declare a cursor that will be used to update
*
* the JOB and WORKDEPT columns of the EMP table. *
**************************************************
EXEC SQL
DECLARE THISEMP CURSOR FOR
SELECT EMPNO, LASTNAME,
WORKDEPT, JOB
FROM DSN8710.EMP
WHERE WORKDEPT = 'D11'
FOR UPDATE OF JOB
END-EXEC.
**************************************************
* Open the cursor
*
**************************************************
EXEC SQL
OPEN THISEMP
END-EXEC.
**************************************************
* Indicate what action to take when all rows
*
* in the result table have been fetched.
*
**************************************************
EXEC SQL
WHENEVER NOT FOUND
GO TO CLOSE-THISEMP
END-EXEC.
**************************************************
* Fetch a row to position the cursor.
*
**************************************************
EXEC SQL
FETCH THISEMP
INTO :EMP-NUM, :NAME2,
:DEPT, :JOB-NAME
END-EXEC.
**************************************************
* Update the row that the cursor points to.
*
**************************************************
EXEC SQL
UPDATE DSN8710.EMP
SET JOB = :NEW-JOB
WHERE CURRENT OF THISEMP
END-EXEC.
**************************************************
* Branch back to fetch and process the next row. *
**************************************************
.
.
.
**************************************************
* Close the cursor
*
**************************************************
CLOSE-THISEMP.
EXEC SQL
CLOSE THISEMP
END-EXEC.
Figure 17 shows how to use a scrollable cursor to retrieve data backward from a table.
**************************************************
* Declare a cursor that will be used to retrieve *
* the data backward from the EMP table. The
*
* cursor should have access to changes by other *
* processes.
*
**************************************************
EXEC SQL
DECLARE THISEMP SENSITIVE STATIC SCROLL CURSOR FOR
SELECT EMPNO, LASTNAME, WORKDEPT, JOB
FROM DSN8710.EMP
END-EXEC.
**************************************************
* Open the cursor
*
**************************************************
EXEC SQL
OPEN THISEMP
END-EXEC.
**************************************************
* Indicate what action to take when all rows
*
* in the result table have been fetched.
*
**************************************************
EXEC SQL
WHENEVER NOT FOUND GO TO CLOSE-THISEMP
END-EXEC.
**************************************************
* Position the cursor after the last row of the *
* result table. This FETCH statement cannot
*
* include the SENSITIVE or INSENSITIVE keyword
*
* and cannot contain an INTO clause.
*
**************************************************
EXEC SQL
FETCH AFTER THISEMP
END-EXEC.
**************************************************
* Fetch the previous row in the table.
*
**************************************************
EXEC SQL
FETCH SENSITIVE PRIOR THISEMP
INTO :EMP-NUM, :NAME2, :DEPT, :JOB-NAME
END-EXEC.
**************************************************
* Check that the fetched row is not a hole
*
* (SQLCODE +222). If the row is not a
*
* hole, print the row contents.
*
**************************************************
IF SQLCODE IS GREATER THAN OR EQUAL TO 0 AND
SQLCODE IS NOT EQUAL TO +100 AND
SQLCODE IS NOT EQUAL TO +222 THEN
PERFORM PRINT-RESULTS.
**************************************************
* Branch back to fetch the previous row.
*
**************************************************
.
.
.
**************************************************
* Close the cursor
*
**************************************************
CLOSE-THISEMP.
EXEC SQL
CLOSE THISEMP
END-EXEC.
F
H
L
T
Z
Floating point
Hexadecimal
Date
Time
Timestamp
http://www.sybase.com/detail?id=20375
http://publib.boulder.ibm.com/infocenter/iseries/v5r3/index.jsp?topic=%2Frzajp%2Frzajpcobile.htm
PROCEDURE DIVISION.
120
121
A000-MAIN.
122
MOVE 1.04 TO PERCENTAGE.
123
OPEN OUTPUT PRINTFILE.
124
125
***************************************************************
126
* Update the selected employees by the new percentage. If an *
127
* error occurs during the update, ROLLBACK the changes,
*
128
***************************************************************
129
130
3 EXEC SQL
131
WHENEVER SQLERROR GO TO E010-UPDATE-ERROR
132
END-EXEC.
133
4 EXEC SQL
134
UPDATE CORPDATA/EMPLOYEE
135
SET SALARY = SALARY * :PERCENTAGE
136
WHERE COMM >= :COMMISSION
137
END-EXEC.
138
139
***************************************************************
140
* Commit changes.
*
141
***************************************************************
142
143
5 EXEC SQL
144
COMMIT
145
END-EXEC.
146
147
EXEC SQL
148
WHENEVER SQLERROR GO TO E020-REPORT-ERROR
149
END-EXEC.
150
151
***************************************************************
152
* Report the updated statistics for each employee receiving *
153
* a raise and the projects that s/he participates in
*
154
***************************************************************
155
156
***************************************************************
157
* Write out the header for Report 1.
*
158
***************************************************************
159
160
write print-record from rpt1-header1
161
before advancing 2 lines.
162
write print-record from rpt1-header2
163
before advancing 1 line.
164
6 exec sql
165
declare c1 cursor for
166
167
168
169
170
171
172
173
174
175
176
177
178
180
181
182
183
Triggers
A trigger is a set of actions that runs automatically when a specified change operation is
performed on a specified table or view. The change operation can be an SQL INSERT, UPDATE,
or DELETE statement, or an insert, an update, or a delete high-level language statement in an
application program. Triggers are useful for tasks such as enforcing business rules, validating
input data, and keeping an audit trail.
Triggers can be defined as SQL or external.
For an external trigger, the CRTPFTRG CL command is used. The program containing the set of
trigger actions can be defined in any supported high level language. External triggers can be
insert, update, delete, or read triggers.
For an SQL trigger, the CREATE TRIGGER statement is used. The trigger program is defined
entirely using SQL. SQL triggers can be insert, update, or delete triggers.
Once a trigger is associated with a table or view, the trigger support calls the trigger program
whenever a change operation is initiated against the table or view, or any logical file or view
created over the table or view. SQL triggers and external triggers can be defined for the same
table. Only SQL triggers can be defined for a view. Up to 200 triggers can be defined for a single
table or view.
Each change operation for a table can call a trigger before or after the change operation occurs.
Additionally, you can add a read trigger that is called every time the table is accessed. Thus, a
table can be associated with many types of triggers.
Each change operation for a view can call an instead of trigger which will perform some set of
actions instead of the insert, update, or delete. A view can be associated with an:
SQL triggers
The SQL CREATE TRIGGER statement provides a way for the database management system
to actively control, monitor, and manage a group of tables and views whenever an insert, an
update, or a delete operation is performed.
The statements specified in the SQL trigger are executed each time an SQL insert, update, or
delete operation is performed. An SQL trigger may call stored procedures or user-defined
functions to perform additional processing when the trigger is executed.
Unlike stored procedures, an SQL trigger cannot be directly called from an application. Instead,
an SQL trigger is invoked by the database management system on the execution of a triggering
insert, update, or delete operation. The definition of the SQL trigger is stored in the database
management system and is invoked by the database management system when the SQL table or
view that the trigger is defined on, is modified.
An SQL trigger can be created by specifying the CREATE TRIGGER SQL statement. All
objects referred to in the CREATE TRIGGER statement (such as tables and functions) must
exist; otherwise, the trigger will not be created. The statements in the routine-body of the SQL
trigger are transformed by SQL into a program (*PGM) object. The program is created in the
schema specified by the trigger name qualifier. The specified trigger is registered in the
SYSTRIGGERS, SYSTRIGDEP, SYSTRIGCOL, and SYSTRIGUPD SQL catalogs.
External triggers
For an external trigger, the program that contains the set of trigger actions can be defined in any
supported high-level language that creates a *PGM object.
The trigger program can have SQL embedded in it. To define an external trigger, you must create
a trigger program and add it to a table using the ADDPFTRG CL command or you can add it
using iSeries Navigator. To add a trigger to a table, you must:
Identify the program that performs the actions that you want.
http://publib.boulder.ibm.com/html/as400/v4r5/ic2979/info/db2/rbafomstrzahftri.htm
In this section, we focus on the OCCURS clause to store table data. As we will see,
tables and arrays are stored in exactly the same way; they are, however, used for
different purposes.
A table is a list of stored fields that are looked up or referenced by the pro-gram.
Tables are used in conjunction with table look-ups, whereas a table look-up is a
procedure that finds a specific entry in the table. Thus, an array stores data or totals
to be manipulated or outputted, whereas a table is used for looking up, or
referencing, data.
Array can be declared in cobol using the OCCURS clause.
For one dimensional array,
01 Arrays.
05 Var1 PIC X(10) Occurs 10 times.
For two dimensional array,
01 Arrays.
03 AAA Occurs 10 times
05 BBB Occurs 10 times
07 Value PIC 9(3).
01 ARRAYNAME
05 ARRAY-RECDFORMAT OCCURS 20 TIMES
10 ARRAYNAME-FIELDNAME1 PIC X(20)
10 ARRAYNAME-FIELDNAME2 PIC X(30)
AND WE CAN ACCESS THIS ARRAY BY USING SUBSCRIPT...
88
Condition names
2. What is 77 level used for?
Elementary level item. Cannot be subdivisions of other items (cannot be qualified), nor can they
be subdivided themselves.
3. What is 88 level used for?
88 level is used for condition names.
The level 88 in the Data Division can be used to give condition names to the values that a field
contains. A condition name entry specifies either a single value or a set of values for the
condityional variable. When this level is specified you can use the condition name instead of is
=equal to in the IF statement. Condition name should be specified under Level 88 immediately
following the field description.
Example:
01 WS-MARITAL-STATUS PIC X.
88 SINGLE VALUE Y.
88 MARRIED
VALUE N.
01 WS-REPLY
PIC X.
88 REPLY-YES
VALUE Y.
88 REPLY-N0 VALUE N.
01 WS-MARKS
PIC 999.
88 PASSED
VALUE 40 THRU 100.
88 FAILED
VALUE 0 THRU 39.
4. What are level 66 used for?
For RENAMES clause.
5. What is the difference between level 77 and 01? And which is more efficient?
Level 77 can be used to describe independent elementary items in the Working Storage or
Linkage Sections. Level 77 cannot use Redefines.
Level 01 can be used to describe both elementary and group items. Any item described in Level
01, the system is putting on Double-Word boundary and inserts slack bytes if necessary
6. How many different level numbers can be used in COBOL to describe a record?
01-49.
7. Give some advantages of REDEFINES clause.
1.You can REDEFINE a Variable from one PI1CTURE class to another PICTURE class by using
the same memory location. Multiple REDEFINES of same area possible
2. By REDEFINES we can INITIALISE the variable in WORKING-STORAGE Section itself.
3. We can REDEFINE a Single Variable into so many sub-variables.
Syntax:
<Level> <DataName1> REDEFINES <DataName2>
Example:
01 Sales-Record.
02 Sales-Type
Pic X.
02 Sales-By-Unit.
03 Qty
Pic 9(4).
03 Unit-Price Pic 9(8)V99.
02 Total-Sales REDEFINES Sales-By-Unit.
03 Amount
Pic 9(10)V99.
03 Filler
Pic X(2).
8. Can I redefine an X (100) field with a field of X (200)?
Yes.
Redefines just cause both fields to start at the same location. For example:
01 WS-TOP PIC X (1)
01 WS-TOP-RED REDEFINES WS-TOP PIC X (2).
If you MOVE 12 to WS-TOP-RED,
DISPLAY WS-TOP will show 1 while
DISPLAY WS-TOP-RED will show 12.
9. Can I redefine an X (200) field with a field of X (100)?
Yes.
Subfile in cobol400
http://www-01.ibm.com/support/docview.wss?uid=nas8N1018134
http://www-01.ibm.com/support/docview.wss?uid=nas8N1010284
http://www-01.ibm.com/support/docview.wss?uid=nas8N1010265
Page at a Time