两个表借款人(rollno,名称,bookissue_date)和Fine(rollno,名称,金额)
delimiter //
create procedure student( in roll_no int,in Nameofbook varchar(40))
begin
declare Dateofiss1 date;
Declare cur cursor for
select Dateofiss from Borrower where Roll_no = roll into Dateofiss1;
OPEN cur;
fetch cur into Dateofiss1
if(datediff(sysdate(),Dateofiss1)<15) then varchar(20))
update Borrower set status='R'where Roll_no=roll_no
elseif(datediff(sysdate(),Dateofiss1)>=15)and datediff (sysdate(),Dateofiss1<30)
SET FINEAMOUNT=5*(datediff(sysdate(),Dateofiss1)-15)
insert into Fine(Roll_no,Date,amount)values(rollno,sysdate,fineamount);
update.borrower set status='R' where Roll_no='rollno';
elseif (datediff(sysdate(),Dateofiss1)>30)
SET FINEAMOUNT=50*(datediff(sysdate(),Dateofiss1)-15)
insert into Fine(Roll_no,Date,amount)values(rollno,sysdate,fineamount);
update.borrower set status='R' where Roll_no='rollno';
close cur;
end if
select * from Borrower;
elect * from Fine;
end
答案 0 :(得分:0)
您有许多语法错误。
varchar(20))
语句中有一个无关的if
。THEN
语句中缺少ELSEIF
。update.borrower
而不是update borrower
。roll_no
语句的引号中有update
。roll_no
参数与表列相同,因为列名不区分大小写。因此,条件where Roll_no = roll_no
将匹配每一行。为参数指定其他名称。SELECT
中,INTO
子句位于FROM
之后,而不是末尾。SELECT INTO
,则无需使用游标。只需执行查询,它将设置变量。您还可以通过将日期差放入变量中来简化代码,因此不必重复计算。在ELSEIF
中,您不需要测试>= 15
,因为只有在< 15
测试失败的情况下,您才能到达那里。
UPDATE
语句在所有情况下都是相同的,因此根本不需要放在IF
中。
delimiter //
create procedure student( in p_roll_no int,in Nameofbook varchar(40))
begin
declare Dateofiss1 date;
declare diff INT;
select Dateofiss from Borrower into Dateofiss1 where Roll_no = p_roll_no;
OPEN cur;
SET diff = datediff(sysdate(),Dateofiss1)
IF diff BETWEEN 15 AND 29 THEN
SET FINEAMOUNT= 5 * (diff - 15)
insert into Fine(Roll_no,Date,amount)values(rollno,sysdate,fineamount);
else
SET FINEAMOUNT= 50 * (diff - 15)
insert into Fine(Roll_no,Date,amount)values(rollno,sysdate,fineamount);
end if
update Borrower set status='R'where Roll_no=p_roll_no
select * from Borrower;
select * from Fine;
end