当试图在 phpmyadmin 的 SQL 窗口中运行它时,会返回以下错误。
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 ;
错误
#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)
更新(工作解决方案)
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