I have a custom module and i want to define a Mysql Function in Setup/InstallSchema.php:
$query = 'DELIMITER $$
DROP FUNCTION IF EXISTS getQuantity;
CREATE FUNCTION getQuantity(PID INT) RETURNS INT
BEGIN DECLARE sum_qty INT;
SELECT sum(qty) INTO sum_qty FROM
(SELECT qty FROM cataloginventory_stock_item WHERE product_id = PID
UNION ALL
(SELECT IFNULL(sum(qty),0) as qty FROM cataloginventory_stock_item
WHERE product_id IN (SELECT product_id FROM catalog_product_super_link WHERE parent_id = PID))) qty;
RETURN sum_qty;
END;
$$ DELIMITER;';
$installer->getConnection()->query($query);
When run setup:upgrade, it throws an exception:
Multiple queries can't be executed. Run a single query and try again.
How can I fix it? The function \Magento\Framework\DB\Adapter\Pdo\MySql::multiQuery() is deprecated and I don't want to use it.