【问题标题】:Writing script creates and calls a stored procedure编写脚本创建并调用存储过程
【发布时间】:2019-09-07 04:20:07
【问题描述】:

编写一个脚本来创建和调用名为 test 的存储过程。这个存储过程应该 声明一个变量并将其设置为 Products 表中所有产品的计数。如果计数更大 大于或等于 7,存储过程应显示一条消息:“产品数量 大于或等于 7”。否则,它应该说,“产品数量小于 7”。

DROP PROCEDURE IF EXISTS test;
CREATE  procedure test()
BEGIN
DECLARE count_of_7 DECIMAL(10,2);

SELECT  count(product_id) 
into count_of_7

FROM products;
IF count_of_7 >= 7 THEN

SELECT 'The number of products is greater than or equal to 7' AS message;
ELSE

SELECT 'The number of products is less than 7' AS message;
end if;
call test();

21:38:55 CREATE procedure test() BEGIN DECLARE count_of_7 DECIMAL(10,2) 错误代码:1064。您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以获取正确的语法,以便在第 3 行 0.016 秒的 '' 附近使用

【问题讨论】:

    标签: mysql mysql-workbench


    【解决方案1】:

    您需要在声明程序之前更改分隔符。此外,您还缺少 END 声明。这应该有效:

    DROP PROCEDURE IF EXISTS test;
    DELIMITER //
    CREATE  procedure test()
    BEGIN
        DECLARE count_of_7 DECIMAL(10,2);
    
        SELECT count(product_id) into count_of_7 FROM products;
        IF count_of_7 >= 7 THEN
            SELECT 'The number of products is greater than or equal to 7' AS message;
        ELSE
            SELECT 'The number of products is less than 7' AS message;
        END IF;
    END //
    DELIMITER ;
    call test();
    

    【讨论】:

      猜你喜欢
      • 2021-09-25
      • 2010-10-05
      • 2011-03-18
      • 2018-07-22
      • 1970-01-01
      • 2012-04-02
      • 2019-05-07
      • 2020-08-08
      • 2020-12-12
      相关资源
      最近更新 更多