forked from oleksiivorobiov/oracle_oci_examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cbdem1.cob
481 lines (397 loc) · 17.1 KB
/
cbdem1.cob
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
*
* $Header: cbdem1.cob 14-jul-99.14:30:27 mjaeger Exp $
*
* Copyright (c) 1991, 1999, Oracle Corporation. All rights reserved.
*
* NAME
* cbdem1.cob - Cobol demo program # 1
* MODIFIED (MM/DD/YY)
* mjaeger 07/14/99 - bug 808870: OCCS: convert tabs, no long lines
* plocke 11/14/95 - to update for v7.3
* dchatter 07/20/95 - merge changes from branch 1.1.720.1
* dchatter 04/14/95 - fix test to work on sun
* sjain 03/16/92 - Creation
* ---------------------------------------------------------
* CBDEM1 IS A SIMPLE EXAMPLE PROGRAM WHICH ADDS
* NEW EMPLOYEE ROWS TO THE PERSONNEL DATA BASE. CHECKING
* IS DONE TO INSURE THE INTEGRITY OF THE DATA BASE.
* THE EMPLOYEE NUMBERS ARE AUTOMATICALLY SELECTED USING
* THE CURRENT MAXIMUM EMPLOYEE NUMBER AS THE START.
* IF ANY EMPLOYEE NUMBER IS A DUPLICATE, IT IS SKIPPED.
* THE PROGRAM QUERIES THE USER FOR DATA AS FOLLOWS:
*
* Enter employee name :
* Enter employee job :
* Enter employee salary:
* Enter employee dept :
*
* TO EXIT THE PROGRAM, ENTER A CARRIAGE RETURN AT THE
* PROMPT FOR EMPLOYEE NAME.
*
* IF THE ROW IS SUCCESSFULLY INSERTED, THE FOLLOWING
* IS PRINTED:
*
* ENAME added to DNAME department as employee # NNNNN
*
* THE MAXIMUM LENGTHS OF THE 'ENAME', 'JOB', AND 'DNAME'
* COLUMNS WILL BE DETERMINED BY THE ODESCR CALL.
*
*----------------------------------------------------------
IDENTIFICATION DIVISION.
PROGRAM-ID. CBDEM1.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 LDA.
02 LDA-V2RC PIC S9(4) COMP.
02 FILLER PIC X(10).
02 LDA-RC PIC S9(4) COMP.
02 FILLER PIC X(50).
01 HDA PIC X(512).
01 CURSOR-1.
02 C-V2RC PIC S9(4) COMP.
02 C-TYPE PIC S9(4) COMP.
02 C-ROWS PIC S9(9) COMP.
02 C-OFFS PIC S9(4) COMP.
02 C-FNC PIC S9(4) COMP.
02 C-RC PIC S9(4) COMP.
02 FILLER PIC X(50).
01 CURSOR-2.
02 C-V2RC PIC S9(4) COMP.
02 C-TYPE PIC S9(4) COMP.
02 C-ROWS PIC S9(9) COMP.
02 C-OFFS PIC S9(4) COMP.
02 C-FNC PIC S9(4) COMP.
02 C-RC PIC S9(4) COMP.
02 FILLER PIC X(50).
77 USER-ID PIC X(5) VALUE "SCOTT".
77 USER-ID-L PIC S9(9) VALUE 5 COMP.
77 PSW PIC X(5) VALUE "tiger".
77 PSW-L PIC S9(9) VALUE 5 COMP.
77 CONN PIC S9(9) VALUE 0 COMP.
77 CONN-L PIC S9(9) VALUE 0 COMP.
77 CONN-MODE PIC S9(9) VALUE 0 COMP.
77 SQL-SEL PIC X(38) VALUE
"SELECT DNAME FROM DEPT WHERE DEPTNO=:1".
77 SQL-SEL-L PIC S9(9) VALUE 38 COMP.
77 SQL-INS PIC X(150) VALUE
"INSERT INTO EMP (EMPNO,ENAME,JOB,SAL,DEPTNO)
- " VALUES (:EMPNO,:ENAME,:JOB,:SAL,:DEPTNO)".
77 SQL-INS-L PIC S9(9) VALUE 150 COMP.
77 SQL-SELMAX PIC X(33) VALUE
"SELECT NVL(MAX(EMPNO),0) FROM EMP".
77 SQL-SELMAX-L PIC S9(9) VALUE 33 COMP.
77 SQL-SELEMP PIC X(26) VALUE
"SELECT ENAME,JOB FROM EMP".
77 SQL-SELEMP-L PIC S9(9) VALUE 26 COMP.
77 EMPNO PIC S9(9) COMP.
77 EMPNO-D PIC ZZZZ9.
77 ENAME PIC X(12).
77 JOB PIC X(12).
77 SAL PIC X(10).
77 DEPTNO PIC X(10).
77 FMT PIC X(6).
77 CBUF PIC X(10).
77 DNAME PIC X(15).
77 ENAME-L PIC S9(9) VALUE 12 COMP.
77 ENAME-SIZE PIC S9(4) COMP.
77 JOB-L PIC S9(9) VALUE 12 COMP.
77 JOB-SIZE PIC S9(4) COMP.
77 SAL-L PIC S9(9) VALUE 10 COMP.
77 DEPTNO-L PIC S9(9) VALUE 10 COMP.
77 DNAME-L PIC S9(9) VALUE 15 COMP.
77 DNAME-SIZE PIC S9(4) COMP.
77 EMPNO-N PIC X(6) VALUE ":EMPNO".
77 ENAME-N PIC X(6) VALUE ":ENAME".
77 JOB-N PIC X(4) VALUE ":JOB".
77 SAL-N PIC X(4) VALUE ":SAL".
77 DEPTNO-N PIC X(7) VALUE ":DEPTNO".
77 EMPNO-N-L PIC S9(9) VALUE 6 COMP.
77 ENAME-N-L PIC S9(9) VALUE 6 COMP.
77 JOB-N-L PIC S9(9) VALUE 4 COMP.
77 SAL-N-L PIC S9(9) VALUE 4 COMP.
77 DEPTNO-N-L PIC S9(9) VALUE 7 COMP.
77 INTEGER PIC S9(9) COMP VALUE 3.
77 ASC PIC S9(9) COMP VALUE 1.
77 ZERO-A PIC S9(9) COMP VALUE 0.
77 ZERO-B PIC S9(4) COMP VALUE 0.
77 ONE PIC S9(9) COMP VALUE 1.
77 TWO PIC S9(9) COMP VALUE 2.
77 FOUR PIC S9(9) COMP VALUE 4.
77 SIX PIC S9(9) COMP VALUE 6.
77 EIGHT PIC S9(9) COMP VALUE 8.
77 ERR-RC PIC S9(4) COMP.
77 ERR-FNC PIC S9(4) COMP.
77 ERR-RC-D PIC ZZZ9.
77 ERR-FNC-D PIC ZZ9.
77 MSGBUF PIC X(160).
77 MSGBUF-L PIC S9(9) COMP VALUE 160.
77 ASK-EMP PIC X(25) VALUE
"Enter employee name: ".
77 ASK-JOB PIC X(25) VALUE
"Enter employee job: ".
77 ASK-SAL PIC X(25) VALUE
"Enter employee salary: ".
77 ASK-DEPTNO PIC X(25) VALUE
"Enter employee dept: ".
PROCEDURE DIVISION.
BEGIN.
*----------------------------------------------------------
* CONNECT TO ORACLE IN NON-BLOCKING MODE.
* HDA MUST BE INITIALIZED TO ALL ZEROS BEFORE CALL TO OLOG.
*----------------------------------------------------------
MOVE LOW-VALUES TO HDA.
CALL "OLOG" USING LDA, HDA, USER-ID, USER-ID-L,
PSW, PSW-L, CONN, CONN-L, CONN-MODE.
IF LDA-RC NOT = 0
PERFORM ORA-ERROR
GO TO EXIT-STOP.
DISPLAY "Connected to ORACLE as user ", USER-ID.
*----------------------------------------------------------
* OPEN THE CURSORS.
*----------------------------------------------------------
CALL "OOPEN" USING CURSOR-1, LDA.
IF C-RC IN CURSOR-1 NOT = 0
PERFORM ORA-ERROR
GO TO EXIT-LOGOF.
CALL "OOPEN" USING CURSOR-2, LDA.
IF C-RC IN CURSOR-2 NOT = 0
PERFORM ORA-ERROR
GO TO EXIT-LOGOF.
*----------------------------------------------------------
* DISABLE AUTO-COMMIT.
* NOTE: THE DEFAULT IS OFF, SO THIS COULD BE OMITTED.
*----------------------------------------------------------
CALL "OCOF" USING LDA.
IF LDA-RC NOT = 0
PERFORM ORA-ERROR
GO TO EXIT-CLOSE.
*----------------------------------------------------------
* RETRIEVE THE CURRENT MAXIMUM EMPLOYEE NUMBER.
*----------------------------------------------------------
CALL "OPARSE" USING CURSOR-1, SQL-SELMAX, SQL-SELMAX-L,
ZERO-A, TWO.
IF C-RC IN CURSOR-1 NOT = 0
PERFORM ORA-ERROR
GO TO EXIT-CLOSE.
CALL "ODEFIN" USING CURSOR-1, ONE, EMPNO, FOUR,
INTEGER, ZERO-A, ZERO-B, FMT, ZERO-A, ZERO-A,
ZERO-B, ZERO-B.
IF C-RC IN CURSOR-1 NOT = 0
PERFORM ORA-ERROR
GO TO EXIT-CLOSE.
CALL "OEXEC" USING CURSOR-1.
IF C-RC IN CURSOR-1 NOT = 0
PERFORM ORA-ERROR
GO TO EXIT-CLOSE.
CALL "OFETCH" USING CURSOR-1.
IF C-RC IN CURSOR-1 NOT = 0
IF C-RC IN CURSOR-1 NOT = 1403
PERFORM ORA-ERROR
GO TO EXIT-CLOSE
ELSE
MOVE 10 TO EMPNO.
*----------------------------------------------------------
* DETERMINE THE MAX LENGTH OF THE EMPLOYEE NAME AND
* JOB TITLE. PARSE THE SQL STATEMENT -
* IT WILL NOT BE EXECUTED.
* DESCRIBE THE TWO FIELDS SPECIFIED IN THE SQL STATEMENT.
*----------------------------------------------------------
CALL "OPARSE" USING CURSOR-1, SQL-SELEMP, SQL-SELEMP-L,
ZERO-A, TWO.
IF C-RC IN CURSOR-1 NOT = 0
PERFORM ORA-ERROR
GO TO EXIT-CLOSE.
CALL "ODESCR" USING CURSOR-1, ONE, ENAME-SIZE, ZERO-B,
CBUF, ZERO-A, ZERO-A, ZERO-B, ZERO-B, ZERO-B.
IF C-RC IN CURSOR-1 NOT = 0
PERFORM ORA-ERROR
GO TO EXIT-CLOSE.
CALL "ODESCR" USING CURSOR-1, TWO, JOB-SIZE, ZERO-B,
CBUF, ZERO-A, ZERO-A, ZERO-B, ZERO-B, ZERO-B.
IF C-RC IN CURSOR-1 NOT = 0
PERFORM ORA-ERROR
GO TO EXIT-CLOSE.
IF ENAME-SIZE > ENAME-L
DISPLAY "ENAME too large for buffer."
GO TO EXIT-CLOSE.
IF JOB-SIZE > JOB-L
DISPLAY "JOB too large for buffer."
GO TO EXIT-CLOSE.
*----------------------------------------------------------
* PARSE THE INSERT AND SELECT STATEMENTS.
*----------------------------------------------------------
CALL "OPARSE" USING CURSOR-1, SQL-INS, SQL-INS-L,
ZERO-A, TWO.
IF C-RC IN CURSOR-1 NOT = 0
PERFORM ORA-ERROR
GO TO EXIT-CLOSE.
CALL "OPARSE" USING CURSOR-2, SQL-SEL, SQL-SEL-L,
ZERO-A, TWO.
IF C-RC IN CURSOR-2 NOT = 0
PERFORM ORA-ERROR
GO TO EXIT-CLOSE.
*----------------------------------------------------------
* BIND ALL SQL SUBSTITUTION VARIABLES.
*----------------------------------------------------------
CALL "OBNDRV" USING CURSOR-1, EMPNO-N, EMPNO-N-L,
EMPNO, FOUR, INTEGER, ZERO-A.
IF C-RC IN CURSOR-1 NOT = 0
PERFORM ORA-ERROR
GO TO EXIT-CLOSE.
CALL "OBNDRV" USING CURSOR-1, ENAME-N, ENAME-N-L,
ENAME, ENAME-L, ASC.
IF C-RC IN CURSOR-1 NOT = 0
PERFORM ORA-ERROR
GO TO EXIT-CLOSE.
CALL "OBNDRV" USING CURSOR-1, JOB-N, JOB-N-L,
JOB, JOB-L, ASC.
IF C-RC IN CURSOR-1 NOT = 0
PERFORM ORA-ERROR
GO TO EXIT-CLOSE.
CALL "OBNDRV" USING CURSOR-1, SAL-N, SAL-N-L, SAL,
SAL-L, ASC.
IF C-RC IN CURSOR-1 NOT = 0
PERFORM ORA-ERROR
GO TO EXIT-CLOSE.
CALL "OBNDRV" USING CURSOR-1, DEPTNO-N, DEPTNO-N-L,
DEPTNO, DEPTNO-L, ASC.
IF C-RC IN CURSOR-1 NOT = 0
PERFORM ORA-ERROR
GO TO EXIT-CLOSE.
*----------------------------------------------------------
* BIND THE DEPTNO SUBSTITUTION VAR IN THE SELECT STATEMENT.
*----------------------------------------------------------
CALL "OBNDRN" USING CURSOR-2, ONE, DEPTNO,
DEPTNO-L, ASC.
IF C-RC IN CURSOR-2 NOT = 0
PERFORM ORA-ERROR
GO TO EXIT-CLOSE.
*----------------------------------------------------------
* DESCRIBE THE 'DNAME' COLUMN - ONLY THE LENGTH.
*----------------------------------------------------------
CALL "ODSC" USING CURSOR-2, ONE, DNAME-SIZE.
IF C-RC IN CURSOR-2 NOT = 0
PERFORM ORA-ERROR
GO TO EXIT-CLOSE.
IF DNAME-SIZE > DNAME-L
DISPLAY "DNAME is to large for buffer."
GO TO EXIT-CLOSE.
*----------------------------------------------------------
* DEFINE THE BUFFER TO RECEIVE 'DNAME'.
*----------------------------------------------------------
CALL "ODEFIN" USING CURSOR-2, ONE, DNAME,
DNAME-L, ASC.
IF C-RC IN CURSOR-2 NOT = 0
PERFORM ORA-ERROR
GO TO EXIT-CLOSE.
*----------------------------------------------------------
* ASK THE USER FOR EMPLOYEE NAME, JOB, SAL, AND DEPTNO.
*----------------------------------------------------------
NEXT-EMP.
DISPLAY ASK-EMP WITH NO ADVANCING.
ACCEPT ENAME.
IF ENAME = " "
GO TO EXIT-CLOSE.
DISPLAY ASK-JOB WITH NO ADVANCING.
ACCEPT JOB.
DISPLAY ASK-SAL WITH NO ADVANCING.
ACCEPT SAL.
ASK-DPT.
DISPLAY ASK-DEPTNO WITH NO ADVANCING.
ACCEPT DEPTNO.
*----------------------------------------------------------
* CHECK FOR A VALID DEPARTMENT NUMBER BY EXECUTING.
* THE SELECT STATEMENT.
*----------------------------------------------------------
CALL "OEXEC" USING CURSOR-2.
IF C-RC IN CURSOR-2 NOT = 0
PERFORM ORA-ERROR
GO TO EXIT-CLOSE.
*----------------------------------------------------------
* FETCH THE ROWS - DEPTNO IS A PRIMARY KEY SO A MAX.
* OF 1 ROW WILL BE FETCHED.
* IF THE CURSOR RETURN CODE IS 1403 THEN
* NO SUCH DEPARTMENT EXISTS.
*----------------------------------------------------------
MOVE SPACES TO DNAME.
CALL "OFETCH" USING CURSOR-2.
IF C-RC IN CURSOR-2 = 0 THEN GO TO ADD-ROW.
IF C-RC IN CURSOR-2 = 1403
DISPLAY "No such department."
GO TO ASK-DPT.
*----------------------------------------------------------
* INCREMENT EMPNO BY 10.
* EXECUTE THE INSERT STATEMENT.
*----------------------------------------------------------
ADD-ROW.
ADD 10 TO EMPNO.
IF EMPNO > 9999
MOVE EMPNO TO EMPNO-D
DISPLAY "Employee number " EMPNO-D " too large."
GO TO EXIT-CLOSE.
CALL "OEXEC" USING CURSOR-1.
IF C-RC IN CURSOR-1 = 0 THEN GO TO PRINT-RESULT.
*----------------------------------------------------------
* IF THE RETURN CODE IS 1 (DUPLICATE VALUE IN INDEX),
* THEN GENERATE THE NEXT POSSIBLE EMPLOYEE NUMBER.
*----------------------------------------------------------
IF C-RC IN CURSOR-1 = 1
ADD 10 TO EMPNO
GO TO ADD-ROW
ELSE
PERFORM ORA-ERROR
GO TO EXIT-CLOSE.
PRINT-RESULT.
MOVE EMPNO TO EMPNO-D.
DISPLAY ENAME " added to the " DNAME
" department as employee number " EMPNO-D.
*----------------------------------------------------------
* THE ROW HAS BEEN ADDED - COMMIT THIS TRANSACTION.
*----------------------------------------------------------
CALL "OCOM" USING LDA.
IF LDA-RC NOT = 0
PERFORM ORA-ERROR
GO TO EXIT-CLOSE.
GO TO NEXT-EMP.
*----------------------------------------------------------
* CLOSE CURSORS AND LOG OFF.
*----------------------------------------------------------
EXIT-CLOSE.
CALL "OCLOSE" USING CURSOR-1.
IF C-RC IN CURSOR-1 NOT = 0
PERFORM ORA-ERROR.
CALL "OCLOSE" USING CURSOR-2.
IF C-RC IN CURSOR-2 NOT = 0
PERFORM ORA-ERROR.
EXIT-LOGOF.
CALL "OLOGOF" USING LDA.
IF LDA-RC NOT = 0
PERFORM ORA-ERROR.
EXIT-STOP.
DISPLAY "End of the OCIDEMO1 program."
STOP RUN.
*----------------------------------------------------------
* DISPLAY ORACLE ERROR NOTICE.
*----------------------------------------------------------
ORA-ERROR.
IF LDA-RC NOT = 0
DISPLAY "OLOGON error"
MOVE LDA-RC TO ERR-RC
MOVE "0" TO ERR-FNC
ELSE IF C-RC IN CURSOR-1 NOT = 0
MOVE C-RC IN CURSOR-1 TO ERR-RC
MOVE C-FNC IN CURSOR-1 TO ERR-FNC
ELSE
MOVE C-RC IN CURSOR-2 TO ERR-RC
MOVE C-FNC IN CURSOR-2 TO ERR-FNC.
DISPLAY "ORACLE error" WITH NO ADVANCING.
IF ERR-FNC NOT = 0
MOVE ERR-FNC TO ERR-FNC-D
DISPLAY " processing OCI function"
ERR-FNC-D "."
ELSE
DISPLAY ".".
MOVE " " TO MSGBUF.
CALL "OERHMS" USING LDA, ERR-RC, MSGBUF, MSGBUF-L.
DISPLAY MSGBUF.