2011-05-11 101 views
1
DELIMITER $$ 

DROP PROCEDURE IF EXISTS `pawn`.`simpleproc`$$ 
CREATE DEFINER=`root`@`localhost` PROCEDURE `pawn`.`simpleproc`(OUT param1 int, inout incr int) 
BEGIN 
declare incr Integer; 
    set incr= incr+1; 
    SELECT count(*) into param1 FROM pawnamount; 
END $$ 

這是我的代碼來創建一個存儲過程....它創造了.. 對於執行..存儲過程在MySQL不工作

call simpleproc(@param1,@incr); 
select @param1,@incr 

,結果將是空值。它是簡單的一個。我已經試過很多times.But,我得到空值僅..

回答

1
 
DECLARE incr INT;       -- incr is NULL here, add DEFAULT 0 if you want it to have a value 
SET incr = incr + 1       -- NULL + 1 is still NULL 
SELECT COUNT(*) INTO param1 FROM pawnamount; -- If the table pawnamount is empty, it generates an empty set, which in a parameter assignment becomes NULL. 
+0

COUNT(*)不會返回NULL。如果表格爲空,它將返回0。 – 2011-05-11 14:58:39

1

自定義incrINOUT paramater,你不應該再在你的程序的身體聲明它。這樣,只要在傳遞到過程之前進行初始化,就可以正確地增加它。

下面的代碼:

DELIMITER $$ 

DROP PROCEDURE IF EXISTS `pawn`.`simpleproc`$$ 
CREATE DEFINER=`root`@`localhost` PROCEDURE `pawn`.`simpleproc`(OUT param1 int, inout incr int) 
BEGIN 
    set incr= incr+1; 
    SELECT count(*) into param1 FROM pawnamount; 
END $$ 

DELIMITER ; 

set @incr = 0; 
call simpleproc(@param1,@incr); 
select @param1,@incr;