Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
存储过程在应该返回值时不返回任何值(MySQL)_Mysql_Stored Procedures - Fatal编程技术网

存储过程在应该返回值时不返回任何值(MySQL)

存储过程在应该返回值时不返回任何值(MySQL),mysql,stored-procedures,Mysql,Stored Procedures,我有一个存储过程,希望它返回一个值(lastinsertid)。它只返回0。 我的问题是下面的代码有什么问题 CREATE PROCEDURE my_stored_procedure( IN inCartId int ) BEGIN DECLARE orderId INT; -- Insert a new record into orders and obtain the new order ID INSERT INTO orders (created_on)

我有一个存储过程,希望它返回一个值(lastinsertid)。它只返回0。 我的问题是下面的代码有什么问题

CREATE PROCEDURE my_stored_procedure(
    IN inCartId int
)
BEGIN

    DECLARE orderId INT;

    -- Insert a new record into orders and obtain the new order ID
    INSERT INTO orders (created_on) VALUES (NOW());

    -- Obtain the new Order ID
    SELECT LAST_INSERT_ID() INTO orderId;

    -- Insert order details in order_detail table
    INSERT INTO order_detail (
        order_id, product_id, attributes, product_name, quantity, unit_cost
    )
    SELECT
        orderId,
        p.id,
        sc.attributes,
        p.name,
        sc.quantity,
        COALESCE( NULLIF( p.discounted_price, 0 ), p.price ) AS unit_cost
    FROM
        shopping_cart sc
        INNER JOIN products p ON sc.product_id = p.id
    WHERE
        sc.cart_id = inCartId
        AND
        sc.buy_now;

    -- Save the order's total amount
    UPDATE
        orders
    SET
        total_amount = (
            SELECT
                SUM( unit_cost * quantity )
            FROM
                order_detail
            WHERE
                order_id = orderId
        )
    WHERE
        id = orderId;

    -- Clear the shopping cart
    CALL shopping_cart_empty( inCartId );

END
  • 在大多数数据库系统(如MySQL/MariaDB和MS SQL Server)中,存储过程不像函数那样具有“返回值”

  • 相反,存储过程通过输出参数将数据返回给调用者

    • 在MySQL中,
      函数
      确实返回值,但是
      函数
      与存储过程不同,最好在单独的问题中解释差异
  • (在Microsoft SQL Server和Sybase中,存储过程都返回一个
    int
    “return code”值,但该值表示状态(如可执行程序如何具有退出代码),并不用于从数据库返回数据。)

  • 要在MySQL和MariaDB中使用输出参数,请使用
    OUT
    关键字而不是
    in
    关键字,然后分配给该参数

在您的情况下,将局部变量
orderId
替换为我在存储过程参数列表中添加的
OUT newOrderId
参数。无需进行其他更改

CREATE PROCEDURE my_stored_procedure(
    IN inCartId int,
    OUT newOrderId int
)
BEGIN

    -- Insert a new record into orders and obtain the new order ID
    INSERT INTO orders (created_on) VALUES (NOW());

    -- Obtain the new Order ID
    SELECT LAST_INSERT_ID() INTO newOrderId;

    -- Insert order details in order_detail table
    INSERT INTO order_detail (
        order_id, product_id, attributes, product_name, quantity, unit_cost
    )
    SELECT
        orderId,
        p.id,
        sc.attributes,
        p.name,
        sc.quantity,
        COALESCE( NULLIF( p.discounted_price, 0 ), p.price ) AS unit_cost
    FROM
        shopping_cart sc
        INNER JOIN products p ON sc.product_id = p.id
    WHERE
        sc.cart_id = inCartId
        AND
        sc.buy_now;

    -- Save the order's total amount
    UPDATE
        orders
    SET
        total_amount = (
            SELECT
                SUM( unit_cost * quantity )
            FROM
                order_detail
            WHERE
                order_id = orderId
        )
    WHERE
        id = orderId;

    -- Clear the shopping cart
    CALL shopping_cart_empty(inCartId);

END

存储过程的返回值是其成功/失败条件的指示,不用于返回数据-为此,您应该使用输出参数。我如何做到这一点@dai您应该使用输出参数的信息应该为您提供足够的信息来使用Google MySQL存储过程输出参数和你自己努力想清楚,不是吗?