0

我已经声明了一个名为的变量order_id,我想存储为我尝试插入的订单设置的自动递增 id。我在OUTPUT.

我已经阅读了很多示例,并且我所看到的,这是编码它的正确方法。所有代码都在存储过程中。插入订单和其余程序都可以,除了我需要自动递增的 id 来插入另一个表。

CREATE DEFINER=`name`@`%` PROCEDURE `conduct_order`(
    IN username_par VARCHAR(50), 
    IN addr_par VARCHAR(100),
    IN zip_par VARCHAR(45))
BEGIN
    DECLARE user_addr VARCHAR(100);
    DECLARE user_zip VARCHAR(45);
    DECLARE user_country VARCHAR(100);
    DECLARE user_phone VARCHAR(45);
    DECLARE user_email VARCHAR(45);
    DECLARE this_date VARCHAR(45);
    DECLARE temp INT;


    SET user_id = (SELECT id FROM customers WHERE username= username_par);
    SET user_addr = addr_par;
    SET user_zip = zip_par;
    SET user_country = (SELECT country FROM customers WHERE username=username_par);
    SET user_phone = (SELECT phone FROM customers WHERE username=username_par);
    SET user_email = (SELECT email FROM customers WHERE username=username_par);
    SET this_date = (SELECT CURDATE());

    INSERT INTO orders(
                    customer_id, 
                    customer_addr,
                    customer_phone, 
                    customer_email, 
                    order_date, 
                    order_state, 
                    customer_postalcode, 
                    customer_country)
            OUTPUT order_id INTO temp
            VALUES( 
                    user_id,
                    user_addr,
                    user_phone,
                    user_email,
                    this_date,
                    'not sent',
                    user_zip,
                    user_country);

    INSERT INTO order_items(order_id, product_id, amount)
    VALUES(
        (SELECT id FROM orders 
        WHERE   customer_id=user_id 
        AND     order_date=this_date
        AND     customer_addr=user_addr
        AND     customer_postalcode=user_zip
        AND     customer_country=user_country),
        (SELECT product_id FROM carts WHERE customer_id=user_id),
        (SELECT amount FROM carts WHERE customer_id=user_id));

END

MySQL工作台说

语法错误:意外的“输出”(标识符)

我不知道出了什么问题。

编辑:我在 stackoverflow 上发现了另一个问题,该问题通过使用表变量解决了类似的问题,但我似乎无法声明表变量。Wordbench 说

TABLE 此时不是有效的输入。

这是我尝试 `DECLARE MyTableVar TABLE (IdentityColumn BIGINT);

4

0 回答 0