MySql 5.1.32: call another procedure within a stored procedure and set variable
I'm new at creating and working with stored procedures.
After spending several hours on trying, reading tutorials (and yes reading all the related questions at stackoverflow :-) ) I'm stuck.
This works fine:
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
SE开发者_StackOverflowLECT 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;
When called like:
Call GetAgent("Firefox 3.6.18", @AgentID);
It gives the proper response: "2"
So far so good. So let's get that into another procedure: (GetOS does the same thing, left out tot minimize reading :-)
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;
When called with
Call SetSessionInfo("Windows XP", "Firefox 3.6.18", 857264713, @SessionInfoID)
I get the answer "3" (proper response from GetOS), then the procedure stops and does not insert anything.
After installing Toad I saw the reason: an error: "FUNCTION GetAgent does not exist" Well, it is not a function, it's a procedure.
So basicly, my question: How do I call another procedure within a stored procedure and set a variable with the result?
This is why you are getting "FUNCTION GetAgent does not exist" error:
INSERT INTO tbl_session_info(session, agent_id, os_id)
VALUES (SessionID, GetAgent(AgentName, @AgentID), GetOs(OsName , @OsID));
You are trying to call GetAgent as a function (while it is a procedure). But you have already got Agent and OS IDs into variables. Just use them:
INSERT INTO tbl_session_info(session, agent_id, os_id)
VALUES (SessionID, NW_AgentID, NW_OSID);
精彩评论