Calling an API using COBOL: An Example
This example calls the OBTAIN PERSON LOAF Stored Procedure (BAUDD051).
If running on the mainframe where these stored procedures are already defined, there is no need to seek a connect. The DB2 stored procedure may be called directly as follows.
In Working Storage…
01 WS-DB2-PRSN-OBTL-PGM-ID PIC X(17) VALUE 'BASCHEMA.BAUDD051'. 01 WS-OUT-ONLY-IND PIC S9(4) COMP VALUE -1. * ASSIGN THE OUT ONLY INDICATOR IN THE * SQL CALL STATEMENT TO HOST VARIABLES * THAT ARE USED ONLY TO RETURN VALUES * FROM THE CALL. THIS TELLS DB2 NOT * TO SEND THIS DATA UP TO THE SERVER. * AND HELPS TO REDUCE NETWORK I/O TIME. * * (THE DEFAULT IS ALL IN, INOUT, AND * OUTPUT HOST VARIABLES ARE SENT TO * THE SERVER) 01 WS-PROC-PRSN-OBT-AREA. 05 LKP-IN-UFID PIC X(8). 05 LKP-IN-UPDT-SRC-ID PIC X(12). 05 LKP-IN-UPDT-UFID PIC X(8). 05 LKP-OUT-RETURN-CD PIC X(9). 05 LKP-OUT-DB2-RETURN-CD PIC X(9). 05 LKP-OUT-FILE-RETURN-CD PIC X(9). 05 LKP-OUT-ERR-PROGRAM PIC X(8). 05 LKP-OUT-ERR-PARA PIC X(30). 05 LKP-OUT-ERR-TABLE PIC X(20). 05 LKP-OUT-ERR-COMMENT PIC X(50). 05 LKP-OUT-ERR-RSRC-DATA-NAME PIC X(8). 05 LKP-OUT-ERR-RSRC-DATA-TEXT PIC X(254). 05 LKP-OUT-STATUS-FLG PIC X(1). 05 LKP-OUT-PRI-AFF-TYPE PIC X(1). 05 LKP-OUT-SSN PIC X(12). 05 LKP-OUT-UFID PIC X(8). 05 LKP-OUT-DISPLAY-NM PIC X(44). 05 LKP-OUT-WORKING-TITLE PIC X(44). 05 LKP-OUT-ETHNICITY PIC X(1). 05 LKP-OUT-GENDER PIC X(1). 05 LKP-OUT-BIRTH-DT PIC X(10). 05 LKP-OUT-DOMESTIC-STAT PIC X(1). 05 LKP-OUT-CITIZEN-CTRY PIC X(2). 05 LKP-OUT-CITY-OF-BIRTH PIC X(30). 05 LKP-OUT-STAT-OF-BIRTH PIC X(2). 05 LKP-OUT-CTRY-OF-BIRTH PIC X(2). 05 LKP-OUT-VETERAN-STAT PIC X(2). 05 LKP-OUT-HANDICAP-FLG PIC X(1). 05 LKP-OUT-DISABLED-FLG PIC X(1). 05 LKP-OUT-HM-P-PUBL-FLG PIC X(1). 05 LKP-OUT-HM-P-CNTRY-CD PIC X(5). 05 LKP-OUT-HM-P-PREFIX PIC X(2). 05 LKP-OUT-HM-P-AREA-CD PIC X(5). 05 LKP-OUT-HM-P-PHONE-NO PIC X(10). 05 LKP-OUT-HM-INV-P-FLG PIC X(1). 05 LKP-OUT-UF-P-PUBL-FLG PIC X(1). 05 LKP-OUT-UF-P-CNTRY-CD PIC X(5). 05 LKP-OUT-UF-P-PREFIX PIC X(2). 05 LKP-OUT-UF-P-AREA-CD PIC X(5). 05 LKP-OUT-UF-P-PHONE-NO PIC X(10). 05 LKP-OUT-UF-P-EXT PIC X(6). 05 LKP-OUT-UF-INV-P-FLG PIC X(1). 05 LKP-OUT-HM-A-PUBL-FLG PIC X(1). 05 LKP-OUT-HM-A-LINE1-AD PIC X(44). 05 LKP-OUT-HM-A-LINE2-AD PIC X(44). 05 LKP-OUT-HM-A-LINE3-AD PIC X(44). 05 LKP-OUT-HM-A-CITY-NM PIC X(30). 05 LKP-OUT-HM-A-STATE-CD PIC X(2). 05 LKP-OUT-HM-A-ZIP-CD PIC X(12). 05 LKP-OUT-HM-A-CNTRY-CD PIC X(2). 05 LKP-OUT-HM-INV-A-FLG PIC X(1). 05 LKP-OUT-UF-A-PUBL-FLG PIC X(1). 05 LKP-OUT-UF-A-LINE1-AD PIC X(44). 05 LKP-OUT-UF-A-LINE2-AD PIC X(44). 05 LKP-OUT-UF-A-LINE3-AD PIC X(44). 05 LKP-OUT-UF-A-CITY-NM PIC X(30). 05 LKP-OUT-UF-A-STATE-CD PIC X(2). 05 LKP-OUT-UF-A-ZIP-CD PIC X(12). 05 LKP-OUT-UF-A-CNTRY-CD PIC X(2). 05 LKP-OUT-UF-INV-A-FLG PIC X(1). 05 LKP-OUT-UF-E-PUBL-FLG PIC X(1). 05 LKP-OUT-UF-INV-E-FLG PIC X(1). 05 LKP-OUT-PREF-LANG PIC X(30). 05 LKP-OUT-SECURITY-FLG PIC X(1). 05 LKP-OUT-PROTECT-FLG PIC X(1). 05 LKP-OUT-PUBLISH-FLG PIC X(1). 05 LKP-OUT-DECEASED-FLG PIC X(1). 05 LKP-OUT-UPDT-SRC-ID PIC X(12). 05 LKP-OUT-UPDT-UFID PIC X(8). 05 LKP-OUT-UPDT-TS PIC X(26). 05 LKP-OUT-UF-EMAIL-AD-TEXT PIC X(254). 5001-CALL-BAUDD051. MOVE SPACES TO WS-PROC-PRSN-OBT-AREA. MOVE 'THEUFID9 ' TO LKP-IN-UFID. MOVE 'MYPROGRAM ' TO LKP-IN-UPDT-SRC-ID. MOVE 'UPDTUFID' TO LKP-IN-UPDT-UFID. EXEC SQL CALL :WS-DB2-PRSN-OBTL-PGM-ID (:LKP-IN-UFID, :LKP-IN-UPDT-SRC-ID, :LKP-IN-UPDT-UFID, :LKP-OUT-RETURN-CD :WS-OUT-ONLY-IND, :LKP-OUT-DB2-RETURN-CD :WS-OUT-ONLY-IND, :LKP-OUT-FILE-RETURN-CD :WS-OUT-ONLY-IND, :LKP-OUT-ERR-PROGRAM :WS-OUT-ONLY-IND, :LKP-OUT-ERR-PARA :WS-OUT-ONLY-IND, :LKP-OUT-ERR-TABLE :WS-OUT-ONLY-IND, :LKP-OUT-ERR-COMMENT :WS-OUT-ONLY-IND, :LKP-OUT-ERR-RSRC-DATA-NAME :WS-OUT-ONLY-IND, :LKP-OUT-ERR-RSRC-DATA-TEXT :WS-OUT-ONLY-IND, :LKP-OUT-STATUS-FLG :WS-OUT-ONLY-IND, :LKP-OUT-PRI-AFF-TYPE :WS-OUT-ONLY-IND, :LKP-OUT-SSN :WS-OUT-ONLY-IND, :LKP-OUT-UFID :WS-OUT-ONLY-IND, :LKP-OUT-DISPLAY-NM :WS-OUT-ONLY-IND, :LKP-OUT-WORKING-TITLE :WS-OUT-ONLY-IND, :LKP-OUT-ETHNICITY :WS-OUT-ONLY-IND, :LKP-OUT-GENDER :WS-OUT-ONLY-IND, :LKP-OUT-BIRTH-DT :WS-OUT-ONLY-IND, :LKP-OUT-DOMESTIC-STAT :WS-OUT-ONLY-IND, :LKP-OUT-CITIZEN-CTRY :WS-OUT-ONLY-IND, :LKP-OUT-CITY-OF-BIRTH :WS-OUT-ONLY-IND, :LKP-OUT-STAT-OF-BIRTH :WS-OUT-ONLY-IND, :LKP-OUT-CTRY-OF-BIRTH :WS-OUT-ONLY-IND, :LKP-OUT-VETERAN-STAT :WS-OUT-ONLY-IND, :LKP-OUT-HANDICAP-FLG :WS-OUT-ONLY-IND, :LKP-OUT-DISABLED-FLG :WS-OUT-ONLY-IND, :LKP-OUT-HM-P-PUBL-FLG :WS-OUT-ONLY-IND, :LKP-OUT-HM-P-CNTRY-CD :WS-OUT-ONLY-IND, :LKP-OUT-HM-P-PREFIX :WS-OUT-ONLY-IND, :LKP-OUT-HM-P-AREA-CD :WS-OUT-ONLY-IND, :LKP-OUT-HM-P-PHONE-NO :WS-OUT-ONLY-IND, :LKP-OUT-HM-INV-P-FLG :WS-OUT-ONLY-IND, :LKP-OUT-UF-P-PUBL-FLG :WS-OUT-ONLY-IND, :LKP-OUT-UF-P-CNTRY-CD :WS-OUT-ONLY-IND, :LKP-OUT-UF-P-PREFIX :WS-OUT-ONLY-IND, :LKP-OUT-UF-P-AREA-CD :WS-OUT-ONLY-IND, :LKP-OUT-UF-P-PHONE-NO :WS-OUT-ONLY-IND, :LKP-OUT-UF-P-EXT :WS-OUT-ONLY-IND, :LKP-OUT-UF-INV-P-FLG :WS-OUT-ONLY-IND, :LKP-OUT-HM-A-PUBL-FLG :WS-OUT-ONLY-IND, :LKP-OUT-HM-A-LINE1-AD :WS-OUT-ONLY-IND, :LKP-OUT-HM-A-LINE2-AD :WS-OUT-ONLY-IND, :LKP-OUT-HM-A-LINE3-AD :WS-OUT-ONLY-IND, :LKP-OUT-HM-A-CITY-NM :WS-OUT-ONLY-IND, :LKP-OUT-HM-A-STATE-CD :WS-OUT-ONLY-IND, :LKP-OUT-HM-A-ZIP-CD :WS-OUT-ONLY-IND, :LKP-OUT-HM-A-CNTRY-CD :WS-OUT-ONLY-IND, :LKP-OUT-HM-INV-A-FLG :WS-OUT-ONLY-IND, :LKP-OUT-UF-A-PUBL-FLG :WS-OUT-ONLY-IND, :LKP-OUT-UF-A-LINE1-AD :WS-OUT-ONLY-IND, :LKP-OUT-UF-A-LINE2-AD :WS-OUT-ONLY-IND, :LKP-OUT-UF-A-LINE3-AD :WS-OUT-ONLY-IND, :LKP-OUT-UF-A-CITY-NM :WS-OUT-ONLY-IND, :LKP-OUT-UF-A-STATE-CD :WS-OUT-ONLY-IND, :LKP-OUT-UF-A-ZIP-CD :WS-OUT-ONLY-IND, :LKP-OUT-UF-A-CNTRY-CD :WS-OUT-ONLY-IND, :LKP-OUT-UF-INV-A-FLG :WS-OUT-ONLY-IND, :LKP-OUT-UF-E-PUBL-FLG :WS-OUT-ONLY-IND, :LKP-OUT-UF-INV-E-FLG :WS-OUT-ONLY-IND, :LKP-OUT-PREF-LANG :WS-OUT-ONLY-IND, :LKP-OUT-SECURITY-FLG :WS-OUT-ONLY-IND, :LKP-OUT-PROTECT-FLG :WS-OUT-ONLY-IND, :LKP-OUT-PUBLISH-FLG :WS-OUT-ONLY-IND, :LKP-OUT-DECEASED-FLG :WS-OUT-ONLY-IND, :LKP-OUT-UPDT-SRC-ID :WS-OUT-ONLY-IND, :LKP-OUT-UPDT-UFID :WS-OUT-ONLY-IND, :LKP-OUT-UPDT-TS :WS-OUT-ONLY-IND, :LKP-OUT-UF-EMAIL-AD-TEXT :WS-OUT-ONLY-IND, END-EXEC. 5001-END. EXIT.
If a need exists to issue a connect the following code connect to the database prior to issuing the call to the DB2 stored procedure.
01 WS-LOCAL PIC X(10) VALUE 'DATABASE'. EXEC SQL CONNECT TO :WS-LOCAL END-EXEC. If the sqlcode is not ok Then a connect error has occurred. Otherwise do the stored procedure call.
It is good programming practice to make sure you always reset the connection.
EXEC SQL CONNECT RESET END-EXEC.