Note that there are some explanatory texts on larger screens.

plurals
  1. POMySql 5.1.32: call another procedure within a stored procedure and set variable
    primarykey
    data
    text
    <p>I'm new at creating and working with stored procedures.</p> <p>After spending several hours on trying, reading tutorials (and yes reading all the related questions at stackoverflow :-) ) I'm stuck.</p> <p>This works fine:</p> <pre><code>PROCEDURE GetAgent(IN AgentName VARCHAR(50), OUT AgentID SMALLINT(6)) BEGIN IF EXISTS (SELECT id FROM tbl_lookup WHERE value = AgentName AND cat = 'agent') THEN SELECT id FROM tbl_lookup WHERE value = AgentName AND cat = 'agent'; ELSE INSERT INTO tbl_lookup(cat, value) VALUES ('agent', AgentName); SELECT id FROM tbl_lookup WHERE value = AgentName AND cat = 'agent'; END IF; END; </code></pre> <p>When called like:</p> <pre><code>Call GetAgent("Firefox 3.6.18", @AgentID); </code></pre> <p>It gives the proper response: "2"</p> <p>So far so good. So let's get that into another procedure: (GetOS does the same thing, left out tot minimize reading :-)</p> <pre><code>PROCEDURE SetSessionInfo(IN OsName VARCHAR(50), IN AgentName VARCHAR(50), IN SessionID BIGINT(30), OUT SessionInfoID SMALLINT(6)) BEGIN DECLARE nw_AgentID SMALLINT; DECLARE nw_OSID SMALLINT; CALL GetOs(OsName, @OsID); SET NW_OSID = @OSID; CALL GetAgent(AgentName, @AgentID); SET NW_AgentID = @AgentID; IF EXISTS (SELECT id FROM tbl_session_info WHERE session = SessionID) THEN SELECT id AS SessionInfoID FROM tbl_session_info WHERE session = SessionID; ELSE INSERT INTO tbl_session_info(session, agent_id, os_id) VALUES (SessionID, GetAgent(AgentName, @AgentID), GetOs(OsName , @OsID)); SELECT id AS SessionInfoID FROM tbl_session_info WHERE session = SessionID; END IF; END; </code></pre> <p>When called with</p> <pre><code>Call SetSessionInfo("Windows XP", "Firefox 3.6.18", 857264713, @SessionInfoID) </code></pre> <p>I get the answer "3" (proper response from GetOS), then the procedure stops and does not insert anything.</p> <p>After installing Toad I saw the reason: an error: "FUNCTION GetAgent does not exist" Well, it is not a function, it's a procedure.</p> <p>So basicly, my question: <strong>How do I call another procedure within a stored procedure and set a variable with the result?</strong></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload