创建存储过程调试



使用此站点中的数据和架构:使用此站点的数据:https://www.sqlservertutorial.net/sql-server-sample-database/

我得到提示:

创建一个名为placeOrder((的存储过程,可以调用该过程在数据库中插入新订单。它将接收一个customerId作为INT,一个productId作为INT,一个qty作为INT并返回(作为输出参数(表订单中创建的新行的order_id。

此存储过程将找到该特定产品库存最多的商店,并将该商店分配给订单。order_status应设置为1(即Pending(,当前系统日期(见函数CURDATE(将分配给order_date,列required_date将从当前系统日期起7天(见函数ADDDATE(,列staff_id将分配给在所选存储中工作的任何人(根据以前的要求(。由于order_id列不是一个自动递增的列,您需要计算它的值。您可以使用max(order_id(来找出表中最高的order_id。

order_item行应设置为传递给存储过程的productId和数量。item_id应设置为1(因为此订单将只有一个项目(。应使用传递的productId从products表中检索标价。折扣值应设置为0

如果我正确理解了提示,我需要首先找到拥有最多特定产品的商店id。一旦我有了这个存储,我就需要在表"orders"中插入一行新的数据,其中的基本数据是order_id、customer_id、order_status、order_date、required、date和staff_id我不明白问题的最后一部分在问什么/如何解决

这是我目前的代码,但我几乎可以肯定,它充满了错误、注释和缺失的部分,所以请尽可能帮助我:

DELIMITER //
CREATE procedure placeOrder (IN customerID INT, IN productID INT, IN QTY INT, OUT order_id INT)
BEGIN
DECLARE customerID INT;
DECLARE produtcID INT;
DECLARE quantity INT;
SELECT customer_id INTO customerID from customers where customer_id = customerID;
SELECT product_id INTO productID from  order_items where product_id = productID;
SELECT quantity INTO qty from order_items where quantity = qty; 
/**find store id with max stocks of product**/
select st.store_name, sk.store_id from stocks as sk
INNER JOIN
stores as st
ON sk.store_id = st.store_id
WHERE max(sk.quantity)
GROUP by sk.product_id;
select st.store_id from stores as st
INNER JOIN orders as o
ON st.store_id= o.store_id
Insert into orders (order_id, customer_id, order_status, order_date, required_date, staff_id)
WHERE order_status = '1',
AND order_date = select(curdate()),
AND required_date = adddate('order_date' +7),
AND staff_id = /**ANYONE from store listed in precious query (how do I relate these two queries)**

END
  1. 不要重新声明函数参数
  2. 您不需要使用SELECT查询来设置已在参数中设置的变量
  3. 你没有得到正确的最大数量的商店。使用ORDER BY sk.quantity DESC LIMIT 1
  4. 您需要在查询中使用INTO <variable>来设置查询中的变量。如果不这样做,查询的结果将变成过程的结果,这在这里是不需要的
  5. INSERT语句中不使用WHEREWHERE用于查找符合条件的现有行,但INSERT用于创建新行。使用VALUES()可以列出应分配给指定列的所有值
CREATE procedure placeOrder (IN customerID INT, IN productID INT, IN QTY INT, OUT orderId INT)
BEGIN
DECLARE topStoreId INT;
DECLARE staffId INT;
/**find store id with max stocks of product**/
select sk.store_id 
from stocks as sk
INNER JOIN stores as st
ON sk.store_id = st.store_id
WHERE sk.product_id = productID
ORDER BY sk.quantity DESC
LIMIT 1
INTO topStoreId;
/* Pick an arbitrary staff from the selected store */
SELECT staff_id
FROM staffs
WHERE store_id = topStoreId
LIMIT 1
INTO staffId;
SELECT MAX(order_id)
FROM orders AS o
INTO orderId;
Insert into orders (order_id, customer_id, order_status, order_date, required_date, staff_id, store_id)
VALUES (orderId, customerId, 1, CURDATE(), DATE_ADD(CURDATE(), INTERVAL 1 WEEK), staffId, topStoreId);
END

最新更新