Passing Stored Procedure Results into Parameters - mysql

I am calling a stored procedure (1) within a stored procedure (2). Is there a way to pass values of (1) INTO parameters of (2) so that I can return to the calling program? Thank you.

Yes. You can define parameters to be INOUT or OUT.
This allows the procedure to pass values back to the caller.
As a brief example, proc1 has parameters that are defined as INOUT and OUT. In proc1 values are assigned to those parameters.
And proc2 calls proc1. The values set in proc1 are available to proc2.
DELIMITER ;
CREATE PROCEDURE proc1(INOUT ua INT, OUT ob INT)
BEGIN
SET ua = ua + 1;
SET ob = 1;
END$$
CREATE PROCEDURE proc2()
BEGIN
DECLARE a INT;
DECLARE b INT;
SET a = 0;
SET b = 0;
CALL proc1(a,b);
SELECT a, b;
END$$
DELIMITER ;
CALL proc2();
a b
---- ----
1 1

Related

Output parameter of stored procedure not showing

I can't seem to get my stored procedure to show the OUT Parameter when it is called. It just shows '0 row affected' with no display of the OUTPUT Variable. The OUTPUT is the value of a variable that was declared. This is my procedure
CREATE DEFINER=`root`#`%` PROCEDURE `test`(IN `masterId` INT, IN `subId` INT, OUT current_balance INT)
BEGIN
declare current_balance INT;
set current_balance = 2;
END
This is how I am calling it:
call test('274', '399', #res)
You have overridden the parameter with a local variable.
I prefer to give names to parameters to avoid conflicts:
CREATE DEFINER=`root`#`%` PROCEDURE `test`(
IN in_masterId INT,
IN in_subId INT,
OUT out_current_balance INT
)
BEGIN
set out_current_balance = 2;
END;

Can't declare variable in MySQL [duplicate]

I don't get what is wrong with this script
BEGIN
DECLARE crs INT DEFAULT 0;
WHILE crs < 10 DO
INSERT INTO `continent`(`name`) VALUES ('cont'+crs)
SET crs = crs + 1;
END WHILE;
END;
I want it to insert 10 values into the table continent but there is an error at the second line.
declare variable in MySQL with # and assign with :=
SET #crs = 0; // declaration
--here your query
#crs := #crs+1 // assignment
References
user defined variables
assignment
MySQL does not support the execution of anonymous blocks of stored procedure code.
You need to create a stored procedure including that code and then invoke it.
Also, you were missing the semi-colon at the end of your insert statements. I fixed that. You also probably want to use concat() instead of + to generate the names, but I'll leave that change to you.
Create the procedure:
DELIMITER $$
DROP PROCEDURE IF EXISTS insert_ten_rows $$
CREATE PROCEDURE insert_ten_rows ()
BEGIN
DECLARE crs INT DEFAULT 0;
WHILE crs < 10 DO
INSERT INTO `continent`(`name`) VALUES ('cont'+crs);
SET crs = crs + 1;
END WHILE;
END $$
DELIMITER ;
Invoke the procedure:
CALL insert_ten_rows();

in and out variable in procedure calls

