Reputation: 3578
Though there are many questions in Stack Overflow regarding this problem I didn't able to figure it out properly, could anyone can help me to figure this out, let's say that there are two stored procedures as,
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
and
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
I have tried this, but am getting the following error when attempting to save 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
Upvotes: 3
Views: 22839
Reputation: 7027
As mentioned in above comments, I don't fully understand what you are doing. Taking your information above verbatim though, the following would seem to achieve the desired effect.
CREATE PROCEDURE `prcTwo`()
BEGIN
CALL procOne(@param1, @param2);
END
In your exact procedure, you were missing some semicolons. The below is your prcTwo
procedure with errors corrected
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
Upvotes: -1
Reputation: 76567
Procedures do not return values, you'll have to declare procOne
as a function instead:
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
Note that a function can only return a single value per call.
Upvotes: 2
Reputation: 7887
use the out parameter like this
CREATE PROCEDURE abc(IN id INTEGER, OUT text VARCHAR(255))
BEGIN
SET text = 'asd';
END
you can call this procedure like this:
SET @outvar = '';
CALL abc(1, @outvar);
than the 'asd' will be saved in @outvar
Upvotes: 7