存储过程 - 创建游标 - 接受 OUT 参数

发布于 2025-01-16 12:31:09 字数 1180 浏览 3 评论 0原文

我希望得到以下问题的一些帮助,不幸的是我创建的脚本不起作用。任何帮助将不胜感激!

问题: 编写一个脚本来创建一个名为 test 的存储过程。此存储过程应为结果集创建一个游标,该结果集由标价大于 700 美元的每种产品的product_name 和list_price 列组成。此结果集中的行应按标价降序排序。存储过程应接受 OUT 参数,其中消息从过程中传递出去。然后,该过程应将 out 参数设置为一个字符串变量,其中包括每个产品的产品名称和标价,因此它看起来像这样: Gibson SG2517.00|Gibson Les Paul1199.00| 这里,每个值都用星号 (*) 括起来,每列由逗号 (,) 分隔,每行由竖线字符 (|) 分隔。

我的脚本:

CREATE PROCEDURE test( OUT message VARCHAR(200) )
BEGIN
DECLARE product_name_var VARCHAR(50);
DECLARE list_price_var DECIMAL(9,2);
DECLARE row_not_found TINYINT DEFAULT FALSE;
DECLARE s_var VARCHAR(400) DEFAULT '';

DECLARE invoice_cursor CURSOR for
    SELECT 
        product_name,
        list_price
    FROM
        products
    WHERE
        list_price > 700
    ORDER BY list_price DESC;
    
DECLARE CONTINUE HANDLER FOR NOT FOUND
    SET row_not_found = TRUE;

OPEN invoice_cursor;

FETCH invoice_cursor INTO product_name_var, list_price_var;
WHILE row_not_found = FALSE DO

SET s_var = CONCAT(s_var,'*', product_name_var,'*,*',list_price_var,'*|');
    FETCH invoice_cursor INTO product_name_var, list_price_var;
END WHILE;

SELECT s_var AS message;
END

I was hoping to get some help the the below question, unfortunately the script I have created isn't working. Any assistance would be greatly appreciated!

Question:
Write a script that creates a stored procedure named test. This stored procedure should create a cursor for a result set that consists of the product_name and list_price columns for each product with a list price that’s greater than $700. The rows in this result set should be sorted in descending sequence by list price. The stored procedure should accept an OUT parameter where a message is passed out of the procedure. Then, the procedure should set the out parameter to a string variable that includes the product_name and list price for each product so it looks something like this:
Gibson SG,2517.00|Gibson Les Paul,1199.00|
Here, each value is enclosed in asterisk(*), each column is separated by a comma (,) and each row is separated by a pipe character (|).

My script:

CREATE PROCEDURE test( OUT message VARCHAR(200) )
BEGIN
DECLARE product_name_var VARCHAR(50);
DECLARE list_price_var DECIMAL(9,2);
DECLARE row_not_found TINYINT DEFAULT FALSE;
DECLARE s_var VARCHAR(400) DEFAULT '';

DECLARE invoice_cursor CURSOR for
    SELECT 
        product_name,
        list_price
    FROM
        products
    WHERE
        list_price > 700
    ORDER BY list_price DESC;
    
DECLARE CONTINUE HANDLER FOR NOT FOUND
    SET row_not_found = TRUE;

OPEN invoice_cursor;

FETCH invoice_cursor INTO product_name_var, list_price_var;
WHILE row_not_found = FALSE DO

SET s_var = CONCAT(s_var,'*', product_name_var,'*,*',list_price_var,'*|');
    FETCH invoice_cursor INTO product_name_var, list_price_var;
END WHILE;

SELECT s_var AS message;
END

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小女人ら 2025-01-23 12:31:09

选择 s_var AS 消息; - 消息被视为别名

使用

SET MESSAGE = s_var ;

https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=c43887fd68a17e5b1dc2093d10cd03ec

并注意空值...

SELECT s_var AS message; - message is seen as an alias

use

SET MESSAGE = s_var ;

https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=c43887fd68a17e5b1dc2093d10cd03ec

and beware nulls...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文