无法在MySQL

时间:2018-10-22 09:42:23

标签: mysql stored-procedures

我已经编写了一个PL / SQL块,它需要执行以下任务:

已创建2个表:

1)借款人:

+-------------+-------------+------+-----+---------+-------+
| Field       | Type        | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| rollno      | int(11)     | NO   | PRI | NULL    |       |
| name        | varchar(30) | YES  |     | NULL    |       |
| dataofissue | date        | YES  |     | NULL    |       |
| nameofbook  | varchar(20) | YES  |     | NULL    |       |
| status      | varchar(2)  | YES  |     | NULL    |       |
+-------------+-------------+------+-----+---------+-------+

它的内容是:

+--------+------+-------------+------------+--------+
| rollno | name | dataofissue | nameofbook | status |
+--------+------+-------------+------------+--------+
|      1 | a    | 2018-09-10  | Ba         | I      |
|      2 | b    | 2018-09-10  | Bb         | I      |
|      3 | c    | 2018-09-01  | Cc         | R      |
|      4 | d    | 2018-08-01  | Dd         | I      |
|      5 | e    | 2018-09-21  | Ee         | I      |
|      6 | f    | 2018-09-18  | Ff         | I      |
+--------+------+-------------+------------+--------+

2)好-没有数据。但是,其架构为:

+--------+---------+------+-----+---------+-------+
| Field  | Type    | Null | Key | Default | Extra |
+--------+---------+------+-----+---------+-------+
| rollno | int(11) | NO   | PRI | NULL    |       |
| days   | int(11) | NO   |     | NULL    |       |
| amt    | int(11) | NO   |     | NULL    |       |
+--------+---------+------+-----+---------+-------+

检查编号。 Borrower表中的天数(从签发日期到当前日期):

  • 如果天数在15到30之间,则罚款金额为每天5美元
  • 否。的 days> 30 ,则罚款金额为 $ 50 /天,而少于30天的罚款金额为$ 5 /天

然后,状态将从I变为R。 只会针对状态为I而不是R的用户计算罚款。

  • I表示已发行。
  • R表示退回的书。

相关信息将存储在Fine表中,其中amt为罚款总额,归还图书时的日期为curdate()。

这是我编写的过程,但是无法使用有效的参数调用该过程。

mysql> Delimiter //
mysql> Create  procedure proce(in roll int, in bname varchar(20))
    -> Begin
    -> Declare notfound int default 0;
    -> Declare rno int default 0;
    -> Declare name varchar(20);
    -> Declare doi date;
    -> Declare nob varchar(20);
    -> Declare stat varchar(20);
    -> Declare diff int default 0;
    -> Declare fine  int default 0;
    -> Declare cursor_name cursor for select rollno, name, dataofissue, nameofbook, status from borrower;
    -> Declare continue handler for NOT FOUND set notfound=1;
    -> Open cursor_name;
    -> loop_1 : LOOP
    -> fetch cursor_name into rno, name, doi, nob, stat ;
    -> If notfound=1 then
    -> leave loop_1;
    -> end if;
    -> 
    -> If(rno=roll and nob=bname) then
    -> 
    -> Select datediff(curdate(),doi) as DAYS into diff;
    -> if(stat=“I”) then
    -> if(diff >=15  and diff <=30 ) then
    -> set fine=( diff-15 )*5;
    -> elseif( diff > 30 ) then 
    -> set fine=(diff-30)*50 + 15*5;
    -> end if;
    -> insert into fine values(rno,diff,fine);
    -> update borrower set status='D' where rollno=rno; 
    -> end if;
    -> 
    -> 
    -> End if;
    -> 
    -> End LOOP loop_1;
    -> Close cursor_name;
    -> End;//
Query OK, 0 rows affected (0.03 sec)

我在这里称呼它

mysql> call proce(1,"Ba")//
  

错误1054(42S22):“字段列表”中的未知列“ I”

1 个答案:

答案 0 :(得分:1)

此语句if(stat =“ I”)中的单引号似乎是问题所在-更改为单引号,然后proc起作用。

删除表,如果存在借用者,则罚款;     创建表借款人(      rollno int(11),      名称varchar(30),      发行日期,
     书名varchar(20),      状态varchar(2)
     );     插入借款人的价值     (1,'a','2018-09-10','Ba','I'),
    (2,'b','2018-09-10','Bb','I'),
    (3,'c','2018-09-01','Cc','R'),
    (4,'d','2018-08-01','Dd','I'),
    (5,'e','2018-09-21','Ee','I'),
    (6,'f','2018-09-18','Ff','I');

create table fine
( rollno  int(11),
 days    int(11), 
 amt     int(11));

drop procedure if exists proce;
 Delimiter //
 Create  procedure proce(in roll int, in bname varchar(20))
     Begin
     Declare notfound int default 0;
     Declare rno int default 0;
     Declare name varchar(20);
     Declare doi date;
     Declare nob varchar(20);
     Declare stat varchar(20);
     Declare diff int default 0;
     Declare fine  int default 0;
     Declare cursor_name cursor for select rollno, name, dataofissue, nameofbook, status from borrower;
     Declare continue handler for NOT FOUND set notfound=1;
     Open cursor_name;
     loop_1 : LOOP
     fetch cursor_name into rno, name, doi, nob, stat ;
     If notfound=1 then
     leave loop_1;
     end if;

     If(rno=roll and nob=bname) then

     Select datediff(curdate(),doi) as DAYS into diff;

      if(stat='I') then
        if(diff >=15  and diff <=30 ) then
            set fine=( diff-15 )*5;
        elseif( diff > 30 ) then 
        set fine=(diff-30)*50 + 15*5;
        end if;
     insert into fine values(rno,diff,fine);
     update borrower set status='D' where rollno=rno; 
     end if;


     End if;


     End LOOP loop_1;
     Close cursor_name;
     End //

call proce(1,'Ba');

select * from fine;

+--------+------+------+
| rollno | days | amt  |
+--------+------+------+
|      1 |   42 |  675 |
+--------+------+------+
1 row in set (0.00 sec)

我还将检查列,声明的变量和参数是否具有相同的名称和反引号状态。