Skip to Content

How to exit hdbsql if an error occurs while executing a script?

I'm using HANA hdbsql to execute a SQL script containing many SQL statements. I want it to immediately stop processing the input file and exit with an error code as soon as any statement gets an error. Is there a way to do this? In Oracle's sqlplus, this functionality is enabled with "whenever sqlerror exit sql.sqlcode". I haven't found anything like this in the hdbsql documentation. Not having this capability makes it very difficult to create effective scripts. You have to be able to detect when something doesn't work and stop.

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

2 Answers

  • Best Answer
    Posted on Oct 31, 2019 at 03:22 AM

    That is possible with hdbsql for HANA 2 (my version is 2.3.119) by using the -E <error code> command line parameter.

    For example with a script like this:

    > cat test.sql
    select '1', current_timestamp from dummy;
    insert into test values (1, 'YYY');
    select '2', current_timestamp from dummy;

    and a table that has a primary key constraint on the first value, I should get an error message when I run the script multiple times.

    > ./hdbsql -U HXEDEV -E 5 -I test.sql
    "1","2019-10-31 14:20:51.844000000"
    * 301: unique constraint violated: Table(TEST) with error: unique constraint violation on pos=0 for table HXE::SYSTEM:TEST$delta_1$en, key: AAA=1 already exists as udiv=1 SQLSTATE: 23000

    The script has stopped now and hdbsql exited. The error code should be 5 (as we specified -E 5):

    > echo $?

    So, "it works on my machine!" ;-)



    Add a comment
    10|10000 characters needed characters exceeded

    • Aha! "-E <exit code>" Thank you, Lars.

      The -E option is NOT documented in the SAP HANA Administration Guide for SAP HANA Platform (Chapter 14 - SAP HANA HDBSQL Reference) which is where I looked. It does show up in the usage text if you type "hdbsql -?" so at least it is there.

      Note to SAP: You need to update the documentation!

  • Posted on Oct 31, 2019 at 01:22 AM


    I am afraid that currently we don't the option to cancel them.


    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.