MySQL从另一个存储过程调用存储过程

时间:2009-11-26 09:15:35

标签: mysql stored-procedures

我正在处理一个调用另一个存储过程(子)的存储过程(父)。子proc每次都返回一行记录集。

我需要做的是从子proc中提取数据并在父proc中使用它。使用MSSQL中的方法我会假设我可以只填充临时表,但我不太清楚如何做到这一点。

非常感谢任何帮助。

这是我的proc的当前版本。

DELIMITER//

CREATE PROCEDURE CreateTransaction(IN p_TransType tinyint, in p_UserID INT)

BEGIN  

DROP TEMPORARY TABLE IF EXISTS fileData_tmp;

CREATE TEMPORARY TABLE fileData_tmp (t_FilePrefix varchar(5), t_FileSuffix int, t_FileDate varchar(4));

CALL GenerateFileNumber(p_TransType);


END//
DELIMITER;

2 个答案:

答案 0 :(得分:3)

当您想要返回多个值时,这将有效。

DROP TABLE IF EXISTS Filename;
CREATE TABLE Filename(
    id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
    name VARCHAR(255) NOT NULL,
    PRIMARY KEY (id)
);

INSERT INTO Filename(name) VALUES ('AAA'), ('BBB'), ('CCC'), ('DDD'), ('EEE');

DELIMITER //
DROP PROCEDURE IF EXISTS GenerateFileNumber//
CREATE PROCEDURE GenerateFileNumber(p_name VARCHAR(255))
COMMENT 'Gets the file number associated with the given name'
BEGIN
    DECLARE v_id BIGINT;

    DROP TABLE IF EXISTS ReturnValues;
    CREATE TEMPORARY TABLE ReturnValues (
        val1 BIGINT(20) UNSIGNED,
        str1 VARCHAR(255)
    );

    SELECT id INTO v_id FROM Filename WHERE name = p_name;
    INSERT INTO ReturnValues(val1, str1) VALUES (v_id, 'Moop');
END;
//

DROP PROCEDURE IF EXISTS TestCall//
CREATE PROCEDURE TestCall(p_name VARCHAR(255))
COMMENT 'Tests function call within a procedure'
BEGIN
    DECLARE v_id BIGINT;
    DECLARE v_str VARCHAR(255);

    CALL GenerateFileNumber(p_name);

    SELECT val1, str1 INTO v_id, v_str FROM ReturnValues;

    SELECT 'Returns: ' || COALESCE(v_id, 'none') || ', ' || v_str AS msg;
END;
//

DELIMITER ;

但是,一般情况下,应尽可能使用之前基于功能的答案。它跑得更快,你不必担心意外地破坏你的临时表。

mysql> call TestCall('AAA');
+------------------+
| msg              |
+------------------+
| Returns: 1, Moop |
+------------------+
1 row in set (0.27 sec)

Query OK, 0 rows affected (0.27 sec)

mysql> call TestCall('CCC');
+------------------+
| msg              |
+------------------+
| Returns: 3, Moop |
+------------------+
1 row in set (0.17 sec)

Query OK, 0 rows affected (0.17 sec)

答案 1 :(得分:2)

如果您尝试返回值,为什么不使用函数而不是过程?

DROP TABLE IF EXISTS Filename;
CREATE TABLE Filename(
    id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
    name VARCHAR(255) NOT NULL,
    PRIMARY KEY (id)
);

INSERT INTO Filename(name) VALUES ('AAA'), ('BBB'), ('CCC'), ('DDD'), ('EEE');

DELIMITER //
DROP FUNCTION IF EXISTS GenerateFileNumber//
CREATE FUNCTION GenerateFileNumber(p_name VARCHAR(255))
RETURNS BIGINT
COMMENT 'Gets the file number associated with the given name'
BEGIN
    DECLARE v_id BIGINT;

    SELECT id INTO v_id FROM Filename WHERE name = p_name;

    RETURN v_id;
END;
//

DROP PROCEDURE IF EXISTS TestCall//
CREATE PROCEDURE TestCall(p_name VARCHAR(255))
COMMENT 'Tests function call within a procedure'
BEGIN
    SELECT 'Returns: ' || COALESCE(GenerateFileNumber(p_name), 'none') AS msg;
END;
//

DELIMITER ;

以下是它的工作原理:

mysql> CALL TestCall('CCC');
+------------+
| msg        |
+------------+
| Returns: 3 |
+------------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

mysql> CALL TestCall('XXX');
+---------------+
| msg           |
+---------------+
| Returns: none |
+---------------+
1 row in set (0.00 sec)

Query OK, 0 rows affected, 1 warning (0.00 sec)