MYSQL 5.5 Nested Stored Procedure Error Handling - mysql

I'm using MYSQL 5.5, How to do Error Handling with Nested Stored Procedure. Pretty much most of the stored procedure, does truncate table, insert into select * from and insert into on duplicate key update.
If there is any issue with any of the nested stored procedure, I would like to catch that error or tell me that error happened in that particular stored procedure.
Right now, due to the nested stored procedure, there is no way for me to know,
in which stored procedure, the exception happended.

Within each stored procedure, you can DECLARE ... HANDLER for the errors in which you're interested and have them write state information to a temporary table; that table can subsequently be read by other (outer) stored procedures to determine what error(s), if any, were raised.

Related

MariaDB stored functions

I currently have all of my sql queries written in my PHP files, within each class method. Is it possible to move all of these queries into stored procedures or stored functions in the database & simply pass the corresponding values / arguments into them from PHP?
I have read some of the documentation & it still appears unclear.
Thank you. :)
DELIMITER $$
create procedure `accounting`.`delete_invoice_line` (invoice_line_id INT)
BEGIN
delete from invoice_line where id = invoice_line_id;
END;
$$
DELIMITER ;
I had to figure the format for creating the procedure. I am following this pattern & it appears to be working properly. Then granting execute privileges for the user name.
Thank you all for your input. :)
Most queries can be moved into stored procedures, but probably not all of them. See mariadb's documentation on which SQL statements cannot be used in stored procedures:
ALTER VIEW; you can use CREATE OR REPLACE VIEW instead. LOAD DATA and
LOAD TABLE. INSERT DELAYED is permitted, but the statement is handled
as a regular INSERT.
LOCK TABLES and UNLOCK TABLES.
References to local variables within prepared statements inside a stored routine (use user-defined variables instead).
BEGIN (WORK) is treated as the beginning of a BEGIN END block, not a transaction, so START TRANSACTION needs to be used instead.
The number of permitted
recursive calls is limited to max_sp_recursion_depth. If this variable
is 0 (default), recursivity is disabled. The limit does not apply to
stored functions.
Most statements that are not permitted in prepared
statements are not permitted in stored programs. See Prepare
Statement:Permitted statements for a list of statements that can be
used.
SIGNAL, RESIGNAL and GET DIAGNOSTICS are exceptions, and may be
used in stored routines
Having said this, even though a SQL statement can be moved into a stored procedure, you may not necessarily want to do that due to code complexity or performance reasons.

Creating an event inside a mysqlstored procedure

can we create an event inside MySQL stored procedure? i tried creating event scheduler inside MySQL stored procedure ,but event is not created
MYSQL documentation has not been updated even in v8 to remove the inconsistency between https://dev.mysql.com/doc/refman/8.0/en/create-event.html which states in the last sentence 'You can create an event as part of a stored routine, but an event cannot be created by another event.' and https://dev.mysql.com/doc/refman/8.0/en/stored-program-restrictions.html under EVENT RESTRICTIONS which states 'An event may not be created, altered, or dropped from within a stored program' - the latter being the correct statement.
Restrictions on Stored Programs
Generally, statements not permitted in SQL prepared statements are also not permitted in stored programs. ... Exceptions are SIGNAL, RESIGNAL, and GET DIAGNOSTICS, which are not permissible as prepared statements but are permitted in stored programs.
Prepared Statements
There is NO CREATE EVENT in "SQL Syntax Allowed in Prepared Statements" list.
An attempt to create procedure which tries to create event causes
ERROR 1576 (HY000): Recursion of EVENT DDL statements is forbidden when body is present.

Bulk Insert statements MYSQL Stored Procedure

I have a stored procedure where I need, amongst other things, to be able to do multiple inserts to a particular table.
The number of inserts to this table can vary.
The stored procedure is being called via JDBC.
Passing parameters for a single insert statement to a stored procedure is easy. Is there a way I can pass an array of values to the stored procedure, and then loop through the array to perform the insert statements?
I'm pretty new to stored procedures, so thanks in advance for all your help....
This was achieved by including everything in a transaction and it will automatically rollback if anything goes haywire. THis is done in JDBC by setting the connection autocommit to be false, and then autocommiting
conn.setAutoCommit(false);
conn.commit();

How to insert the SQL Error message into table

I want to store the error messages which occurs while executing the stored procedure in another error table.
Here is the my sample procedure having some error statements.
DELIMITER $$
DROP PROCEDURE IF EXISTS `test`$$
CREATE PROCEDURE `test`()
BEGIN
SELECT * FROM emp;
END $$
DELIMITER $$;
When i call the above procedure it gives me error.I wnat to store this error code & message in another table as "error".
Any pointers are appreciated.
Thanks in Advance.
The standard way to handle this in MySQL is to declare a HANDLER to handle the error condition the way you want. This allows you to insert an error message into another table if you want, and then to either CONTINUE or EXIT the running procedure as required.
Here's the documentation:
http://dev.mysql.com/doc/refman/5.0/en/declare-handler.html
Unfortunately you will not be able to access the SQLSTATE of the statement that caused the error, so this approach is somewhat limited.
Here's another relevant question on SO with much more detail:
MySQL Stored Procedure Error Handling
SQL ERROR means there is something wrong with your SQL Query. It may (or may not) depend on your table. If the error is TABLE SPECIFIC or QUERY SYNTAX ERROR, that is, if there is something wrong with a specific table ONLY, or with a query, then definitely you can insert your errors into a table. But, if there is something with your CONNECTION or something else, then you cannot insert errors into any table.
ALWAYS try to log your errors in a html file or txt (text) file, so that you can smoothly access it. Also there is a less chance for failure.

Stored Procedures vs Triggers in MySQL

How are STORED PROCEDURES different than TRIGGERS in the MySQL world ?
Stored procedures are stored as precompilated code (stored routine) and called by the programmer wherever it wants to fire. Stored procedure can return value(s). About procedures and functions.
Triggers are named database objects fired automatically when insert, delete, update (or other event) occurred, there can be no explicit invocation. Trigger can not return any data.
About triggers.
You can use procedures in trigger's code.
A trigger is a type of stored procedure, but it runs based off of an event on a table instead of just being a set of instructions to be executed repeatedly.
A trigger is defined to activate when an INSERT, DELETE, or UPDATE statement executes for the associated table.
A stored procedure is a group of Transact-SQL statements compiled into a single execution plan.