multiple mysql stored procedure in a single call - mysql

I have a stored procedure like this and it's working fine:
$drop = $mysqli->query("DROP PROCEDURE IF EXISTS changegroup");
$initiate = $mysqli->query("
Create Procedure changegroup(IN param1 int(10),IN param2 int(10))
BEGIN
UPDATE t_parts SET part_group_id = param2 WHERE part_id = param1;
END;
");
$result = $mysqli->query("CALL changegroup($p1,$p2);");
My question is, can we put two SQL statements in a single procedure and execute the second procedure based on first like this:
BEGIN
SELECT * FROM ........
/**fetch the result of this mysql statment and if matches certain conditions,then execute the update statment***/
UPDATE t_parts SET part_group_id = param2 WHERE part_id = param1;
END;

In your stored procedure write
if <condition> then
your code
end if;
So your code should be like this
Create Procedure changegroup(IN param1 int(10),IN param2 int(10))
BEGIN
DECLARE totalcount Integer default 0;
SELECT count(*) into totalcount FROM yourtable where <condition>;
/**fetch the result of this mysql statment and if matches certain conditions,then execute the update statment***/
if(totalcount > 0) then
UPDATE t_parts SET part_group_id = param2 WHERE part_id = param1;
end if;
END;

Related

How to use different where conditions depending on the input parameter in procedure?

I want to use different where conditions, which depends on input parameter in procedure.
CREATE PROCEDURE proc_IF (IN param1 INT)
BEGIN
SELECT * FROM articles
IF param1 = 0 THEN
WHERE name = 'Тест'
ELSE
WHERE name = 'Проверка'
END IF;
END
You can use case when when there are more than two conditions
CREATE PROCEDURE proc_IF (IN param1 INT)
BEGIN
SELECT * FROM articles
WHERE name =
(
CASE WHEN param1 = 0 THEN 'Тест'
ELSE 'Проверка' END
);
END
If there are only two conditions,then you can use IF instead
CREATE PROCEDURE proc_IF (IN param1 INT)
BEGIN
SELECT * FROM articles
WHERE name = IF(param1 = 0,'Тест','Проверка');
END
I would suggest avoiding CASE expressions in the WHERE clause in general. You can use:
CREATE PROCEDURE proc_IF (IN param1 INT)
BEGIN
SELECT a.*
FROM articles a
WHERE (param1 = 0 AND name = 'Тест') OR
(not param1 <=> 0);
END

Wrong result while declaring a variable inside Stored procedure

Following is a simple stored procedure to calculate male count from a table , I have declared a variable total_count inside the proc where i'm storing my result.
DELIMITER //
CREATE PROCEDURE GetMaleCount()
BEGIN
DECLARE total_count INT DEFAULT 0 ;
SELECT COUNT(STUDENT_ID) INTO total_count
FROM [table1]
where STUDENT_GENDER = 'M' ;
END //
DELIMITER ;
call GetMaleCount();
select #total_count as tc;
When i executed this procedure i'm getting NULL as the answer, but when i seperately executed just the inner sql query i got the right answer 1852. have i declared the variable in the wrong way ?
total_count that you've declared is visible only in procedure. That is why it is NULL outside of it. You need to use OUT parameter when defining procedure:
DELIMITER //
CREATE PROCEDURE GetMaleCount(OUT total_count INT)
BEGIN
SELECT COUNT(STUDENT_ID) INTO total_count
FROM [table1]
where STUDENT_GENDER = 'M' ;
END //
DELIMITER ;
call GetMaleCount(#total_count);
select #total_count as tc;
You need to use OUT parameter.
DELIMITER //
CREATE PROCEDURE GetMaleCount(OUT total_count INT)
BEGIN
SELECT COUNT(STUDENT_ID) INTO total_count
FROM [table1]
where STUDENT_GENDER = 'M' ;
END //
DELIMITER ;
call GetMaleCount(#total_count);
select #total_count as tc;

How can I get and assign the return value from MySQL stored procedure

I have the next stored procedure:
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `getUserIdByLogin`(userId VARCHAR(255))
BEGIN
SELECT id FROM `userdata` WHERE login = userId;
END
I want to declare a new variable #tmp for e.g. and do smth to this:
SET #tmpValue = CALL getUserIdByLogin("someLogin");
But it doesn't work.
If just to call:
CALL getUserIdByLogin("someLogin");
Then I would see results, but I need to declare the results in the variable ( of array type ).
How can I do it?
Thanks!
CREATE DEFINER=`root`#`localhost` PROCEDURE `getUserIdByLogin`(
userId VARCHAR(255),
OUT idout int
)
BEGIN
SELECT id INTO idout FROM `userdata` WHERE login = userId;
END
then
SET #id = 0;
CALL getUserIdByLogin("someLogin", #id);
SELECT #id;

How to return a resultset from StoredProcedure using MySql?

I am writing a stored procedure using MySql which returns multiple rows using select statement.
My code is as below
drop procedure if exists GetAccounts;
DELIMITER //
CREATE PROCEDURE GetAccounts()
BEGIN
DECLARE rowcount int;
SET #resultset = (SELECT * from requests where STATUS = "FAILURE" ;
END //
DELIMITER
Any examples of how to return a resultSet in storedProcedure?
Thanks
Gendaful
DROP PROCEDURE IF EXISTS GetAccounts;
DELIMITER //
CREATE PROCEDURE GetAccounts()
BEGIN
DECLARE rowcount int;
SELECT * from requests where STATUS = "FAILURE" ;
END //
DELIMITER ;

Mysql stored procedure multiple selects

I am running a stored procedure. The issue seems to be that it will go into the if statement. Also for some reason or another regardless of how many selects I use it will only return the first. I've copied this from another stored procedure that works like a charm, but this one just won't go. Any ideas?
DROP PROCEDURE IF EXISTS genSelPriceTier;
DELIMITER $$
CREATE PROCEDURE genSelPriceTier(tier_id INT, default_id INT)
BEGIN
DECLARE rowCount INT DEFAULT 0;
SELECT * FROM price_tier WHERE price_tier_id = tier_id;
SET rowCount = FOUND_ROWS();
IF rowCount < 1 THEN
SELECT * FROM price_tier WHERE price_tier_id = default_id;
END IF;
END$$
DELIMITER ;
There is a bug reported related to the usage of FOUND_ROWS(). So, I recommend using Count(*) for the number of rows returned. Something like the following should work.
DROP PROCEDURE IF EXISTS genSelPriceTier;
DELIMITER $$
CREATE PROCEDURE genSelPriceTier(tier_id INT, default_id INT)
BEGIN
DECLARE rowCount INT DEFAULT 0;
SELECT COUNT(*) INTO rowCount FROM price_tier WHERE price_tier_id = tier_id
IF rowCount < 1 THEN
SELECT * FROM price_tier WHERE price_tier_id = default_id;
END IF;
END$$
DELIMITER ;