cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Exit command in SQL script

arpitasethi
Product and Topic Expert
Product and Topic Expert
0 Likes
6,054

Hello Experts,

Is there an 'Exit' like command in SQL script (for script based calculation view)?

Thanks and Regards,

Arpita

View Entire Topic
arpitasethi
Product and Topic Expert
Product and Topic Expert
0 Likes

Thanks alot Ravi and Jody!!

Former Member
0 Likes

Hi folks,

Just a follow up that I found a bit more documentation on exception handling underneath the trigger section of the HANA SQL guide for SP5 (not the SQLScript guide as one might expect). Check out page 99 of the guide.

Another useful feature of this functionality is RESIGNAL. Example use case:

1) You want to stop execution of a stored procedure under certain circumstances but don't need to throw an error (i.e. you need something like 'return' in Java).

2) In order to implement this, you signal a custom condition that you defined call MYCOND, and you also defined an EXIT_HANDLER that handles exceptions.

3) You want to make sure that other SQL exceptions still get thrown (i.e. you don't want them 'caught up' in your generic exit handler.

In this case, you can do something like this:

DECLARE MYCOND CONDITION FOR SQL_ERROR_CODE 10001;

DECLARE EXIT HANDLER FOR SQLEXCEPTION

IF (::SQL_ERROR_CODE != 10001) THEN

     RESIGNAL;

ELSE

     -- do whatever else you might want to do once your custom exception is caught. Kind of like 'finally' following a try/catch block

END;

-- in your proc, when you want to stop execution because, for example, you have your answer, signal your exception:

SIGNAL MYCOND SET MESSAGE_TEXT = 'If this exception is handled, this message won't make it to the client';

Former Member
0 Likes

After messing around with this a bit, there's a much cleaner approach:

DECLARE EXIT HANDLER FOR MYCOND

-- enter whatever code you want executed if any

All normal SQL exceptions that might be thrown will still be thrown without getting caught in your handler. No need for cludgy IF/ELSE.