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
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