我已经声明了一个名为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);