MYSQL Calling stored procedures inside an SELECT CASE on a Trigger

痴心易碎 提交于 2019-12-25 01:58:01

问题


im stuck on calling stored procedures inside a SELECT CASE on a Trigger, it gaves me the following error:

[Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your   MySQL server version for the right syntax to use near 'empata(NEW.eqvis))
            WHEN 'loc' THEN pierde(NEW.eqvis)
            WHEN 'vis' THEN g' at line 16

Here is the code:

DELIMITER |
CREATE TRIGGER updpartido AFTER UPDATE ON partidos
FOR EACH ROW
    BEGIN
        SET @vgls = vgoles(NEW.eqvis);
        SET @lgls = vgoles(NEW.eqloc);
        SET @vglsec = vgolesec(NEW.eqvis);
        SET @lglsec = vgolesec(NEW.eqloc);
        SELECT CASE 
            WHEN @vgls=@lgls THEN "emp"
            WHEN @vgls>@lgls THEN "loc"
            WHEN @vgls<@lgls THEN "vis" 
        END
        INTO @st;

        SELECT CASE @st
            WHEN 'emp' THEN CALL empata(NEW.eqvis)
            WHEN 'loc' THEN CALL pierde(NEW.eqvis)
            WHEN 'vis' THEN CALL gana(NEW.eqvis)
        END
        INTO @dat;

        SELECT CASE @st
            WHEN 'emp' THEN CALL empata(NEW.eqloc)
            WHEN 'vis' THEN CALL pierde(NEW.eqloc)
            WHEN 'loc' THEN CALL gana(NEW.eqloc)
        END
        INTO @dat2;

        UPDATE equipos SET gf=@vgls,gc=@vglsec WHERE id=NEW.eqvis;
        UPDATE equipos SET gf=@lgls,gc=@lglsec WHERE id=NEW.eqloc;
    END;

|

But if i remove the "CALL" the Triggers adds but when i do some update it gives me the error of "FUNCTION not found" since i made them as stored procedures and not as functions because im not going to return nothing...

Any help is very appreciated!


回答1:


You can convert your procedures empata, pierde, and gana into functions and use as coded in the first example below:

-- works
SET @st = 'loc';
SELECT CASE @st
  WHEN 'loc' THEN function_(@st)
END
INTO @dat;
SELECT @dat;

I tested the following scenarios and they didn't work:

-- won't work
SET @st = 'loc';
IF @st = 'loc' THEN
    function_(@st);
END IF;

-- won't work
SET @st = 'loc';
SELECT CASE @st
  WHEN 'loc' THEN CALL stored_procedure_(@st)
END
INTO @dat;
SELECT @dat;

-- won't work
SET @st = 'loc';
IF @st = 'loc' THEN
    CALL stored_procedure_(@st);
END IF;

At the very least, they didn't work for me.




回答2:


I am going to adventure an answer even though I'm not a MySQL guy:

I don't think you can call stored procedures within a select statement whose output is stored in a variable; or more colloquially: You can't blow and suck at the same time. You either have have a select statement whose purpose is to return some records or you execute some kind of process on your data, but you can't have both intertwined.

If you separate the CALL statements I think it should work. You can do the check below the into @... and call the appropriate stored proc.



来源:https://stackoverflow.com/questions/7871260/mysql-calling-stored-procedures-inside-an-select-case-on-a-trigger

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!