Results 1 to 3 of 3
  1. #1
    Join Date
    Oct 2012
    Posts
    2

    Unanswered: db2 SELECT return code ($?) different

    In the below script, the db2 return code ($?) was different in two different executions for the same SELECT statement when there were no rows returned.

    In the first script run, the return code was a zero, which was unexpected! In the second script run, the return code was a non-zero, which was as expected. The script log from the first script run and second script run are also provided below.

    The question is why "$?" was incorrectly set to 0 when there were no rows returned by the SELECT statement in the first script run?


    -------------------------
    AIX 5.3; KSH Shell script:
    -------------------------
    excp_table_name="${TBL_NM}_EXCP"
    db2 -v "select tabname from syscat.tables where tabname = upper('$excp_table_name') and tabschema = upper('$SCHEMA_NM')"
    if [[ $? -ne 0 ]]
    then
    echo "INFO: Exception table does not exist in ($SCHEMA_NM) schema\n"
    EXCEPT_EXISTS=0
    else
    echo "INFO: Exception table exists in ($SCHEMA_NM) schema\n"
    EXCEPT_EXISTS=1
    fi

    --------------------
    First Run Script Log:
    --------------------
    select tabname from syscat.tables where tabname = upper('DLY_PRCSD_PERSNL_PYMT_FACT_EXCP') and tabschema = upper('DW')
    TABNAME
    --------------------------------------------------------------------------------------------------------------------------------
    0 record(s) selected.

    INFO: Exception table exists in (DW) schema


    -----------------------
    Second Run Script Log:
    -----------------------
    select tabname from syscat.tables where tabname = upper('DLY_PRCSD_PERSNL_PYMT_FACT_EXCP') and tabschema = upper('DW')
    TABNAME
    --------------------------------------------------------------------------------------------------------------------------------
    0 record(s) selected.

    INFO: Exception table does not exist in (DW) schema

  2. #2
    Join Date
    Aug 2008
    Location
    Toronto, Canada
    Posts
    2,367
    What about 3rd, 4th, etc... run? Was it always non-zero?


    I always get RC=1 with AIX 6.1 and DB2 v9.7 (don't have access to AIX 5.3):

    test.ksh:
    #!/bin/ksh
    db2 connect to xxxxx > /dev/null
    db2 -v "select * from test"
    if [[ $? -ne 0 ]]
    then
    echo "INFO: Exception table does not exist\n"
    EXCEPT_EXISTS=0
    else
    echo "INFO: Exception table exists\n"
    EXCEPT_EXISTS=1
    fi



    > ./test.ksh
    select * from test

    C1
    -----------

    0 record(s) selected.


    INFO: Exception table does not exist



    > ./test.ksh
    select * from test

    C1
    -----------

    0 record(s) selected.


    INFO: Exception table does not exist

  3. #3
    Join Date
    Oct 2012
    Posts
    2
    Yes, in the subsequent runs, it is always non-zero, as expected.

    What I don't understand is what could make "$?" value a zero though the SELECT returned zero rows.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •