Mysql 存储过程sql语法错误

Mysql 存储过程sql语法错误,mysql,Mysql,数据库中有5个表 ApartamentClass(idclass,descript) Room(idroom,idclass,beds,isfree) ItemPrice(iditem,idclass,description,price) Client(idclient,fio) Active(idcontract,idclient,idroom,days,price,idclass) 我需要创建一个存储过程,检查是否存在具有特定类别(param1)和床位数(param2)的免费房间,并为该房

数据库中有5个表

ApartamentClass(idclass,descript)
Room(idroom,idclass,beds,isfree)
ItemPrice(iditem,idclass,description,price)
Client(idclient,fio)
Active(idcontract,idclient,idroom,days,price,idclass)
我需要创建一个存储过程,检查是否存在具有特定类别
(param1)
和床位数
(param2)
的免费房间,并为该房间和客户(fio)
(param3)
创建几天的租赁合同
(param4)
。 价格取决于房间的等级(更高的等级->一张床和一天的更高价格)

但我总是发现SQL语法错误。我不知道问题出在哪里。

您的SQL语法有错误;检查与您的MySQL服务器版本对应的手册,以了解在“declare iclient int default 0”附近使用的正确语法;
从客户端将idclient选择到iclient

第20行的所有DECLARE语句必须位于BEGIN的开头。。。此处MySQL文档中所述的结束块:

仅允许在BEGIN中声明。。。结束复合语句,并且必须在其开始处,在任何其他语句之前

因此,您可能需要尝试以下代码:

create procedure UserRequest(
    in param1 int,
    in param2 int,
    in param3 varchar(100),
    in param4 int
)
begin   
    declare iroom int default 0;
    declare iclient int default 0;
    declare bedprice decimal default 0.0;
    declare dayprice decimal default 0.0;
    declare price decimal default 0.0;

    select idroom into iroom from room
    where beds = param2 and idclass = param1 and isfree = 0
    limit 1;


    if not (iroom=0) then
        update room set isfree = 1
        where idroom = iroom;
    end if;


    select idclient into iclient from client
    where fio = param3
    limit 1;

    select (param2 * price) into bedprice from itemprice
    where description = "bed" and idclass = param1;

    select (param4 * price) into dayprice from itemprice
    where description = "day" and idclass = param1;

    set price = bedprice + dayprice;


    insert into active(idclient,idroom,days,price,idclass)
    values(iclient,iroom,param4,price,param1);
end

是的,它有效!但为什么
声明的位置在MySQL中很重要呢?
create procedure UserRequest(
    in param1 int,
    in param2 int,
    in param3 varchar(100),
    in param4 int
)
begin   
    declare iroom int default 0;
    declare iclient int default 0;
    declare bedprice decimal default 0.0;
    declare dayprice decimal default 0.0;
    declare price decimal default 0.0;

    select idroom into iroom from room
    where beds = param2 and idclass = param1 and isfree = 0
    limit 1;


    if not (iroom=0) then
        update room set isfree = 1
        where idroom = iroom;
    end if;


    select idclient into iclient from client
    where fio = param3
    limit 1;

    select (param2 * price) into bedprice from itemprice
    where description = "bed" and idclass = param1;

    select (param4 * price) into dayprice from itemprice
    where description = "day" and idclass = param1;

    set price = bedprice + dayprice;


    insert into active(idclient,idroom,days,price,idclass)
    values(iclient,iroom,param4,price,param1);
end