Exit codes for dbsql on UNIX systems
On UNIX client systems, the exit codes from the dbsql command provide information about the execution status of the SQL commands.
- 0
- All queries were successful.
- 1
- At least one query was successful.
- 2
- All queries failed.
$ dbsql -f mytest.sql
[IBM][CLI Driver][DB2/LINUXX8664] SQL0204N "BLUDB.T11" is an undefined
name. SQLSTATE=42704
$ echo $?
2
- 0
- Query succeeded
- 1
- 1 SQL failed (issues with syntax, failed authentication, permissions)
- -2 or 254
- Connection failed
- -3 or 253
- User cancellation request
- -4 or 252
- Session was terminated
$ dbsql -c "select * from t11"
[IBM][CLI Driver][DB2/LINUXX8664] SQL0204N "BLUDB.T11" is an undefined
name. SQLSTATE=42704
$ echo $?
255
The dbsql -f command runs all statements in the file. If you want the dbsql command to stop and exit when it encounters an error with a query, include -v ON_ERROR_STOP=1 on the command line. The exit code is 1 if there were successful queries before the failed query, or 2 if the first query failed.
<initial SQL queries>
\set ON_ERROR_STOP ON
<queries inside block>
\unset ON_ERROR_STOP
<trailing SQL queries>
The success or failure of queries in the initial SQL command section or the trailing SQL command section are ignored when there is an ON_ERROR_STOP block. The success or failure of the commands inside the block determine the exit value. The exit codes are 0 (all queries in the block succeeded), 1 (some queries were successful), or 2 (all queries inside the block failed). If you do not include the command to unset the ON_ERROR_STOP block, the block ends at the end of the file.