存储过程中的多个存储过程,仅返回第一个存储过程输出变量

时间:2013-12-11 18:27:07

标签: mysql stored-procedures

我是mysql的新手,我试图在存储过程中使用多个存储过程(有点像嵌套的存储过程)。我的结果输出为空集,任何帮助都将受到赞赏

我的存储过程代码块


Outer stored Procedure

DELIMITER $$
USE `test`$$

DROP PROCEDURE IF EXISTS `GetHsi`$$
CREATE DEFINER=`root`@`localhost` 
PROCEDURE `GetHsi`
(
IN subregion VARCHAR(255),
OUT inc DOUBLE,
OUT dcl DOUBLE
)
BEGIN
CALL GetSubrgnSntmt('I', subregion, @Inc_Sntmt);
CALL GetSubrgnSntmt('D', subregion, @Dec_Sntmt);

SELECT @Inc_Sntmt INTO inc; 
SELECT @Dcln_Sntmt INTO Dcl; 
SELECT inc, dcl; 

END$$
DELIMITER ;

inner stored Procedure

USE `test`$$
DROP PROCEDURE IF EXISTS `GetSubrgnSntmt`$$
CREATE DEFINER=`root`@`localhost` 
PROCEDURE `GetSubrgnSntmt`
(
IN Sntmt VARCHAR(25),
IN subregion VARCHAR(255),
OUT sub_Sntmt INT 
)
BEGIN

DECLARE Total INT DEFAULT 1;
DECLARE subtotal INT DEFAULT 1 ;
DECLARE sub_sntmt DOUBLE DEFAULT 1; 

SELECT COUNT(Cust_Id) INTO Total 
FROM test.`jnk_test`
WHERE buy_cty = subregion ;

SELECT COUNT(Cust_Id) INTO subtotal
FROM test.`jnk_test`
WHERE Sntmt_2 =Sntmt  
AND buy_cty = subregion ;

SET sub_Sntmt  =  (subtotal / total) * 100;
SELECT sub_sntmt; 

END$$
DELIMITER

Output

CALL gethsi('central hyderabad',@inc, @dcl);
SELECT @inc, @dcl;

===========
@inc| @dcl
===========
null| null

我对此感到震惊并且无法在这个时刻继续进行,我不确定我在哪里出错了,请帮助。

1 个答案:

答案 0 :(得分:0)

问题出在内部存储过程中。输出参数OUT sub_Sntmt INT也定义为局部变量DECLARE sub_sntmt DOUBLE DEFAULT 1;。一种选择是删除局部变量。

13.1.15. CREATE PROCEDURE and CREATE FUNCTION Syntax

  
      
  • 参数名称不区分大小写。
  •   

13.6.4.1. Local Variable DECLARE Syntax

  
      
  • 本地变量名称不区分大小写。
  •   

还要检查数据类型的定义以避免出现问题。

CREATE PROCEDURE `GetSubrgnSntmt`(
IN `Sntmt` VARCHAR(25),
IN `subregion` VARCHAR(255),
OUT `sub_Sntmt` INT
)
BEGIN
    DECLARE `Total` INT DEFAULT 1;
    DECLARE `subtotal` INT DEFAULT 1;
    -- DECLARE `sub_sntmt` DOUBLE DEFAULT 1;

    SELECT COUNT(`Cust_Id`) INTO `Total`
    FROM `jnk_test`
    WHERE `buy_cty` = `subregion`;

    SELECT COUNT(`Cust_Id`) INTO `subtotal`
    FROM `jnk_test`
    WHERE `Sntmt_2` = `Sntmt`
    AND `buy_cty` = `subregion`;

    SET `sub_Sntmt` := (`subtotal` / `total`) * 100;
END$$

DELIMITER ;

SQL Fiddle demo