在Laravel迁移中创建/使用存储过程

时间:2018-09-26 12:36:53

标签: mysql laravel stored-procedures eloquent laravel-migrations

这有点怪!

我建立了一个运行良好的存储过程。但是,当我将其放入Laravel(频繁的)迁移过程中时,该迁移过程会构建存储过程,但是当调用它时,会给我一个错误1292(截断了不正确的DOUBLE值)。然后,在mysql工作台上,我右键单击由迁移构建的存储过程,复制create语句,删除该存储过程,然后从其自己的create语句重建它。...AND .....一切正常!

我愿意接受所有猜测。我显然希望此存储过程成为我迁移的一部分,而不是需要手动构建它。预先感谢:

DELIMITER $$

CREATE PROCEDURE sp_cancelTasksOnApplicationCancel(IN loanAppId INT(20))

BEGIN

            SELECT loanAppId;

            #SET @taskids := 0;

            update tasks set task_status = 26, task_status_note = 'Application Cancelled or Denied' 

where loan_app_id = loanAppId AND task_status IN (22, 23, 24) AND ( SELECT @taskids := CONCAT_WS(',', id, @taskids) );        

#SELECT @taskids;

SET @cleanids = SUBSTRING(@taskids, 1, CHAR_LENGTH(@taskids) -1);

#SELECT @cleanids;

            SET @pos = 0;

            SET @len = 0;

            WHILE LOCATE(',', @cleanids, @pos+1) > 0 DO

                            SET @len = LOCATE(',', @cleanids,@pos+1) - @pos;

                            SET @id = SUBSTRING(@cleanids, @pos, @len);

                                            #select (@id);

                                            IF (@id <> 0) THEN

                                            insert into status_historys 

                                                            (loan_app_id, `type`, type_recid, type_keyfield,type_status_field, type_status_date_field, type_status_note_field, type_status_userid_field, `status`, status_date, status_note, status_userid, created_at, updated_at) 

            values(loanAppId, 'tasks', @id, 'id', 'task status', 'tasks status datetime', 'tasks status note', 'tasks status userid', 26, now(), 'Application Cancelled or Denied', 1, now(), now()); 

                                            END IF;

                            SET @pos = LOCATE(',', @taskids, @pos+@len) + 1;

            END WHILE;

END $$

DELIMITER ;
###############更新

问题出在以下两行。即使从命令行,它也会给出有关不正确的double值的警告。

SET @taskids := '0';

update tasks set task_status = 22, task_status_note = 'Just Testing' 
where loan_app_id = loanAppId AND task_status IN (22, 23, 24) AND ( SELECT 
@taskids := CONCAT_WS(',', cast(id as char), @taskids) );

1 个答案:

答案 0 :(得分:0)

OK-主要问题是update where子句中的select关键字。我相信它会继续声明@taskids用户变量,如果存储过程是从工作台构建的,mysql会发出警告,但是如果存储过程是由迁移构建的,则mysql将失败。

所以正确的代码是

#snip#
#Note string '0' rather than 0
SET @taskids := '0';

#note casting of id and removing the select keyword before the first @taskids
update tasks set task_status = 22, task_status_note = 'Just Testing' 
where loan_app_id = loanAppId AND task_status IN (22, 23, 24) AND (  
@taskids := CONCAT_WS(',', cast(id as char), @taskids) );

#snip#