简体   繁体   English

为什么我的程序不起作用?

[英]Why doesn't my procedure work?

I can't figure out why :( 我不知道为什么:(

I have to update the table products when I call the the procedure with the parameters(reference_requested, quantity_requested) and "return" (I don't how to do it, i read about return and params in, out and inout, but i didn't understand very well) the value of units sold 当我使用参数(reference_requested,quantity_requested)和“ return”(我不怎么做)调用过程时,我必须更新表产品,我了解了return和params in,out和inout的信息,但是我没有(不太了解)所售商品的价值

delimiter &&
create procedure compra (@reference_requested varchar, @quantity_requested int)
begin
  if quantity >= quantity_requested then
    update products
    set quantity = quantity - quantity_requested
    where reference_requested = reference;
    return quantity_requested;
  else
    return quantity;
    update products
    set quantity = 0;
    where reference_requested = reference;
  end if;
end
&&

Edit: As was pointed out in the comments, you cannot return a value (or use a return statement) in a procedure. 编辑:正如注释中指出的那样,您不能在过程中返回值(或使用return语句)。 If you're looking to do that, then you should use "create function" and not "create procedure" and additionally specify a return data type. 如果要这样做,则应使用“创建函数”而不是“创建过程”,并另外指定返回数据类型。

In your "else" the "return quantity" statement should be last, right before "end if" or the update will not run. 在您的“其他”中,“退货数量”语句应为最后一个,紧接在“如果...结束”之前,否则更新将不会运行。

   ...
   create procedure compra(reference_requested varchar(50), quantity_requested int)
   ...
  • Does the statement: 该语句是否:
   ...
   where reference_requested = reference;
   ...

reference_requested refers to the parameter or column in the table?. reference_requestedreference_requested表中的参数还是列? Avoid the names of the parameters/variables are equal to the columns of the tables, this can lead to problems. 避免参数/变量的名称等于表的列,否则可能导致问题。

   ...
   return quantity;
   ...

is used in functions for stored procedure is not used. 用于存储过程的函数中不使用。

  1. To return a value from a stored procedure use an OUT parameter. 要从存储过程返回值,请使用OUT参数。
  2. Besides issues that have been already mentioned you can't just arbitrarily address quantity column in products table. 除了已经提到的问题之外,您还不能随意处理products表中的quantity列。 It should be used in a context of some statement (eg SELECT ). 应该在某些语句(例如SELECT )的上下文中使用。

Now a streamlined version of your procedure might look something like 现在,您的程序的简化版本可能看起来像

DELIMITER $$
CREATE PROCEDURE compra
(
  IN  _ref_requested VARCHAR(32), 
  IN  _qty_requested INT, 
  OUT _result INT
)
BEGIN
  DECLARE qty INT DEFAULT 0;

  SET qty = 
  (
    SELECT quantity
      FROM products
     WHERE reference = _ref_requested
     LIMIT 1
  );

  SET qty = IF(qty > _qty_requested, _qty_requested, qty);

  UPDATE products
     SET quantity = quantity - qty
   WHERE reference = _ref_requested;

  SET _result = qty;
END$$
DELIMITER ;

To be able to get the value of an OUT parameter you have to use a user variable 为了获得OUT参数的值,您必须使用用户变量

SET @result = NULL;
CALL compra('REF001', 25, @result);
SELECT @result;

Here is SQLFiddle demo 这是SQLFiddle演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM