Quantcast
Channel: SCN: Message List - SAP HANA Developer Center
Viewing all articles
Browse latest Browse all 9165

Re: Exit command in SQL script

$
0
0

Hi Arpita,

 

One further comment. I'm not exactly sure what EXIT does, but my suspicion is that it's similar to 'return' in Java and other languages.

 

One common application programming requirement is data validation, with a return if required conditions aren't met - i.e. checking input data.

 

One solution in SQLScript is as follows:

 

1) At the beginning of your code (after BEGIN), declare a error condition:

 

DECLARE MYCOND CONDITION FOR SQL_ERROR_CODE 10001; -- example

 

 

2) In your code, check required condition. if not met, signal your condition:

 

IF (requested_start >= requested_end) THEN

          SIGNAL MYCOND SET MESSAGE_TEXT = 'Requested start date must be before requested end date.';

END IF;

 

 

At this point an exception will be thrown with the error msg you specify.

 

 

How do you handle such exceptions gracefully?  After your condition declaration, declare an exit handler. The code following the exit handler declaration is run each time an exception is encountered, giving you an opportunity to handle exceptions gracefully.

 

DECLARE EXIT HANDLER FOR SQLEXCEPTION

SELECT * FROM DEBUG_MSG;

 

So, the entire snippet in my case (requested_start and requested_end are input parameters):

 

-- use this to signal exceptions as required

DECLARE MYCOND CONDITION FOR SQL_ERROR_CODE 10001;

 

 

-- following catches exceptions and runs the code immediately following

DECLARE EXIT HANDLER FOR SQLEXCEPTION

SELECT * FROM DEBUG_MSG;

 

 

 

-- check input dates.

-- check that dates are further from each other than requested duration.

IF (requested_start >= requested_end) THEN

          SIGNAL MYCOND SET MESSAGE_TEXT = 'Requested start date must be before requested end date.';

END IF;


Viewing all articles
Browse latest Browse all 9165

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>