Pergunta

When trying to run this in the SQL window in phpmyadmin the following error is returned..

SQL

DELIMITER $$

DROP FUNCTION IF EXISTS `stock_in_stock_ids` $$
CREATE DEFINER=`root`@`localhost` FUNCTION `stock_in_stock_ids`(_running_total_limit INT, _product_id INT, _block_id INT) RETURNS TEXT
BEGIN
    DECLARE done INT DEFAULT FALSE;
    DECLARE _running_qty INT DEFAULT 0;
    DECLARE _id INT;
    DECLARE _qty INT;
    DECLARE _ids TEXT DEFAULT NULL;

    DECLARE _cur CURSOR FOR
        SELECT id, qty
        FROM stock
        WHERE block_id=_block_id && type=2 && product_id=_product_id
        ORDER BY time DESC, id DESC;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

    OPEN _cur;

    read_loop: LOOP
        FETCH _cur INTO _id, _qty;

        IF done THEN
            SET _ids = '';
            LEAVE read_loop;
        END IF;

        SET _running_qty = _running_qty + _qty;
        SET _ids = CONCAT_WS(',', _ids, _id);

        IF _running_qty >= _running_total_limit THEN
            LEAVE read_loop;
        END IF;
    END LOOP read_loop;

    CLOSE _cur;

    RETURN _ids;
END $$

DELIMITER ;

error

#1418 - This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary logging is enabled (you *might* want to use the less safe log_bin_trust_function_creators variable) 

update (working solution)

SELECT qty, price_cost, @running_total:=@total running_total, @total:=@total+qty total
FROM stock, (SELECT @running_total:=0, @total:=0) vars
WHERE block_id=101 && type=2 && product_id=20
HAVING running_total<=3000
ORDER BY time DESC, id DESC
Foi útil?

Solução

You want to use a stored procedure but are using a function. There's a difference, you know?

Also I think you don't need a procedure at all, let alone a cursor. Cursors are expensive as hell.

Does this get you what you want?

SELECT * FROM (
  SELECT 
  id, 
  qty,
  @running_total:=@running_total + qty as running_total
  FROM stock
  , (SELECT @running_total:=0) vars
  WHERE block_id=_block_id && type=2 && product_id=_product_id
  ORDER BY time DESC, id DESC
) sq 
WHERE running_total <= $yourRunningTotalLimit
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top