sqldb2cobolmainframe

Need hlp with cobol\db2 homework


I have this program where I need to read the table DB2.SMLC_RGR_PRCT_RCS where the field NR_SMLC_RGR_PRCT = I01-NR-SMLC-RGR-PRC, comparing the value of the variable with the field and only advancing when they match, but it doesn't matter what command or logic I use use doesn't work, I've used all the select options and tried to loop with fetch too, any tips? NOTE: I WILL LEAVE ONLY THE PART OF THE PROGRAM THAT I AM REFERRING TO AND THE DATA

  *----------------------------------------------------------*
    WORKING-STORAGE  SECTION.
  *----------------------------------------------------------*
   77  CTE-PROG                    PIC X(016)    VALUE
                                   '*** GPOP9450 ***'.
   77  CTE-VERS                    PIC X(006)    VALUE   'VRS001'.
   77  SBVERSAO                    PIC X(008)    VALUE   'SBVERSAO'.
   77  SBABEND                      PIC  X(07) VALUE 'SBABEND'.
  *----------------------------------------------------------*
  *--------------------CONTADORES----------------------------*
   01  GDA-FIM-ARQUIVO             PIC X(03).
   01  GDA-REGISTROS-LIDOS         PIC 9(18) VALUE ZEROS.
   01  GDA-REGISTROS-N-ENCONTRADOS PIC 9(18) VALUE ZEROS.
   01  GDA_REGISTROS_ENCONTRADOS   PIC S9(4) USAGE COMP.
   01  GDA_REG                     PIC S9(4) USAGE COMP.
  *-------------------------------------------------------------*
  *                    ARQUIVO DE ENTRADA

   01 WREG-ARQENTD.
       03 I01-NR-SMLC-RGR-PRCT           PIC S9(4) USAGE COMP.
       03 I01-CODIGO-USUARIO             PIC X(08) VALUE IS SPACES.
  *-------------------------------------------------------------*
  *                     ARQUIVO DE SAIDA 01

   01 WREG-ARQSAI-1.
       03 O001-NR-SMLC-RGR-PRCT          PIC 9(04) VALUE ZEROS.
       03 O001-CODIGO-USUARIO            PIC 9(08) VALUE IS ZEROS.
       03 O001-DATA-PESQUISA             PIC X(10) VALUE IS SPACES.
  *-------------------------------------------------------------*
  *                      ARQUIVO DE SAIDA 02
   01 WREG-ARQSAI-2.
       03 O002-NR-SMLC-RGR-PRCT          PIC S9(4) USAGE COMP.
       03 O002-NR_SMLC_RGR_PRCT          PIC S9(4) USAGE COMP.
       03 O002-CD_OGM_RCS_SMLC           PIC S9(4) USAGE COMP.
       03 O002-TX_SMLC_RGR_PRCT          PIC X(50) VALUE SPACES.
       03 O002-DT_INC_MVT_SMLC          PIC X(10) VALUE SPACES.
       03 O002-DT_FIM_MVT_SMLC          PIC X(18)  VALUE ZEROS.
       03 O002-DATA-ALTERACAO           PIC X(26)  VALUE SPACES.
  *-------------------------------------------------------------*
   LOCAL-STORAGE SECTION.
  *----------------------------------------------------------*
  *--------------------ARÉA SQL----------------------------*
       EXEC SQL
            INCLUDE SQLCA
       END-EXEC.
  *-------------------------------------------------------------*
   PROCEDURE DIVISION.
   000000-INICIO  SECTION.
  *-------------------------------------------------------------*
        CALL SBVERSAO USING CTE-PROG CTE-VERS.


        PERFORM 0001-ABRE-ARQUIVOS
        PERFORM 0002-LER-ARQUIVO UNTIL GDA-FIM-ARQUIVO EQUAL 'S'
        PERFORM 0006-GERA-ESTATISTICAS


        CLOSE  ARQENTRD
               ARQSAI1
               ARQSAI2.

        STOP RUN.

   000000-FIM. EXIT.


   0001-ABRE-ARQUIVOS            SECTION.

       OPEN INPUT  ARQENTRD
            OUTPUT ARQSAI1
                    ARQSAI2
           .

   0001-FIM. EXIT.


   0002-LER-ARQUIVO              SECTION.

        READ ARQENTRD AT END MOVE 'S' TO GDA-FIM-ARQUIVO
                      NOT AT END
                      ADD 1 TO GDA-REGISTROS-LIDOS
                      PERFORM 01-BUSCA-DB2

        .

   0002-FIM. EXIT.


   01-BUSCA-DB2                  SECTION.

        EXEC SQL
          SELECT *
           INTO :GDA_REG
           FROM DB2.SMLC_RGR_PRCT_RCS
           WHERE NR_SMLC_RGR_PRCT = :I01-NR-SMLC-RGR-PRCT
        END-EXEC

       IF SQLCODE = 0
          PERFORM 0005-GRAVA-ARQSAI-1
       ELSE
       IF SQLCODE > 0
             PERFORM CHAMA-ERRO-001
       END-IF

...


Solution

  • You must ensure that the variables in COBOL and DB2 are equivalent. In major flavors of COBOL, host variables must be embedded in a SQL statement.

    The links bellow could help you to understand what is missing in your program.

    https://www.ibm.com/docs/en/db2-for-zos/12?topic=statements-equivalent-sql-cobol-data-types

    https://www.ibm.com/docs/en/db2-for-zos/12?topic=statements-declaring-host-variables-indicator-variables-in-cobol

    https://www.microfocus.com/documentation/server-express/sx20books/dbhost.htm

    The code provided show that you're getting all columns from DB2.SMLC_RGR_PRCT_RCS table and putting into the host variable GDA_REG, which is the same type and length of I01-NR-SMLC-RGR-PRCT. Feel free to edit your post and add the table definition, so we can check for something wrong, and provide specific notes.

    I hope this help you. Have a lot of fun.