代码之家  ›  专栏  ›  技术社区  ›  asn

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

  •  1
  • asn  · 技术社区  · 6 年前

    我编写了一个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美元/天 .

    然后,状态将从 I R . 只有状态为的人才能计算罚款。 而不是 R .

    • 注明已发行的图书。
    • 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 回复  |  直到 6 年前
        1
  •  1
  •   P.Salmon    6 年前

    如果(stat=__i_)这句话中的奇数引号似乎是问题-更改为单引号,过程正常。

    如有借阅人,可直接删除表格,罚款; 创建表借用人( Rollno int(11), 姓名varchar(30) 出炉日期,
    书籍名称varchar(20) 状态变量(2)
    ; 插入借款人价值 (1,'A'、'2018-09-10'、'BA'、'I'),
    (2,'b'、'2018-09-10'、'b b'、'i'),
    (3,'C'、'2018-09-01'、'CC'、'R'),
    (4,'d'、'2018-08-01'、'd d'、'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)
    

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