I am using a stored procedure call as follows:
DELIMITER //
CREATE procedure getCustomer(NID varchar(200),Name varchar(200), OUT Flag INTEGER, OUT CID VARCHAR(200))
BEGIN
DECLARE id varchar(200);
SET Flag = 0;
SET id = CONCAT(NID, '_' , Name);
SELECT 1 INTO Flag FROM Customer WHERE customerID = id;
IF Flag = 1 THEN
SET CID = id;
ELSE
INSERT INTO Customer(NID, Name, customerID) VALUES(NID, Name, id);
SET CID = id;
END IF;
END//
can you please tell me how to call IN, OUT variables in testing this procedure call?
or simply how to test this procedure call using exec proceudre_name(parameter) format?
As documented under CALL Syntax:
To get back a value from a procedure using an OUT or INOUT parameter, pass the parameter by means of a user variable, and then check the value of the variable after the procedure returns. (If you are calling the procedure from within another stored procedure or function, you can also pass a routine parameter or local routine variable as an IN or INOUT parameter.) For an INOUT parameter, initialize its value before passing it to the procedure. The following procedure has an OUT parameter that the procedure sets to the current server version, and an INOUT value that the procedure increments by one from its current value:
CREATE PROCEDURE p (OUT ver_param VARCHAR(25), INOUT incr_param INT)
BEGIN
# Set value of OUT parameter
SELECT VERSION() INTO ver_param;
# Increment value of INOUT parameter
SET incr_param = incr_param + 1;
END;
Before calling the procedure, initialize the variable to be passed as the INOUT parameter. After calling the procedure, the values of the two variables will have been set or modified:
mysql> SET #increment = 10;
mysql> CALL p(#version, #increment);
mysql> SELECT #version, #increment;
+--------------+------------+
| #version | #increment |
+--------------+------------+
| 5.5.3-m3-log | 11 |
+--------------+------------+
However, from your procedure it looks as though what you really want is to define a uniqueness constraint over the customerID column in your Customer table and then use INSERT ... ON DUPLICATE KEY UPDATE:
ALTER TABLE Customer ADD UNIQUE (customerID);
INSERT INTO Customer
(NID, Name, customerID)
VALUES
(123, 'foobar', CONCAT(123, '_', 'foobar'))
ON DUPLICATE KEY UPDATE
NID = NID
;
Example using IN:
DELIMITER //
CREATE PROCEDURE `procWithIN` (IN var1 INT)
BEGIN
UPDATE someTable set fldname = fldname + var1;
END//
CALL procWithIN(10);
Example using OUT:
DELIMITER //
CREATE PROCEDURE `procWithOUT` (OUT var1 VARCHAR(100))
BEGIN
SET var1 = 'This is a test';
END //
SET #someText = NULL;
CALL procWithOut(#someText);
-- do something with #someText (containing 'This is a test')
SELECT * FROM tbl WHERE fld=#someText
Example with INOUT
DELIMITER //
CREATE PROCEDURE `procWithINOUT` (INOUT var1 INT)
BEGIN
SET var1 = var1 + 100;
END //
SET #someInt = 10;
CALL procWithINOUT(#someInt);
-- #someInt now contains 110

MYSQL declaring variables

I don't get what is wrong with this script
BEGIN
DECLARE crs INT DEFAULT 0;
WHILE crs < 10 DO
INSERT INTO `continent`(`name`) VALUES ('cont'+crs)
SET crs = crs + 1;
END WHILE;
END;
I want it to insert 10 values into the table continent but there is an error at the second line.
declare variable in MySQL with # and assign with :=
SET #crs = 0; // declaration
--here your query
#crs := #crs+1 // assignment
References
user defined variables
assignment
MySQL does not support the execution of anonymous blocks of stored procedure code.
You need to create a stored procedure including that code and then invoke it.
Also, you were missing the semi-colon at the end of your insert statements. I fixed that. You also probably want to use concat() instead of + to generate the names, but I'll leave that change to you.
Create the procedure:
DELIMITER $$
DROP PROCEDURE IF EXISTS insert_ten_rows $$
CREATE PROCEDURE insert_ten_rows ()
BEGIN
DECLARE crs INT DEFAULT 0;
WHILE crs < 10 DO
INSERT INTO `continent`(`name`) VALUES ('cont'+crs);
SET crs = crs + 1;
END WHILE;
END $$
DELIMITER ;
Invoke the procedure:
CALL insert_ten_rows();

Stored Procedure not working in MySQL

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 $$
This is my code to create a stored procedure....It's created..
For execute..
call simpleproc(#param1,#incr);
select #param1,#incr
The Result will be null values.. It is the simple one.. I've tried many times.But,I get null values only..
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.
Since you define incr as an INOUT paramater, you should not declare it again in the body of your procedure. This way you can increment it properly as long as it is initialized before being passed to your procedure.
Here's the code:
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;