2017-04-10 76 views
-1

我試圖在一個過程中使用多個選擇,但我不斷收到第18行所述的錯誤,我對存儲過程有所瞭解,所以我可以使用一些幫助至於我做錯了什麼。在MySQL中使用多個選擇時出錯存儲過程

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 ; 
+0

做基本調試,刪除東西,直到開始工作,然後再開始添加東西。 –

+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處理... 和/但如果多行出來選擇的(多個不會忽略具有相同最高溫度的行)。

+0

好的謝謝,我想我可以重複使用相同的遊標名稱,如果我關閉它,然後重新分配它。但也感謝給我一個更好的寫作方法。 –

+0

@CaseyBridges我總是在procs中的任何「代碼」之前聲明我的所有遊標,總是使用處理程序,並且始終遍歷遊標結果;我不知道爲什麼,但是我甚至從未想過你可以稍後(根據需要)宣佈它們。 – Uueerdo

+0

使用'vs'有什麼區別? –