1

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
        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;

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?

4

1 回答 1

2

这就是您收到“FUNCTION GetAgent 不存在”错误的原因:

INSERT INTO tbl_session_info(session, agent_id, os_id)
  VALUES (SessionID, GetAgent(AgentName, @AgentID), GetOs(OsName , @OsID));

您正在尝试将 GetAgent 作为函数调用(虽然它是一个过程)。但是您已经将代理和操作系统 ID 放入变量中。只需使用它们:

INSERT INTO tbl_session_info(session, agent_id, os_id)
  VALUES (SessionID, NW_AgentID, NW_OSID);
于 2012-12-12T15:31:13.897 回答