1

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
4

1 に答える 1

1

ストアド プロシージャを使用したいが、関数を使用している。違いがありますよね?

また、カーソルは言うまでもなく、手順はまったく必要ないと思います。カーソルは非常に高価です。

これはあなたが望むものを手に入れますか?

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
于 2013-04-13T14:11:48.870 に答える