Question on Mysql procedures

0

Hello Experts, As batch processing is critical for performance when we deal with large data volume processing, so we normally avoid row by row processing and opt for batch execution. And in oracle we use bulk collect for that. I am not getting similar functionality for "Type" and "bulk collect" statement as its in oracle also the exception handling part.

Below is the simple block i wrote using oracle plsql and it works fine. Can you guide me if similar can be re-written in mysql(mainly AWS aurora mysql)? or any alternate way to achieve the same functionality(cursor bulk fetch and type declaration) and exception handling?

Create table employees(employee_id NUMBER(10,0), first_name varchar2(4000));
Insert into employees values (1, 'a');
Insert into employees values(1, 'b');
Insert into employees values(1, 'c');
Insert into employees values(1, 'd');
Insert into employees values(1, 'e');
Insert into employees values(1, 'f');
Insert into employees values(1, 'g');
Insert into employees values(1, 'h');
Insert into employees values(1, 'i');
`SET SERVEROUTPUT ON
DECLARE
CURSOR c1 IS
SELECT first_name FROM employees
ORDER BY first_name;
TYPE fName IS TABLE OF VARCHAR2 (4000);
v_first_name fname;
e exception;
BEGIN
OPEN c1;
LOOP
FETCH c1 bulk collect INTO v_first_name limit 2;
EXIT WHEN c1%NOTFOUND;
FOR idx IN v_first_name.FIRST.. v_first_name.LAST
LOOP
DBMS_OUTPUT.PUT_LINE (idx||' '||v_first_name(idx) );
--Raise e;
END LOOP;
END LOOP;
CLOSE c1;
EXCEPTION
WHEN OTHERS THEN
raise_application_error (-20001, substr(sqlerrm, 100));
END;
/`
asked a year ago249 views
1 Answer
1

Hi there.

Something like this might get you started.

DECLARE var$ID DECIMAL (10, 0);
DECLARE var$NAME VARCHAR (4000);
DECLARE done INT DEFAULT FALSE;
DECLARE v_list CURSOR FOR 
   SELECT * FROM employees;
DECLARE CONTINUE HANDLER FOR NOT FOUND
SET done := TRUE;
OPEN v_list;
read_label:
LOOP
   FETCH v_list INTO var$ID, var$NAME;
   IF done THEN
      LEAVE read_label;
   END IF;
END LOOP;
CLOSE v_list;

I hope this helps (if so, please accept the answer).

profile pictureAWS
EXPERT
answered a year ago
  • Thank You Jose. Correct me if wrong , but it looks like , this modified code will process the cursor records one by one in loop i.e. a row by row processing but not in batch. In case of the Oracle code i posted, there its using a clause "LIMIT 2" along with "BULK COLLECT" which is meant for setting the batch size of '2'. Is there anything similar in Aurora mysql to make the cursor records processed in certain batch size rather in a row by row approach?

    Also i don't find anything similar to oracle's exception handling block(i.e. "When Others" statement) which brings/handles any of the exception raised in the "Begin.. End" block in a controlled way , without throwing it outside the block.

    Basically something similar to "sqlerrem", "sqlcode" as its in Oracle which decodes the exact error due to which the procedure failed along with block/line number and persisting this exception message into another table.

    Can you guide me here?

  • Error handling in MySQL is typically managed through an exit handler. Depending on the use case you may want to rollback or log the event when the handler is called. Example:

    DELIMITER $$
    CREATE PROCEDURE my_procedure(IN search_var VARCHAR(255))
    BEGIN
    DECLARE exit handler for SQLEXCEPTION
     BEGIN
      GET DIAGNOSTICS CONDITION 1 
          @sqlstate = RETURNED_SQLSTATE, 
          @errno = MYSQL_ERRNO, 
          @text = MESSAGE_TEXT;
      SET @error = CONCAT("ERROR ", @errno, " (", @sqlstate, "): ", @text);
      SELECT @error;
     END;
    SELECT * FROM my_table WHERE id = search_var;
    END$$
    DELIMITER ;
    
  • You can use LIMIT only in the SELECT statement to limit the number of results. I have not seen similar functionality to create a batch like Oracle does it. :(

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions