COBOL access Oracle database sample

前端之家收集整理的这篇文章主要介绍了COBOL access Oracle database sample前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。


This is a sample program how COBOL access Oracle database.


Table Definition

sql> desc tab;
 Name                                      Null?    Type
 ----------------------------------------- -------- --------------------
 A                                         NOT NULL NUMBER(6)
 B                                                  CHAR(10)
 C                                                  NUMBER(6)


COBOL Program

This program demos the Oracle INSERT and SELECT statement.


$ cat ACCESSORA.pco
       IDENTIFICATION DIVISION.
       PROGRAM-ID. ACCESSORA.
       ENVIRONMENT DIVISION.
       CONFIGURATION SECTION.

       DATA DIVISION.
       WORKING-STORAGE SECTION.

           EXEC sql BEGIN DECLARE SECTION END-EXEC.
       01 USERNAME PIC X(010).
       01 PASSWD PIC X(010).
       01 DBSTRING PIC X(020).

       01 sql-DB-STATE PIC X(5).

       01 H-O-A       PIC S9(6) COMP-3.
       01 H-O-B       PIC X(10).
       01 H-O-C       PIC S9(6) COMP-3.

       01 H-I-A       PIC S9(6) COMP-3.
           EXEC sql END DECLARE SECTION END-EXEC.

           EXEC sql INCLUDE "sqlCA.cpy" END-EXEC.

       PROCEDURE DIVISION.

       BEGIN-PGM.
           EXEC sql WHENEVER sqlERROR
            DO PERFORM sql-ERROR
           END-EXEC.

           PERFORM logoN.

           EXEC sql WHENEVER NOT FOUND
            GOTO NO-CUST
           END-EXEC.

*     *INSERT A RECORD
           MOVE 2 TO H-O-A.
           MOVE "BBBBBBBBB" TO H-O-B.
           MOVE 222222 TO H-O-C.
           EXEC sql
            INSERT INTO TAB(A,B,C)
            VALUES(2,:H-O-B,:H-O-C)
           END-EXEC.

*     *SELECT A RECORD
           MOVE 2 TO H-I-A.
           MOVE ZEROS TO H-O-A.
           MOVE ZEROS TO H-O-B.
           MOVE ZEROS TO H-O-C.
           MOVE sqlSTATE TO sql-DB-STATE.

           EXEC sql
            SELECT A,C
            INTO :H-O-A,:H-O-C
            FROM TAB 
            WHERE A = :H-I-A
           END-EXEC.

           PERFORM DISPLAY-INFO.
           PERFORM logoUT.

       NO-CUST.
           DISPLAY "NOT A VALID CUST CODE - TRY AGAIN".
           PERFORM logoUT.

       logoN.
           MOVE "scott" TO USERNAME.
           MOVE "tiger" TO PASSWD.
           MOVE "orcl" TO DBSTRING.
           EXEC sql
            CONNECT :USERNAME IDENTIFIED BY :PASSWD
            USING :DBSTRING
           END-EXEC.
           DISPLAY "sqlCODE: ",sqlCODE OF sqlCA.
           DISPLAY "CONNECTED TO DATABASE ",DBSTRING.

       DISPLAY-INFO.
           DISPLAY "sqlCODE: ",sqlCODE OF sqlCA.
           DISPLAY "RECORD:".
           DISPLAY "    A:"  H-O-A.
           DISPLAY "    B:"  H-O-B.   
           DISPLAY "    C:"  H-O-C.    

       logoUT.
           DISPLAY "HAVE A GOOD DAY.".
           EXEC sql COMMIT WORK RELEASE END-EXEC.
           STOP RUN.

       sql-ERROR.
           EXEC sql WHENEVER sqlERROR CONTINUE END-EXEC.
           DISPLAY "ORACLE ERROR DETECTED:".
           DISPLAY sqlERRMC.
           EXEC sql ROLLBACK WORK RELEASE END-EXEC.
           STOP RUN.


Compile


#Pre-processor
procob include=COPY iname=ACCESSORA.pco release_cursor=no hold_cursor=no \
       mode=oracle sqlcheck=Syntax common_parser=yes lname=ACCESSORA.lis \
       oname=ACCESSORA.cbl declare_section=no varchar=yes format=variable \
       ireclen=300 db2_array=yes picx=char unsafe_null=yes dbms=v8 \
       close_on_commit=no errors=yes end_of_fetch=100

#Compile
cob -ug ACCESSORA.cob -C "use(../common/opt.dir)" -C "list(ACCESSORA.lst)" \
    -C XREF -C SETTINGS


Run

$ rtsora ACCESSORA       
sqlCODE: +0000000000
CONNECTED TO DATABASE orcl                
sqlCODE: +0000000000
RECORD:
    A:+000002
    B:BBBBBBBBB 
    C:+222222
HAVE A GOOD DAY.
原文链接:https://www.f2er.com/oracle/212182.html

猜你在找的Oracle相关文章