2013-10-11 27 views
0

我有一個名爲Percentages with a Column的表稱爲5StarResults,我想用存儲過程名稱5star的結果填充此表。將存儲過程的結果插入現有表

當我使用'Call 5star'時,它只返回一個百分比,我想把這個百分比插入Percentages表中。

請你幫忙,我試圖編輯程序來包含一個Insert,但它總是返回說0行插入。

謝謝

編輯,

存儲過程如下

BEGIN 
    declare Rating5Total int default 5; 
    declare Rating5Win int default 5; 
    declare 5StarPerformance decimal; 
    set Rating5Total = (select COUNT(Ratings) from vHighPerformance where Ratings = 7); 
    set Rating5Win = (select COUNT(Results) from vHighPerformance where Ratings = 7 and Results = 1); 
    set 5StarPerformance = Rating5Win*100.0/Rating5Total; 
    Select 5StarPerformance; 
END 
+0

您可以發佈存儲過程'5star'的代碼? – wchiquito

+0

你將需要使用臨時表,讓我試着嘲弄一下 – CSharper

回答

0

如果某個選項是做在同一個存儲過程的INSERT,下面的代碼應該工作:

CREATE PROCEDURE `5star`() 
BEGIN 
    DECLARE Rating5Total INT DEFAULT 5; 
    DECLARE Rating5Win INT DEFAULT 5; 
    DECLARE 5StarPerformance DECIMAL(18, 3); 
    SET Rating5Total := (SELECT COUNT(Ratings) FROM vHighPerformance WHERE Ratings = 7); 
    SET Rating5Win := (SELECT COUNT(Results) FROM vHighPerformance WHERE Ratings = 7 AND Results = 1); 
    SET 5StarPerformance := Rating5Win*100.0/Rating5Total; 
    /* INSERT INTO percentages (percentage) VALUES (5StarPerformance); */ 
    INSERT INTO percentages (id, percentage) VALUES (1, 5StarPerformance) 
     ON DUPLICATE KEY UPDATE percentage = 5StarPerformance; 
    SELECT 5StarPerformance; 
END$$ 

一個例子可以在SQL Fiddle中看到。

UPDATE

如果您需要在表percentages一個單一的記錄,你可以做一個UPSERT。查看更新的代碼。

+0

完美,謝謝:-) – user2785062

+0

是否有可能使用UPDATE,因爲每次運行它都會添加一個新行。謝謝 – user2785062

+0

請參閱更新的代碼。 – wchiquito