Write a script that creates and calls a stored procedure named test. This stored procedure should declare a variable and set it to the count of all products in the Products table. If the count is greater than or equal to 7, the stored procedure should display a message that says, “The number of products is greater than or equal to 7”. Otherwise, it should say, “The number of products is less than 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) Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 3 0.016 sec
答案 0 :(得分:0)
You need to change the delimiter before declaring your procedure. Also, you are missing an END
statement. This should work:
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();