mysql - 存储过程 (mysql) 失败, "can' t 返回给定上下文中的结果集”

标签 mysql stored-procedures

我试图让这个 SP 在某些条件失败等情况下返回(离开)。

此代码验证并保存过程,但是当我调用过程时:

CALL ACH_Deposit(30027616,3300012003,200.00,"USD", "127.0.0.1")

失败并出现错误:“过程无法返回给定上下文中的结果集”

有人知道错误是什么吗?

过程代码:

CREATE DEFINER=`redpass_web_urs`@`%` PROCEDURE `ACH_Deposit`(
IN __Account_ID BIGINT,
IN __To_Bank_Account BIGINT,
IN __Amount DECIMAL(10,2),
IN __Currency CHAR(3),
IN __IP_Address VARCHAR(50)
)
COMMENT 'Makes a ACH deposit'
BEGIN

-- Declare Account Parameters
DECLARE _Account_Status INT;
DECLARE __Transaction_ID INT;
DECLARE _Account_Type INT DEFAULT 0;
DECLARE _Fee INT;

SELECT              
    Account_Status AS _Account_Status,
    Account_Type AS _Account_Type
FROM Account_Users 
WHERE Account_ID = __Account_ID;

main: BEGIN

    -- Step 1, is account active ?
    IF _Account_Status <> 1 THEN
        -- Account must be active (not restricted, closed etc)
        SELECT Response_Code, Description FROM ResponseCodes WHERE Response_Code = 106;
        LEAVE main; -- Here we die..
    END IF;

    -- Step 2, Calculate the FEE (Loading Funds with ACH)
    IF _Account_Type = 1 THEN
        -- Personal Account
        SET _Fee = (SELECT Fee_Template_Personal_1 FROM Fees WHERE Fee_Type_ID = __Fee_Type_ID);

    ELSE
        -- Business Account
        SET _Fee = (SELECT Fee_Template_Business_1 FROM Fees WHERE Fee_Type_ID = __Fee_Type_ID);

    END IF;

    -- Step 3, Check that Fee is not bigger then the actual amount
    IF _Fee > __Amount THEN

        SELECT Response_Code, Description FROM ResponseCodes WHERE Response_Code = 108;
        LEAVE main; -- Here we die..

    END IF;

    -- If we come here, we can make the transactions
    INSERT INTO Bank_Transaction
       (Bank_Account_ID
       ,Transaction_Type
       ,Amount
       ,IP_Address
       ,Pending)
     VALUES
       (__To_Bank_Account
       ,11
       ,__Amount
       ,__IP_Address
       ,1); -- Reserverade pengar

    -- Transaction ID
    SET __Transaction_ID = (SELECT LAST_INSERT_ID());

    -- Deduct the Fee
    INSERT INTO Bank_Transaction
           (Bank_Account_ID
           ,Transaction_Type
           ,Amount
           ,Fee_Type_ID
           ,Fee_Transaction_ID)
        VALUES
           (__To_Bank_Account
           ,4
           ,-__Fee
           ,21
           ,__Transaction_ID);



END main;

SELECT Response_Code, Description, __Transaction_ID AS Transaction_ID
FROM ResponseCodes 
WHERE Response_Code = 1;

END

最佳答案

要从存储过程中检索多个结果集,您应该使用支持多个查询的客户端。

如果您使用 PHP,请使用 MySQLi 扩展并使用 mysqli_multi_query 调用过程。

MySQL 扩展只能检索 proc 返回的第一个记录集。为了能够使用 ti,您应该将参数 $client_flags 中的 CLIENT_MULTI_RESULTS(十进制 131072)设置为 mysql_connect

关于mysql - 存储过程 (mysql) 失败, "can' t 返回给定上下文中的结果集”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5447930/

相关文章:

php - MySQL,需要帮助定义多个连接查询以返回完整信息

c# - 从 Entity Framework 返回自定义对象 <List T> 并分配给对象数据源

stored-procedures - 存储过程中输出参数的用途/好处

sql - 用于根据列类型插入默认值的存储过程

sql-server - 为什么 SSMS 会更改我的存储过程(重新格式化、将 exec 更改为 EXECUTE 等)

MySQL SELECT 从另一个表的数据开始?

javascript - .ajaxForm 将数据传递给 php

mysql - MySql 过程中的 WHILE 循环返回多个结果

SQL分组问题

mysql - 如何在 Docker Compose 中初始化 MySql 数据库