MySQL - 使用嵌套语句的存储过程

时间:2015-04-09 23:07:04

标签: mysql stored-procedures parameters nested

新手到MySQL - 我在将参数输入到存储过程中的嵌套语句时遇到了麻烦。

以下3个语句在顺序执行时效果很好。

insert into rental(movie_id, membership_id, format, no_nights) 
values (3014, 10004, "dvd", 2);

update rental  
inner join movie_format on rental.movie_id = movie_format.movie_id  
set rental_cost = daily_rental_cost * no_nights  
where rental.movie_id = 3014 and rental.format = "dvd" and membership_id = 10004;

update rental  
set rental_cost_due = rental_cost  
where rental.movie_id = 3014 and rental.format = "dvd" and membership_id = 10004;      

但是,当我尝试使用带有一组参数的存储过程时,where子句会给出错误消息,例如ERROR 1054(42S22):未知列' movie_id_in'在' where子句'

delimiter //  
create procedure usp_rent_movie   
        (IN movie_id_in int, membership_id_in int, format_in varchar(15), no_nights_in int)  

    insert into rental(movie_id, membership_id, format, no_nights) 
    values (movie_id_in, membership_id_in, format_in, no_nights_in);  

    update rental  
    inner join movie_format on rental.movie_id = movie_format.movie_id  
    set rental_cost = daily_rental_cost * no_nights   
    where rental.movie_id = movie_id_in and rental.format = format_in and membership_id = membership_id_in;  

    update rental   
    set rental_cost_due = rental_cost  
    where rental.movie_id = movie_id_in and rental.format = format_in and membership_id = membership_id_in;   
end//   
delimiter ;

道歉,如果这是一个简单的错误,我已经在这几个小时了!

1 个答案:

答案 0 :(得分:0)

如果您的程序正文中有多个陈述,则需要使用BEGINEND

delimiter //  
create procedure usp_rent_movie   
        (IN movie_id_in int, membership_id_in int, format_in varchar(15), no_nights_in int)  
BEGIN
    insert into rental(movie_id, membership_id, format, no_nights) 
    values (movie_id_in, membership_id_in, format_in, no_nights_in);  

    update rental  
    inner join movie_format on rental.movie_id = movie_format.movie_id  
    set rental_cost = daily_rental_cost * no_nights   
    where rental.movie_id = movie_id_in and rental.format = format_in and membership_id = membership_id_in;  

    update rental   
    set rental_cost_due = rental_cost  
    where rental.movie_id = movie_id_in and rental.format = format_in and membership_id = membership_id_in; 
end//   
delimiter ;