5

我正在处理错误的这个存储过程。我收到一个Result consisted of more than one row错误,但仅限于某些JOB_ID_INPUT值。我了解导致此错误的原因,因此我尝试非常小心以确保我的返回值在应该是标量时是标量。很难看到存储过程,所以我不确定哪里会产生错误。由于错误是有条件地抛出的,因此我认为内存可能是一个问题,或者游标重用。我不经常使用游标,所以我不确定。感谢任何提供帮助的人。

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 ;
4

2 回答 2

4

我想你想用

 LIMIT 1

在您的选择中,不是

 WHERE 1

除了使用这个安全网之外,您还应该了解您的数据,以找出您获得多个结果的原因。没有看到数据,我很难猜测。

于 2013-06-12T16:50:54.967 回答
4

(正如@cdonner所WHERE 1指出的)绝对看起来不正确,但我很确定这个错误正在发生,因为您的SELECT ... INTO命令之一返回了不止一行。

这个应该没问题,因为它是一个没有 a 的聚合GROUP BY,它总是返回一行:

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

所以大概是这个:

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

试着弄清楚这个查询是否有可能返回多行,如果是,你如何解决它。一种方法可能是MAX总分:

SELECT MAX(tests.overall_sore) INTO _overall_score
  FROM tests
  WHERE user_id = candidate_id
于 2013-06-12T16:57:30.583 回答