Sybase stored procedure exception handling - exception

I am new to sybase. After studying a bit I came to know following is the correct way to handle error/exceptions in sybase stored procedure.
CREATE PROCEDURE dbo.sp_testErrorHandling (#age varchar(20))
AS
BEGIN
DECLARE #myerr int
BEGIN TRANSACTION mytrans
DELETE FROM TestStoredProc where Name='Z'
IF ##error<>0 BEGIN SELECT #myerr=##error GOTO failed END
DECLARE #result int
EXECUTE #result = 5/0 /* throws an exception */
IF ##error<>0 BEGIN SELECT #myerr=##error GOTO failed END
COMMIT TRANSACTION mytrans
RETURN 0
failed:
ROLLBACK TRANSACTION mytrans
return #myerr
END
I thought, this stored procedure would return the error code correspondin to exception devision by zero. But actually it is throwing exception. Please help me to undestand the behaviour.
Regards,
Anirban
Anirban

To be able to capture / handle 'divide by zero' errors better, you will have to set appropriate values to the arithignore arith_overflow system option in the Sybase server. There is some good documentation in the Sybase online manual at :
http://infocenter.sybase.com/help/index.jsp?topic=/com.sybase.infocenter.dc32300.1550/html/sqlug/X47118.htm

Related

MySql: Stored procedure syntax error due to Exception command

CREATE or replace procedure `isEven`(IN num int,OUT result varchar(6)) is
BEGIN
set result=(select IF(MOD(num,2)=0,"Even","Odd"));
EXCEPTION WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE ("Error!");
END isEven;
I am trying to make a stored procedure but there is an error in my code I can't understand what is the issue in this code.
Can anyone let me the correct syntax of the exception?

Data Truncation error in stored procedure but SSIS package not failing

I'm having stored procedure: usp_data. If I run in SSMS, am not getting any error but if I remove being and end trans then am I'm getting data truncation error. I'm using SSIS package data flow task to run this stored procedure. Job is going thru and not failing. What needs to be done to fix this SP and SSIS package. Which one needs to fix? I have put dummy fields and table name.
Server : MS SQL
Appreciate your help.
create procedure usp_data as
begin
begin trans
begin try
insert into table1 (field1, field2)
select field1,field2 from table2
commit trans
return
end try
begin catch
if ##transcount>0
begin
rollback trans
end
set #err = ERROR_MESSAGE()
RAISERROR (#err,-1,-1,'usp_data')
print(ERROR_MESSAGE())
RETURN -1
END Catch
END
An error severity of 11 or higher is needed in order for the error message to be considered an exception. A severity of 10 or less are considered informational/warning messages and do not throw an exception in consuming applications.
You can observe this in SSMS with the following:
--this is informational message
RAISERROR ('example %s',-1,-1,'usp_data');
--this is an error message
RAISERROR ('example %s',16,-1,'usp_data');
In SQL Server 2012 and later, consider using THROW to re-raise the original error. Below is the boiler plate code I suggest. Additionally, it's a good practice to specify SET XACT_ABORT ON; in stored procedures with explict transactions to ensure the transaction is rolled back immediately when a client query timeout occurs.
BEGIN CATCH
IF ##transcount > 0 ROLLBACK;
THROW;
END CATCH;

How do I handle errors in a basic MySQL stored procedure?

Let's say I have a stored procedure:
DELIMITER $$
CREATE DEFINER=`root`#`%` PROCEDURE `SetupBlocks`(
IN myBlock VARCHAR(20)
)
BEGIN
INSERT INTO blocks (block)
VALUE (myBlock);
END
What if I made a mistake in my code and DB setup, and the column block only allows for 15 characters but someone using my application has been able to get 20 characters into myBlock? I would get an error similar to this for this example:
Error Code: 1406. Data too long for column 'block'
Is there some way to handle any error (not just this one) and then report it into a table called BlockSprocErrors for monitoring within MySQL and within the stored procedure itself?
Just looking for a very basic example by modifying my procedure above.
Here is what I ended up doing:
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
GET DIAGNOSTICS condition 1
#SQLState = RETURNED_SQLSTATE, #SQLMessage = MESSAGE_TEXT;
SELECT CONCAT('Database error occurred, state - ',#SQLState, '; error msg - ', #SQLMessage) INTO #errorString;
CALL Collectors_Errors
(#errorString,
'Collectors_InsertAlbum',
barcodeApp,
usernameApp);
END;

How to convert oracle procedure into mysql with exception handling

Hi all,
Am new to mysql.Actually am oracle developer now am converting some procedure from oracle to mysql.In those changes I have doubt in Mysql.
In Oracle:-
Create procudure test_proc(p_id in varchar2,
p_error_code out number,
p_error_msg out varchar2) is
begin
insert into test_1(a) values(p_id);
commit;
p_error_code:=0;
exception when others then
p_error_code:=1;
p_error_msg:=substr(sqlerrm,1,150);
rollback;
return;
end;
I need same type of procedure in mysql or sample procedure how to handle exception and show it error as output variable like sqlerrm in oracle.
Thanks and regards,
vinoth
You can use EXIT and CONTINUE Handler. But you cant get clear error details as you get in ORACLE.

Trying to learn MySQL and transactions

Over the last couple of days I have tried to write an Stored procedure in MySQL and I have some truble getting it to work. Hope someone here can give me some input :)
The example I post is for asp.Net Membership provider to create a new user. I expect to send email and password to the DB and get an int return to verify that the userdeatils was written to the DB.
I use a MySQL DB 5.1 (I think) and write the SQL to a webinterface.
I got 2 sidequestions, can someone explain that too :):
1) I use a DELIMITER, but do not know what it does.
2) I am not sure if I have to do other things then to set autocommit = 0 to get transactions to work, or if I even have to do that.
I know that I could have used a IF / ELSE statement instead of a transaction, but would like to do it with one to find out how it works. (I expect to use it alot later)
The code I can not get to work:
DELIMITER //
CREATE DEFINER=`websharp_dk`#`%` PROCEDURE `CreateUser`(
IN _username VARCHAR(100),
IN _Password VARCHAR(100))
RETURNS INT
BEGIN
SET autocommit = 0;
DECLARE return_value INT;
BEGIN TRY
START TRANSACTION
INSERT INTO User
(Email
,Password
,Failed_Password_Count
,Creation_Date)
VALUES
(_username
,_Password
,0
,Datetime.Now())
SET return_value = 1;
COMMIT;
END TRY
BEGIN CATCH
ROLLBACK
SET return_value = 0;
END CATCH
BEGIN FINALLY
RETURN return_value;
END FINALLY
END//
DELIMITER ;
Edit:
The error message I get is:
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INT BEGIN SET autocommit = 0; DECLARE return_value INT; ' at line 4
To get support for transactions, make sure you are using the InnoDB storage engine rather than the default MyISAM.
As far as that code itself, my first question would be, why are you wrapping that single query in a transaction? Also, what errors are you seeing?
The delimiter redefines what sequence of characters you use to end an sql statement. The entire create procedure is one big statement and you need to tell MySQL where it ends with something (would normally be ';'). But since you have a bunch of other statements in the "body" (between BEGIN and END) of the create procedure statement that all need to be ended too you need to redefine the delimiter so you don't end the create procedure statement at the first ';'.
Without redefining the delimiter, MySQL would think that the create procedure statement looked like this and then begin a new statement:
CREATE DEFINER=`websharp_dk`#`%` PROCEDURE `CreateUser`(
IN _username VARCHAR(100),
IN _Password VARCHAR(100))
RETURNS INT
BEGIN
SET autocommit = 0;
Using DELIMITER ; at the end of the script changes the delimiter back to ';' and is not needed although it's good practice to do so.