MySQL Ergebnis Bestand aus mehr als einer Zeile der gespeicherten Prozedur

Diese gespeicherte Prozedur, in der ich arbeite, Fehler einige Zeit. Ich bin immer ein Result consisted of more than one row Fehler, aber nur für bestimmte JOB_ID_INPUT Werte. Ich verstehe, was diesen Fehler verursacht, und so habe ich versucht, wirklich vorsichtig sein, um sicherzustellen, dass meine Rückgabewerte Skalare, wenn Sie sein sollten. Seine harten, um zu sehen, in der gespeicherten Prozedur, so bin ich nicht sicher, wo der Fehler erzeugt werden kann. Da der Fehler ausgelöst wird bedingt, es hat mich zum nachdenken Speicher könnte ein Problem sein,, oder den cursor-Wiederverwendung. Ich arbeite nicht mit Cursor so oft ich bin mir nicht sicher. Danke jedem, der hilft.

DROP PROCEDURE IF EXISTS export_job_candidates;
DELIMITER $$
CREATE PROCEDURE export_job_candidates (IN JOB_ID_INPUT INT(11))
BEGIN

DECLARE candidate_count INT(11) DEFAULT 0;
DECLARE candidate_id INT(11) DEFAULT 0;

# these are the ib variables
DECLARE _overall_score DECIMAL(5, 2) DEFAULT 0.0;

# declare the cursor that will be needed for this SP
DECLARE curs CURSOR FOR SELECT user_id FROM job_application WHERE job_id = JOB_ID_INPUT;

# this table stores all of the data that will be returned from the various tables that will be joined together to build the final export
CREATE TEMPORARY TABLE IF NOT EXISTS candidate_stats_temp_table (
    overall_score_ib DECIMAL(5, 2) DEFAULT 0.0
) engine = memory;

SELECT COUNT(job_application.id) INTO candidate_count FROM job_application WHERE job_id = JOB_ID_INPUT;

OPEN curs;

# loop controlling the insert of data into the temp table that is retuned by this function
insert_loop: LOOP

    # end the loop if there is no more computation that needs to be done
    IF candidate_count = 0 THEN 
        LEAVE insert_loop;
    END IF;

    FETCH curs INTO candidate_id;

    # get the ib data that may exist for this user
    SELECT
        tests.overall_score
    INTO 
        _overall_score
    FROM 
        tests
    WHERE
        user_id = candidate_id;

    #build the insert for the table that is being constructed via this loop 
    INSERT INTO candidate_stats_temp_table (
        overall_score
    ) VALUES (
        _overall_score
    );

    SET candidate_count = candidate_count - 1;

END LOOP;

CLOSE curs;

SELECT * FROM candidate_stats_temp_table WHERE 1;

END $$
DELIMITER ;

InformationsquelleAutor usumoio | 2013-06-12

Schreibe einen Kommentar