Einzelnen Beitrag anzeigen

spaniac

Registriert seit: 28. Jan 2010
20 Beiträge
 
#13

Re: Alternative zu dynamic sql-cursor problematik

  Alt 26. Mai 2010, 11:57
so, wie gesagt, das obere problem ist gelöst worden, vielen dank für den input.

dennoch würde ich gerne noch einmal omatas anmerkung bezüglich des füllens der tabelle aufgreifen:

die von mir oben gezeigt funktion liefert nur die daten für die berechnung eines wertes (sagen wir wert1), ich habe jedoch eine hauptmethode, die alle werte zusammenfasst:


procedure main
ruft mehrere, verschiedene untermethoden auf, die jeweils eine tabelle mit der struktur

|datum|berechneter wert| zurückliefern, also genau wie die oben bereits besprochene stored procedure.


procedure main liefert also eine tabelle mit der struktur

|datum|berechneter wert von unterprocedure1|berechneter wert von unterprocedure2|berechneter wert von unterprocedure3|...
Dies mache ich eigentlich analog der oberen procedure:

SQL-Code:
DELIMITER $$

DROP PROCEDURE IF EXISTS `main_os` $$
CREATE PROCEDURE `main_os`(in_startdate DATE, in_enddate DATE)
    MODIFIES SQL DATA
BEGIN

  /*Declare variables*/
  DECLARE done_offeredcalls INT DEFAULT 0;
  DECLARE done_handledcalls INT DEFAULT 0;
  DECLARE done_answerquota INT DEFAULT 0;

  DECLARE temp_datum DATE;
  DECLARE temp_offeredcalls FLOAT(7,2);
  DECLARE temp_handledcalls FLOAT(7,2);
  DECLARE temp_answerquota FLOAT(7,2);

  /*Declare cursor to access temporary tables from underlying stored procedures*/

  DECLARE cur_offeredcalls CURSOR FOR
    SELECT *
    FROM temp_table_offeredcalls;

  DECLARE cur_handledcalls CURSOR FOR
    SELECT e.datum, SUM(e.wert)
    FROM element e WHERE e.pilot_id = 1 AND e.typ_id=1 AND e.datum BETWEEN in_startdate AND in_enddate GROUP BY e.datum;

  DECLARE cur_answerquota CURSOR FOR
    SELECT *
    FROM temp_table_answerquota;


  /*Declare continue handler to exit loop on empty row*/
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET done_offeredcalls=1, done_handledcalls=1, done_answerquota=1;

  /*Create temporary table to store all values from underlying stored procedures*/
  DROP TEMPORARY TABLE IF EXISTS temp_table_complete_os;
  CREATE TEMPORARY TABLE temp_table_complete_os (datum DATE, offeredcalls FLOAT(7,2), handledcalls FLOAT(7,2), answerquota FLOAT (7,2));


  /*Call stored procedure*/
  CALL offeredcalls_cur ('OS', in_startdate, in_enddate);

  /*Fetch offeredcalls-cursor values into temporary-table - uses sql-update-command to just add the values for the offeredcalls-column to the temporary table*/
  OPEN cur_offeredcalls;
  offeredcalls_loop:LOOP

    FETCH cur_offeredcalls INTO temp_datum, temp_offeredcalls;

    IF done_offeredcalls=1 THEN

      LEAVE offeredcalls_loop;

    END IF;

    IF done_offeredcalls=0 THEN

      INSERT INTO temp_table_complete_os (datum, offeredcalls)

      VALUES (temp_datum, temp_offeredcalls);

    END IF;

  END LOOP offeredcalls_loop;

  CLOSE cur_offeredcalls;


   /*Reset continue handler*/
  SET done_handledcalls=0;

  /*Fetch handledcalls-cursor values into temporary-table - no sql-update command needed*/
  OPEN cur_handledcalls;
  handledcalls_loop:LOOP

    FETCH cur_handledcalls INTO temp_datum, temp_handledcalls;

    IF done_handledcalls=1 THEN

      LEAVE handledcalls_loop;

    END IF;

    IF done_handledcalls=0 THEN

      UPDATE temp_table_complete_os

      SET handledcalls = temp_handledcalls

      WHERE datum = temp_datum;

    END IF;

  END LOOP handledcalls_loop;

  CLOSE cur_handledcalls;

  /*Reset continue handler*/
  SET done_answerquota=0;

  /*Call stored procedure*/
  CALL answerquota_cur ('OS', in_startdate, in_enddate);

  /*Fetch answerquota-cursor values into temporary-table - no sql-update command needed*/
  OPEN cur_answerquota;
  answerquota_loop:LOOP

    FETCH cur_answerquota INTO temp_datum, temp_answerquota;

    IF done_answerquota=1 THEN

      LEAVE answerquota_loop;

    END IF;

    IF done_answerquota=0 THEN

      UPDATE temp_table_complete_os

      SET answerquota = temp_answerquota

      WHERE datum = temp_datum;

    END IF;

  END LOOP answerquota_loop;

  CLOSE cur_answerquota;


  /*Returning the full temporary table*/
  SELECT *
  FROM temp_table_complete_os;


END $$

DELIMITER ;
Wie kann ich die nach der Erzeugung des temp_table_complete_os die Werte aus den anderen temporären Tabellen bzw. Cursorn einfacher einlesen bzw. in einem Durchlauf? Ich würde gerne vermeiden, für jede einzelne Subprocedur einen neuen LOOP zu machen.

Wie immer schon einmal vielen Dank im Voraus!

EDIT: Hier das ganze noch einmal grafisch dargestellt (siehe anhang)
Miniaturansicht angehängter Grafiken
main_os_architektur_126.jpg  
  Mit Zitat antworten Zitat