sql >> Database >  >> RDS >> Mysql

Roep een opgeslagen procedure aan vanuit de DECLARE-instructie bij gebruik van cursors in MySQL

Het gebruik van een cursor vereist een standaard boilerplate-code om het te omringen.

Het gebruik van een cursor om een ​​opgeslagen procedure op te roepen voor elke set waarden uit de tabel vereist in wezen dezelfde standaardtekst. Jij SELECT de waarden die u wilt doorgeven, waar u ze ook vandaan haalt (dit kan een tijdelijke tabel, basistabel of weergave zijn, en kan oproepen naar opgeslagen functies bevatten) en roep vervolgens de procedure met die waarden aan.

Ik heb hieronder een syntactisch geldig voorbeeld van die standaardcode geschreven, met opmerkingen om uit te leggen wat elk onderdeel doet. Er zijn weinig dingen waar ik een grotere hekel aan heb dan te worden gevraagd om gewoon iets te doen "gewoon omdat" -- dus alles wordt (hopelijk) uitgelegd.

U noemde het aanroepen van de procedure met meerdere waarden, dus dit voorbeeld gebruikt 2.

Merk op dat er gebeurtenissen die haar gebeuren om een ​​bepaalde reden in een specifieke volgorde staan. Variabelen moeten eerst gedeclareerd worden, cursors moeten gedeclareerd worden voor hun continue handlers, en loops moeten al deze dingen volgen. Dit geeft de indruk dat er hier een vrij extreme inflexibiliteit is, maar dat is niet echt het geval. U kunt de volgorde resetten door extra code in BEGIN te nesten ... END blokkades binnen de procedure-instantie; als je bijvoorbeeld een tweede cursor in de lus nodig hebt, declareer je deze gewoon in de lus, in een andere BEGIN ... END .

DELIMITER $$

DROP PROCEDURE IF EXISTS `my_proc` $$
CREATE PROCEDURE `my_proc`(arg1 INT) -- 1 input argument; you might not need one
BEGIN

-- from http://stackoverflow.com/questions/35858541/call-a-stored-procedure-from-the-declare-statement-when-using-cursors-in-mysql

-- declare the program variables where we'll hold the values we're sending into the procedure;
-- declare as many of them as there are input arguments to the second procedure,
-- with appropriate data types.

DECLARE val1 INT DEFAULT NULL;
DECLARE val2 INT DEFAULT NULL;

-- we need a boolean variable to tell us when the cursor is out of data

DECLARE done TINYINT DEFAULT FALSE;

-- declare a cursor to select the desired columns from the desired source table1
-- the input argument (which you might or might not need) is used in this example for row selection

DECLARE cursor1 -- cursor1 is an arbitrary label, an identifier for the cursor
 CURSOR FOR
 SELECT t1.c1, 
        t1.c2
   FROM table1 t1
  WHERE c3 = arg1; 

-- this fancy spacing is of course not required; all of this could go on the same line.

-- a cursor that runs out of data throws an exception; we need to catch this.
-- when the NOT FOUND condition fires, "done" -- which defaults to FALSE -- will be set to true,
-- and since this is a CONTINUE handler, execution continues with the next statement.   

DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

-- open the cursor

OPEN cursor1;

my_loop: -- loops have to have an arbitrary label; it's used to leave the loop
LOOP

  -- read the values from the next row that is available in the cursor

  FETCH NEXT FROM cursor1 INTO val1, val2;

  IF done THEN -- this will be true when we are out of rows to read, so we go to the statement after END LOOP.
    LEAVE my_loop; 
  ELSE -- val1 and val2 will be the next values from c1 and c2 in table t1, 
       -- so now we call the procedure with them for this "row"
    CALL the_other_procedure(val1,val2);
    -- maybe do more stuff here
  END IF;
END LOOP;

-- execution continues here when LEAVE my_loop is encountered;
-- you might have more things you want to do here

END $$

DELIMITER ;


  1. Haal records op met de hoogste/kleinste <whatever> per groep

  2. Gebruik SERVERPROPERTY() om serverinformatie op te halen in SQL Server

  3. Kan geen verbinding maken met externe database via php

  4. Wat is de locatie van mysql-client .my.cnf in XAMPP voor Windows?