on ‎2013 Feb 18 10:01 AM
Hello Experts,
Is there an 'Exit' like command in SQL script (for script based calculation view)?
Thanks and Regards,
Arpita
Request clarification before answering.
Hi Arpita,
I don't think there is any EXIT as such, but if you are using LOOPS (WHILE or IF) with a condition, then you can always make the condition false for the loop to complete.
For ex: if you have while loop with
WHILE v_val < 50 DO
END WHILE;
In the loop you can always set the v_val to 50 when you want to exit the loop.
Is this what you are looking for or is there something else.
Regards,
Ravi
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Just to add there are BREAK & CONTINUE statements that you could use to exit from loops - page 43 in http://help.sap.com/hana/hana_dev_sqlscript_en.pdf
Thanks,
Anooj
I've just tested this in a Rev61 system, and the RETURN statement works just fine for this.
CREATE PROCEDURE test_return ( in im_flag nvarchar(1))
LANGUAGE SQLSCRIPT
SQL SECURITY INVOKER
--DEFAULT SCHEMA <default_schema_name>
READS SQL DATA AS
BEGIN
/*****************************
Write your procedure logic
*****************************/
select 'This' from dummy;
select 'That' from dummy;
if :im_flag = 'Y' THEN
RETURN;
end if;
select 'The Other' from dummy;
END;
Cheers,
Rich Heilman
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks alot Ravi and Jody!!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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';
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;
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Arpita,
If you run the following procedure, you'll get an exception as expected.
If you uncomment the two commented lines, you'll see that the exception is handled and that a value is stored in a scalar output variable.
Let me know if this helps.
Cheers,
Jody
DROP PROCEDURE TESTP;
CREATE PROCEDURE TESTP(OUT a INTEGER) AS
BEGIN
DECLARE MYCOND CONDITION FOR SQL_ERROR_CODE 10001;
-- DECLARE EXIT HANDLER FOR SQLEXCEPTION
-- SELECT 5 INTO a FROM DUMMY;
SIGNAL MYCOND SET MESSAGE_TEXT = 'Enter a message here';
END;
CALL TESTP(?);
Hi Arpita,
I think Jody already mentioned that. You can check the value of the variable and can SIGNAL the exception.
IF (variable = value) THEN
SIGNAL MYCOND SET MESSAGE_TEXT = 'Requested start date must be before requested end date.';
END IF;
Surprisingly, I could not find any information on SIGNAL or Declaring custom exceptions (except for SQL exception in sample codes) in the SQL script guide.
This is quite an interesting and much awaited feature in SQL script and I am keen to know more about it.
Regards.
Ravi
BREAK will get you out of a loop, but subsequent code will execute. Signaling your condition will stop execution of your procedure. If you don't want error messages, etc - just make sure to include the exit handler syntax above, and just don't execute any SQL following that exit handler.
Try it out once you get access to test system and let me know if you have any questions.
| User | Count |
|---|---|
| 12 | |
| 9 | |
| 6 | |
| 4 | |
| 4 | |
| 3 | |
| 3 | |
| 2 | |
| 2 | |
| 2 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.