在MySQL存储过程中使用多个选择时出错

时间:2017-04-10 20:14:02

标签: mysql stored-procedures

我试图在一个程序中使用多个选择,但是我在第18行上遇到了一个错误,我对存储过程有点新意,所以我可以使用一些帮助来解决我的问题&# 39;我做错了。

DROP PROCEDURE IF EXISTS `WeatherRecord`;

DELIMITER //
CREATE PROCEDURE WeatherRecord(IN ID INT)
BEGIN
    DECLARE Rid INT UNSIGNED;
    DECLARE Rdate DATE;
    DECLARE Record DECIMAL(5,2);


DECLARE c CURSOR FOR select Date, City_id, High_temp
    FROM Past_weather WHERE High_temp = (
SELECT MAX(High_temp) FROM Past_weather WHERE City_id = ID) && City_id = ID;
open c;
fetch c INTO Rdate, Rid, Record;

Insert into Weather_records(Date, City_id, Record, Record_type, Record_name)
    values(Rdate, Rid, Record, '1', 'HH_Temp');
    close c;

DECLARE c CURSOR FOR select Date, City_id, High_temp
    FROM Past_weather WHERE High_temp = (
SELECT MIN(High_temp) FROM Past_weather WHERE City_id = ID) && City_id = ID;
open c;
fetch c INTO Rdate, Rid, Record;

Insert into Weather_records(Date, City_id, Record, Record_type, Record_name)
    values(Rdate, Rid, Record, '2', 'LH_Temp');
    close c;

DECLARE c CURSOR FOR select Date, City_id, Low_temp
    FROM Past_weather WHERE Low_temp = (
SELECT MAX(Low_temp) FROM Past_weather WHERE City_id = ID) && City_id = ID;
open c;
fetch c INTO Rdate, Rid, Record;

Insert into Weather_records(Date, City_id, Record, Record_type, Record_name)
    values(Rdate, Rid, Record, '3', 'HL_Temp');
    close c;

DECLARE c CURSOR FOR select Date, City_id, Low_temp
    FROM Past_weather WHERE Low_temp = (
SELECT MIN(Low_temp) FROM Past_weather WHERE City_id = ID) && City_id = ID;
open c;
fetch c INTO Rdate, Rid, Record;

Insert into Weather_records(Date, City_id, Record, Record_type, Record_name)
    values(Rdate, Rid, Record, '4', 'LL_Temp');
    close c;


DECLARE c CURSOR FOR select Date, City_id, High_hum
    FROM Past_weather WHERE High_hum = (
SELECT MIN(High_hum) FROM Past_weather WHERE City_id = ID) && City_id = ID;
open c;
fetch c INTO Rdate, Rid, Record;

Insert into Weather_records(Date, City_id, Record, Record_type, Record_name)
    values(Rdate, Rid, Record, '5', 'LH_Hum');
    close c;

DECLARE c CURSOR FOR select Date, City_id, Low_hum
    FROM Past_weather WHERE Low_hum = (
SELECT MIN(Low_hum) FROM Past_weather WHERE City_id = ID) && City_id = ID;
open c;
fetch c INTO Rdate, Rid, Record;

Insert into Weather_records(Date, City_id, Record, Record_type, Record_name)
    values(Rdate, Rid, Record, '6', 'LL_Hum');
    close c;


DECLARE c CURSOR FOR select Date, City_id, High_dew
    FROM Past_weather WHERE High_dew = (
SELECT MAX(High_dew) FROM Past_weather WHERE City_id = ID) && City_id = ID;
open c;
fetch c INTO Rdate, Rid, Record;

Insert into Weather_records(Date, City_id, Record, Record_type, Record_name)
    values(Rdate, Rid, Record, '7', 'HH_dew');
    close c;

DECLARE c CURSOR FOR select Date, City_id, High_dew
    FROM Past_weather WHERE High_dew = (
SELECT MIN(High_dew) FROM Past_weather WHERE City_id = ID) && City_id = ID;
open c;
fetch c INTO Rdate, Rid, Record;

Insert into Weather_records(Date, City_id, Record, Record_type, Record_name)
    values(Rdate, Rid, Record, '8', 'LH_dew');
    close c;

DECLARE c CURSOR FOR select Date, City_id, Low_dew
    FROM Past_weather WHERE Low_dew = (
SELECT MAX(Low_dew) FROM Past_weather WHERE City_id = ID) && City_id = ID;
open c;
fetch c INTO Rdate, Rid, Record;

Insert into Weather_records(Date, City_id, Record, Record_type, Record_name)
    values(Rdate, Rid, Record, '9', 'HL_dew');
    close c;

DECLARE c CURSOR FOR select Date, City_id, Low_dew
    FROM Past_weather WHERE Low_dew = (
SELECT MIN(Low_dew) FROM Past_weather WHERE City_id = ID) && City_id = ID;
open c;
fetch c INTO Rdate, Rid, Record;

