简体   繁体   中英

Magento: Stored Procedures in SQL Upgrade Scripts

I've been trying to put a stored procedure creation step into my SQL upgrade script in Magento. like

$this->startSetup();
$sql = <<< __SQLPRC
CREATE PROCEDURE my_proc(
    IN length int
    ,IN column_used_for varchar(50)
    ,OUT return_id bigint
)
BEGIN
    DECLARE count_unused INT;
    SET count_unused = 0;
    select 
            id into return_id
        from 
            my_table
        where 
            used_status=0
            limit 1;

    if length(return_id) = length 
    then
        update my_table
            set 
                used_status=1
            where 
                id = return_id;
    else
        set return_id = 0;
    end if;
END;
__SQLPRC;

try {
$this->run($sql);
}
catch(Exception $e)
{ echo "<pre>" . $e->getTraceAsString(); }

$this->endSetup();

What I've concluded from my debugging is that Magento just grabs the SQL till first semi-colon " ; " Can somebody help me with this?

You have put the Stored Procedure code as:-

$this->startSetup();
$sql = <<< __SQLPRC
...

This needs to be as:-

$this->startSetup();
$sql = <<<__SQLPRC
...

This should work now, as there shouldn't be any space after the " <<< " according to the Heredoc Syntax of PHP String Delimiting.

Hope it helps.


Updated Answer:-

Can you try using this code:-

$write = Mage::getSingleton('core/resource')->getConnection('core_write');
$write->exec($sql);

instead of:-

$this->run($sql);

I have seen some issues earlier regarding this on Magento.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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