如何在mysql中存储存储过程的返回值

时间:2012-02-24 11:49:39

标签: mysql stored-procedures

虽然Stack Overflow中有很多关于这个问题的问题,但是我无法正确解决这个问题,任何人都可以帮我解决这个问题,让我们说有两个存储过程,

CREATE PROCEDURE `procOne`(stName varchar(7),fullItemCode varchar(12), fullLedNo varchar(12))
BEGIN
set @x = concat('
SELECT ID, Uprice FROM ',stName,' 
where Uprice > 0 and FulItmCode="',fullItemCode,'" and FullLedgerNo = "',fullLedNo,'" order by Dat desc limit 1;
');
PREPARE stmt FROM @x;
    EXECUTE stmt;
    DEALLOCATE PREPARE stmt;
END

CREATE PROCEDURE `prcTwo`()
BEGIN

CREATE TEMPORARY TABLE tmpStore (
  ID int(10),
  Uprice decimal(18,2)
)

insert into tmpStore exec procOne(@param1, @param2) 

select * from tempStore; 

DROP TABLE tmpStore;

END

我试过这个,但在尝试保存prcTwo

时遇到以下错误
ERROR 1064: You have an error in your SQL syntax; check the manual that 
corresponds to your MySQL server version for the right syntax to use near
'insert into tmpStore exec procOne 'St_BFG','SCI019', 'BIO-JVS-30' output
sele' at line 9 SQL Statement: CREATE DEFINER=user1@localhost PROCEDURE 
prcTwo() BEGIN CREATE TEMPORARY TABLE tmpStore ( ID int(10), Uprice 
decimal(18,2) ) insert into tmpStore exec procOne 'St_BFG','SCI019', 
'BIO-JVS-30' output  select * from tempStore; DROP TABLE tmpStore; END

3 个答案:

答案 0 :(得分:7)

像这样使用out参数

CREATE PROCEDURE abc(IN id INTEGER, OUT text VARCHAR(255))
BEGIN 
    SET text = 'asd';
END

你可以像这样调用这个程序:

SET @outvar = '';
CALL abc(1, @outvar);

比'asd'将保存在@outvar

答案 1 :(得分:2)

程序不返回值,您必须将procOne声明为函数:

CREATE FUNCTION `procOne`(stName varchar(7),fullItemCode varchar(12), fullLedNo varchar(12)) 
RETURNS INTEGER
BEGIN 
  set @x = concat(' 
  SELECT ID, Uprice FROM ',stName,' 
    INTO @var1, @var2 
    WHERE Uprice > 0 
      AND FulItmCode="',fullItemCode,'" 
      AND FullLedgerNo = "',fullLedNo,'" 
     ORDER BY Dat DESC limit 1; 
  '); 
  PREPARE stmt FROM @x; 
      EXECUTE stmt; 
      DEALLOCATE PREPARE stmt; 
  RETURN @var1;
END 

请注意,函数每次调用只能返回一个值。

答案 2 :(得分:0)

如上述评论所述,我并不完全明白你在做什么。以上是逐字记录您的信息,以下似乎可以达到预期的效果。

CREATE PROCEDURE `prcTwo`()
BEGIN
    CALL procOne(@param1, @param2);
END

在您的确切程序中,您错过了一些分号。以下是您更正错误的prcTwo程序

CREATE PROCEDURE `prcTwo`()
BEGIN

CREATE TEMPORARY TABLE tmpStore (
  ID int(10),
  Uprice decimal(18,2)
);

INSERT INTO tmpStore CALL procOne(@param1, @param2) ;

SELECT * FROM tempStore; 

DROP TABLE tmpStore;

END