我不是mysql存储过程的专家。 我需要从查询返回元素的结果集。但是,如果我调用此过程,则只有一行,并且似乎存储被调用了n次。 相反,我只希望在输出中有很多行的调用存储过程。
这是存储的,请参阅注释以更好地理解我的目的
DELIMITER //
DROP PROCEDURE IF EXISTS getExAnte //
CREATE PROCEDURE
getExAnte(startDate DateTime , stopDate DateTime)
BEGIN
DECLARE id INT ;
DECLARE instrumental TINYINT ;
DECLARE done BOOLEAN DEFAULT FALSE;
DECLARE health_care_cursor CURSOR FOR SELECT h.id,h.instrumental FROM health_cares h inner join health_care_health_care_types hht on h.id=hht.health_care_id;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN health_care_cursor;
get_health_cares_loop: LOOP
IF done THEN
LEAVE get_health_cares_loop;
END IF;
FETCH health_care_cursor INTO id,instrumental;
############## I NEED TO ADD THIS QUERY RESULT TO SOME ARRAY OR CURSOR... HOW ?
SELECT distinct
count(*) as numTotPren ,
sum(case when appointment_date=availability_date then 1 else 0 end) as numTotPrenCheck,
sum(case when (appointment_date=availability_date AND urgency = 'B') then 1 else 0 end) as numB,
sum(case when (appointment_date=availability_date AND urgency = 'B' AND DATEDIFF(availability_date, contact_date) <= 10) then 1 else 0 end) as numBinTime,
sum(case when (appointment_date=availability_date AND urgency = 'D') then 1 else 0 end) as numD,
sum(case when (appointment_date=availability_date AND urgency = 'D' AND DATEDIFF(availability_date, contact_date) <= 30) then 1 else 0 end) as numDinTime
FROM cup_reservations
where
contact_date >=startDate and
contact_date <stopDate and
obsolete IS NULL and
health_care_code in (SELECT t.catalog_code FROM health_care_types t inner join health_care_health_care_types ht on t.id=ht.health_care_type_id where ht.health_care_id=id )
;
END LOOP get_health_cares_loop;
CLOSE health_care_cursor;
### NOW I NEED A RESULT SET OF ROWS TO RETURN
END
//
DELIMITER ;
答案 0 :(得分:0)
MySQL中没有数组变量,但是您可以使用临时表存储部分结果并进行常规查询,并将其用作“结果”
DELIMITER //
DROP PROCEDURE IF EXISTS getExAnte //
CREATE PROCEDURE
getExAnte(startDate DateTime , stopDate DateTime)
BEGIN
DECLARE id INT ;
DECLARE instrumental TINYINT ;
DECLARE done BOOLEAN DEFAULT FALSE;
DECLARE health_care_cursor CURSOR FOR SELECT h.id,h.instrumental FROM health_cares h inner join health_care_health_care_types hht on h.id=hht.health_care_id;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN health_care_cursor;
/*1: CREATE TEMPORARY TABLE*/
DROP TEMPORARY TABLE IF EXISTS results_table;
CREATE TEMPORARY TABLE results_table(
numTotPren int,
numTotPrenCheck int,
numB int,
numBinTime int,
numD int,
numDinTime int
);
get_health_cares_loop: LOOP
IF done THEN
LEAVE get_health_cares_loop;
END IF;
FETCH health_care_cursor INTO id,instrumental;
/*2: SAVE YOUR RESULTS IN TEMPORARY TABLE */
INSERT INTO results_table (numTotPren,numTotPrenCheck,numB,numBinTime,numD,numDinTime)
SELECT distinct
count(*) as numTotPren ,
sum(case when appointment_date=availability_date then 1 else 0 end) as numTotPrenCheck,
sum(case when (appointment_date=availability_date AND urgency = 'B') then 1 else 0 end) as numB,
sum(case when (appointment_date=availability_date AND urgency = 'B' AND DATEDIFF(availability_date, contact_date) <= 10) then 1 else 0 end) as numBinTime,
sum(case when (appointment_date=availability_date AND urgency = 'D') then 1 else 0 end) as numD,
sum(case when (appointment_date=availability_date AND urgency = 'D' AND DATEDIFF(availability_date, contact_date) <= 30) then 1 else 0 end) as numDinTime
FROM cup_reservations
WHERE
contact_date >=startDate and
contact_date <stopDate and
obsolete IS NULL and
health_care_code in (SELECT t.catalog_code FROM health_care_types t inner join health_care_health_care_types ht on t.id=ht.health_care_type_id where ht.health_care_id=id )
;
END LOOP get_health_cares_loop;
CLOSE health_care_cursor;
/*3: FINALLY RUN A REGULAR QUERY */
SELECT * FROM results_table;
END
//
DELIMITER ;