存储过程的参数有三种

    • IN:输入参数,也是默认模式。表示该参数的值必须在调用存储过程时指定,在存储过程中修改该参数的值不能被返回。
    • OUT输出参数。该值可在存储过程内部被改变,并可返回。
    • INOUT输入输出参数。调用时指定,并且可被改变和返回。
    1. use library;
    2. drop procedure if exists P_LoanBook;
    3. delimiter //
    4. create procedure P_LoanBook(vloanno varchar(16),vbookno char(9),out vflag tinyint)
    5. begin
    6. -- 调用其他存储过程
    7. call P_IsLoan(vloanno,vbookno,vflag);
    8. -- if语句
    9. if(vflag=0) then
    10. insert into Loan value(vbookno,vloanno,now());
    11. update Books set bstatus = 1 where bookNo = vbookno;
    12. elseif(vflag=1) then
    13. update Books set bstatus = 3 where bookNo = vbookno;
    14. end if;
    15. end//
    16. delimiter ;
    1. drop procedure if exists `GetCustomerLevel`;
    2. delimiter //
    3. create procedure GetCustomerLevel(in customerNumber int(11),out p_customerLevel varchar(10))
    4. Begin
    5. declare money int(10) default 0;
    6. select `creditlimit` into money from `customers`
    7. where customerNumber = customers.`customerNumber`;
    8. -- 方法1: 使用if
    9. if(money>10000) then
    10. set p_customerLevel = 'PLATINUM';
    11. elseif(money>=5000&&money<=10000) then
    12. set p_customerLevel = 'GOLD';
    13. elseif(money<5000) then
    14. set p_customerLevel = 'SILVER';
    15. END IF;
    16. -- 方法2: 使用case
    17. case
    18. when (money > 10000) then
    19. set p_customerLevel = 'PLATINUM';
    20. when (money>=5000&&money<=10000) then
    21. set p_customerLevel = 'GOLD';
    22. when (money<5000) then
    23. set p_customerLevel = 'SILVER';
    24. end case;
    25. select customerNumber,p_customerLevel from customers
    26. where customerNumber = customers.`customerNumber`;
    27. End //
    28. delimiter ;