2014-09-11 99 views
0

在這段代碼中,它總是輸出NULL(儘管他們有數據,但沒有任何數據)。我試圖從2表(customer和order_status)獲取id_cust,lastName,addRess。誰能幫我?即時通訊新的SQL。爲什麼我總是在mySQL中輸出爲空?

delimiter // 
#drop procedure if exists GetOrderStatus 
create procedure GetOrderStatus(IN id_cust INT, 
           OUT lastName VARCHAR(45), 
           OUT o_number INT, 
           OUT addRess VARCHAR(45)) 
begin 
declare id_cust INT; 
declare lastName VARCHAR(45); 
declare addRess VARCHAR(45); 
declare ci_ty VARCHAR(45); 
declare dscrption VARCHAR(45); 
declare o_number INT; 
declare order_stts VARCHAR(45); 
declare total_item INT; 
declare reMarks VARCHAR(45); 

declare curs cursor for 
select c.c_id,o.order_no,o.o_status,o.item_total,c.description,o.remarks,c.lname 
from order_status o, customer c; 
open curs; 
fetch curs into id_cust,o_number,order_stts,total_item,dscrption,reMarks,lastName; 

select id_cust,lastName,addRess from customer 
where id_cust > 2 
and customer.id_cust = order_status.id_cust; 
end 

回答

0
create procedure GetOrderStatus(IN id_cust INT, 
           OUT olastName VARCHAR(45), 
           OUT out_number INT, 
           OUT oaddRess VARCHAR(45)) 
begin 

select c.c_id, c.lname, c.address 
INTO id_cust, olastName, oaddRess from customer AS c 
JOIN order_status AS o -- JOIN the customers and orders table together based on cust num 
ON c.id_cust = o.id_cust 
where c.c_id=id_cust; 
end 

我覺得你的路要走。根本不需要使用遊標,只需根據客戶ID在兩個表之間進行簡單的JOIN即可。我猜你在客戶表中有一個地址字段,這就是爲什麼你會看到@addRess=c.address
我猜這個函數應該根據訂單號得到訂單狀態。另外,對於輸出參數,不需要聲明單獨的變量來存儲它們。簡單地將它們存儲在輸出變量傳遞`@ nameOfOutputVariable = someValue中

讓我知道這是沒有意義的,

肖恩

+0

你爲什麼putthe字「輸出」在@address VARCHAR(45 )輸出?請給我解釋.. – 2014-09-11 04:53:02

+0

爲什麼sql說我在線1上有錯誤代碼1064,它是「創建過程GetOrderStatus(@id_cust INT,...」 – 2014-09-11 06:18:23

+0

對不起,我以爲你使用的是T_SQL,但是你正在使用MySQL。我已經適當地修改了SQL。 – 2014-09-11 13:45:44