Insert into Weather_records(Date, City_id, Record, Record_type, Record_name)
    values(Rdate, Rid, Record, '10', 'LL_dew');
    close c;


DECLARE c CURSOR FOR select Date, City_id, High_ pressure
    FROM Past_weather WHERE High_ pressure = (
SELECT MAX(High_pressure) FROM Past_weather WHERE City_id = ID) && City_id = ID;
open c;
fetch c INTO Rdate, Rid, Record;

Insert into Weather_records(Date, City_id, Record, Record_type, Record_name)
    values(Rdate, Rid, Record, '11', 'HH_pressure');
    close c;

DECLARE c CURSOR FOR select Date, City_id, High_ pressure
    FROM Past_weather WHERE High_ pressure = (
SELECT MIN(High_pressure) FROM Past_weather WHERE City_id = ID) && City_id = ID;
open c;
fetch c INTO Rdate, Rid, Record;

Insert into Weather_records(Date, City_id, Record, Record_type, Record_name)
    values(Rdate, Rid, Record, '12', 'LH_pressure');
    close c;

DECLARE c CURSOR FOR select Date, City_id, Low_ pressure
    FROM Past_weather WHERE Low_ pressure = (
SELECT MAX(Low_pressure) FROM Past_weather WHERE City_id = ID) && City_id = ID;
open c;
fetch c INTO Rdate, Rid, Record;

Insert into Weather_records(Date, City_id, Record, Record_type, Record_name)
    values(Rdate, Rid, Record, '13', 'HL_pressure');
    close c;

DECLARE c CURSOR FOR select Date, City_id, Low_ pressure
    FROM Past_weather WHERE Low_ pressure = (
SELECT MIN(Low_pressure) FROM Past_weather WHERE City_id = ID) && City_id = ID;
open c;
fetch c INTO Rdate, Rid, Record;

Insert into Weather_records(Date, City_id, Record, Record_type, Record_name)
    values(Rdate, Rid, Record, '14', 'LL_pressure');
    close c;


DECLARE c CURSOR FOR select Date, City_id,  Wind_max
    FROM Past_weather WHERE Wind_max = (
SELECT MAX(Wind_max) FROM Past_weather WHERE City_id = ID) && City_id = ID;
open c;
fetch c INTO Rdate, Rid, Record;

Insert into Weather_records(Date, City_id, Record, Record_type, Record_name)
    values(Rdate, Rid, Record, '15', 'H_wind');
    close c;

DECLARE c CURSOR FOR select Date, City_id,  Wind_gust
    FROM Past_weather WHERE Wind_gust = (
SELECT MAX(Wind_gust) FROM Past_weather WHERE City_id = ID) && City_id = ID;
open c;
fetch c INTO Rdate, Rid, Record;

Insert into Weather_records(Date, City_id, Record, Record_type, Record_name)
    values(Rdate, Rid, Record, '16', 'H_gust');
    close c;


DECLARE c CURSOR FOR select Date, City_id, Rainfall
    FROM Past_weather WHERE Rainfall = (
SELECT MAX(Rainfall) FROM Past_weather WHERE City_id = ID) && City_id = ID;
open c;
fetch c INTO Rdate, Rid, Record;

Insert into Weather_records(Date, City_id, Record, Record_type, Record_name)
    values(Rdate, Rid, Record, '17', 'H_rainfall');
    close c;

DECLARE c CURSOR FOR select Date, City_id, Snowfall
    FROM Past_weather WHERE Snowfall = (
SELECT MAX(Snowfall) FROM Past_weather WHERE City_id = ID) && City_id = ID;
open c;
fetch c INTO Rdate, Rid, Record;

Insert into Weather_records(Date, City_id, Record, Record_type, Record_name)
    values(Rdate, Rid, Record, '18', 'H_snowfall');


    close c;
END //
DELIMITER ;

1 个答案:

答案 0 :(得分:0)

来自official docs

  

存储的程序可能包含多个游标声明,但每个都是   在给定块中声明的游标必须具有唯一的名称。

作为旁注;与您可能使用的INSERT-SELECT查询相比,游标的效率相对较低。实施例....

INSERT INTO `Weather_records`(`Date`, `City_id`, `Record`, `Record_type`, `Record_name`)
SELECT `Date`, `City_id`, `High_temp`, '1', 'HH_Temp'
FROM Past_weather 
WHERE High_temp = (
   SELECT MAX(High_temp) 
   FROM Past_weather 
   WHERE City_id = ID
   ) 
   && City_id = ID
;

...这取代了sql proc中的第一个游标插入... 和/但不会忽略多行是否来自select(来自多个行具有相同的行)最高温度。)