我编写了一个 PL/SQL block ,它需要执行以下任务:
已创建 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 美元/天。
然后,状态将从I
变为R
。
只有状态为 I
而不是 R
的人才会被罚款。
I
表示已发行图书。R
表示还书。
Fine
表中会存储相关信息,amt
为罚款总额,date为还书时的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")//
ERROR 1054 (42S22): Unknown column '“I”' in 'field list'
最佳答案
这个语句 if(stat=“I”) 中的奇数引号似乎是问题所在 - 更改为单引号并且 proc 工作。
如果存在 border 则删除表,fine;
创建表借款人(
滚动号 int(11) ,
名称 varchar(30),
发行日期数据,
nameofbook varchar(20),
状态变量(2)
);
插入借款人的值(value)观
( 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)
我还会检查列、声明的变量和参数是否具有相同的名称和反引号状态。
关于mysql - 无法在 MySQL 中使用有效参数调用过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52926373/