cancel
Showing results for 
Search instead for 
Did you mean: 

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

former_member657517
Discoverer
0 Kudos

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.

Accepted Solutions (1)

Accepted Solutions (1)

lbreddemann
Active Contributor

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',CURRENT_TIMESTAMP
"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 $?
5

So, "it works on my machine!" 😉

Cheers,

Lars

former_member657517
Discoverer

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!

Answers (1)

Answers (1)

msundararaja_perumal
Active Contributor
0 Kudos

Hello,

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

Thanks.

former_member657517
Discoverer
0 Kudos

Thank you for the answer, although not what I hoped for. As I said above, not having this capability makes it very difficult to create effective scripts.