2015-12-06 140 views
0

在這裏工作是我的程序INSERT INTO不PROCEDURE

CREATE PROCEDURE Transakcje() 
BEGIN 
DECLARE ile INT DEFAULT 1; 
DROP TEMPORARY TABLE IF EXISTS temp; 

CREATE TEMPORARY TABLE temp 
(
Ilosc int 
); 

WHILE ile <= 11 DO 
    INSERT INTO temp (Ilosc) VALUES (ile); 
    SET ile = ile + 1; 
END WHILE; 

INSERT INTO Sprzedaz (KlientId, ProduktNumer, Ilosc , Cena) 
SELECT k.KlientId, p.ProduktNumer, t.Ilosc, t.Ilosc*p.Cena AS Cena FROM klient AS k, produkt AS P, temp AS t; 

END // 
DELIMITER ; 

當我跑這一切看起來好,但INSERT INTO不起作用。當我單獨運行我的INSERT INTO時,它會執行我想要的操作。我不知道我做錯了什麼。

+0

你的存儲過程中有2個插入,你在說什麼插入?有沒有錯誤信息? – Shadow

+0

我的不好,我的意思是第二個INSERT INTO Sprzedaz。 – Slovvik

回答

0

變化produckt的別名從Pp(大寫小寫),像這樣:

INSERT INTO Sprzedaz (KlientId, ProduktNumer, Ilosc , Cena) 
SELECT k.KlientId, p.ProduktNumer, t.Ilosc, t.Ilosc*p.Cena AS Cena FROM klient AS k, produkt AS p, temp AS t; 

例子:

create table klient (klientid int); insert into klient values (1),(2); 
create table produkt (produktnumer int, cena int); insert into produkt values (1, 1), (2, 2); 
create table Sprzedaz(klientid int, produktnumer int, ilosc int, cena int); 
create table temp (ilosc int); insert into temp values (1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11); 

SELECT k.KlientId, p.produktnumer, t.Ilosc, t.Ilosc*p.Cena AS Cena FROM klient AS k, produkt AS P, temp AS t; 
ERROR 1054 (42S22): Unknown column 'p.produktnumer' in 'field list' 

SELECT k.KlientId, p.produktnumer, t.Ilosc, t.Ilosc*p.Cena AS Cena FROM klient AS k, produkt AS p, temp AS t; 
-- getting good results by changing alias from capital P to lower case p 

INSERT INTO Sprzedaz (KlientId, ProduktNumer, Ilosc , Cena) 
SELECT k.KlientId, p.ProduktNumer, t.Ilosc, t.Ilosc*p.Cena AS Cena FROM klient AS k, produkt AS p, temp AS t; 
-- insert works great 

有了這個改變你的存儲過程應該正確地插入

+0

Thx工作正常。 – Slovvik