无法在mysql中使用有效参数调用过程

wrrgggsh  于 2021-06-19  发布在  Mysql
关注(0)|答案(1)|浏览(420)

我编写了一个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美元/天。
如果天数大于30天,罚款金额为50美元/天,少于30天的罚款金额为5美元/天。
然后,状态将从 IR . 罚款只适用于身份为 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”

li9yvcax

li9yvcax1#

如果(stat=“i”)这个语句中的奇数引号似乎是问题所在-更改为单引号,过程就可以工作了。
如果存在借书人,则删除表格,罚款;创建表borrower(rollno int(11),name varchar(30),dataofissue date,
书名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)

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

相关问题