Trouble with calling a stored procedure within an stored procedure and setting result as a variable - mysql

I'm trying to call a stored procedure from another stored procedure and store the value in a variable. The inner stored procedure basically checks if something exists and uses a select statement to return a zero or one. I keep getting an error. In this situation, MySQL is saying "=" is not valid at this position, expecting ";"
CREATE PROCEDURE `CardNames_Add` (searchedCard VARCHAR(50))
BEGIN
DECLARE exist TINYINT;
EXECUTE exist = CardNames_CheckExist searchedCard
IF (exist = 0)
INSERT INTO card_names (name)
VALUE(searchedCard)
END

You have to rewrite you other stored procedure, that you don't need btw, to give back a result
CREATE PROCEDURE CardNames_CheckExist (IN searchedCard VARCHAR(50), OUT result TINYINT )
BEGIN
--do some stuzff
result = 1
END
CREATE PROCEDURE `CardNames_Add` (searchedCard VARCHAR(50))
BEGIN
CALL CardNames_CheckExist(searchedCard,#result);
IF (#result = 0) THEN
INSERT INTO card_names (name)
VALUES (searchedCard);
END IF;
END

Related

Why won't MYSQL procedure properly iterate new ROWS in TABLE using WHILE statement?

I am creating this basic procedure using MySQL Workbench to accept a single input parameter.
The table "unique_days" has a single PRIMARY KEY column called "dayid" which currently has a single ROW with a value of 1.
DROP PROCEDURE IF EXISTS dayid_iteration;
DELIMITER $$
CREATE PROCEDURE dayid_iteration(maxdate_final INT)
BEGIN
DECLARE maxdate_current INT;
SET #maxdate_current = (SELECT (MAX(dayid) + 1) FROM unique_days);
DELETE FROM unique_days WHERE dayid > 1;
WHILE (maxdate_current > maxdate_final) DO
INSERT INTO unique_days (dayid) VALUES (maxdate_current);
SET maxdate_current = (maxdate_current+1);
END WHILE;
END$$
DELIMITER ;
The procedure is then called with an integer parameter.
CALL dayid_iteration(11);
The variables are setting properly because I can run a select statement with the variable and it shows the correct new value. The deletion of dayid > 1 also works (Tested by manually adding additional rows, and then running procedure). However, I can't seem to get the WHILE statement to insert new rows with the value provided.
Any help is much appreciated. I searched multiple other questions, and countless forums, but everything looks like it should be working.
I am expecting the code to CREATE 9 ROWS for a total of 10 ROWS.
The following is included just so you can see the starting values of the table.
SELECT * FROM unique_days;
For anyone who finds this question, the following code functions correctly. The input variable on the parameter was not setting properly. Once the parameter had "IN" placed in front of the variable name, it correctly received the parameter.
CREATE PROCEDURE dayid_iteration(IN maxdate_final INT)
DROP PROCEDURE IF EXISTS dayid_iteration;
DELIMITER $$
CREATE PROCEDURE dayid_iteration(IN maxdate_final INT)
BEGIN
DECLARE maxdate_current INT;
SET maxdate_current = (SELECT (MAX(dayid) + 1) FROM unique_days);
WHILE (maxdate_current <= maxdate_final) DO
INSERT INTO unique_days (dayid) VALUES (maxdate_current);
SET maxdate_current = (maxdate_current+1);
END WHILE;
END$$
DELIMITER ;
CALL dayid_iteration(1000);
This call procedure now properly works.
CALL dayid_iteration(1000);

SQL stored procedure- where am I going wrong?

I'm trying to create a stored procedure that calculates total revenue from a customer by if it's occupied and the standard rate. I am getting an error message and when I try to call from it I get NULL. Can anyone help? Thanks.
//Delimiter
CREATE PROCEDURE calculateRevenue (in customerIDs int, OUT totalRevenue dec(15,2))
BEGIN
SELECT SUM(Occupied*StandardRate) into totalRevenue FROM climatesouth
WHERE customerIDs = customerID;
END //
delimiter//
call calculateTotal(10, #totalRevenue);
SELECT #totalRevenue;
First you need to give your input parameters different names from the columns. Then you need to use them. Also, DELIMITER goes before the stored procedure definition:
DELIMITER //
CREATE PROCEDURE calculateRevenue (
in in_customerIDs int,
out out_totalRevenue dec(15,2))
BEGIN
SELECT SUM(cs.Occupied cs.* cs.StandardRate) into out_totalRevenue
FROM climatesouth cs
WHERE cs.customerID = in_customerID;
END //
The delimiter assignment is off: your are setting it to // after the create procedure statement.
Also, the parameter name needs to be fixed: your are not using the correct name in the query (your parameter has a trailing 's'), because of which the procedure will not produce the result you expect.
So:
delimiter // -- change the default delimiter here
create procedure calculaterevenue (in p_customerid int, out p_totalrevenue dec(15,2))
begin
select sum(occupied * standardrate) into p_totalrevenue
from climatesouth
where customerid = p_customerid; -- "p_customerid" is the parameter name
end //
delimiter ; -- reset the delimiter, now we can call the procedure
call calculaterevenue(10, #totalrevenue);
select #totalrevenue;
It is easier just to return the result as a result set rather than to use the OUT-parameter. The OUT-parameters are usually used only when calling procedure from another procedure. If you call the procedure from your application, use the result set.
delimiter //
CREATE PROCEDURE calculateRevenue (in_customerID int)
BEGIN
SELECT SUM(Occupied*StandardRate) as totalrevenue
FROM climatesouth
WHERE customerID = in_customerID;
END
//
delimiter ;
call calculateRevenue(10);

MySQL procedure with multi in parameters when you can pass only one

I have create MySQL procedure having multiple IN parameters. I want to call procedure with few parameters but when I leave other fields blank it shows this error:
DELIMITER $$
CREATE DEFINER=itzakeed_akeed#localhost PROCEDURE ApiKez(
IN Choice VARCHAR(100),
IN ValidKey VARCHAR(100),
IN azid INT(5),
IN amts FLOAT(50)
)
BEGIN
DECLARE GetKey VARCHAR(100);
DECLARE Balance FLOAT;
CASE WHEN Choice='KeyCheck' THEN
SELECT COUNT(id) INTO GetKey
FROM users
WHERE api_key=ValidKey;
if key is valid
IF GetKey=1 THEN
SELECT *
FROM users
WHERE key=ValidKey;
ELSE
SELECT 0;
END IF;
ELSE
SELECT "INVALID INPUT CHOICE";
END CASE;
END
$$
DELIMITER ;
No you cannot call a procedure with less parameters than what is required. However, you can pass null or empty strings and check if a parameter has a value from withing the stored procedure.

Using if statements in a stored procedure in MySQL

I'm trying to create a stored procedure in MySQL with an if-elseif statement but it doesn't seem to work. I can create a procedure that will run either the if or the elseif but not with the actual switch.
CREATE DEFINER=`root`#`%` PROCEDURE `sp_do_something`(
IN setting VARCHAR(30),
IN setting2 VARCHAR(30),
IN if_else_switch VARCHAR(20))
DETERMINISTIC
IF #if_else_switch = 'foo' then
BEGIN
...
END;
elseif #if_else_switch = 'bar' then
BEGIN
...
END;
END IF;
Then I call it with
CALL `database`.`sp_do_something`('setting', 'setting2', 'foo');
It completes without any errors but just returns "Affected rows: 0" and doesn't appear to be doing any of the actual SQL-code.
I'm guessing that it has something to do with the last value of the call-query and it somehow not being caught by either if - but why?
Try This
CREATE DEFINER=`root`#`%` PROCEDURE `sp_do_something`(
IN setting VARCHAR(30),
IN setting2 VARCHAR(30),
IN if_else_switch VARCHAR(20))
DETERMINISTIC
IF if_else_switch = 'foo' then
......
elseif if_else_switch = 'bar' then
.......
END IF;
when you use # with variable so it is global variable and it value persist till end of the session.
and in your case if_else_switch is come parameter so it is local variable. so use the local variable instead global variable
for more know http://www.java2s.com/Tutorial/MySQL/0201__Procedure-Function/LOCALSESSIONANDGLOBALVARIABLESINMYSQL.htm

Getting errors when trying to create a PROCEDURE in mysql

I am trying to create a mysql stored procedure, but I get this error:
Script line: 2 Failed to CREATE PROCEDURE proc_test_bideep
The stored procedure code is:
DELIMITER $$
DROP PROCEDURE IF EXISTS `commun`.`insert_categorie` $$
CREATE PROCEDURE `commun`.`insert_categorie` (id_mere INT,
lib_categ VARCHAR(50),
id_categ_sup INT ,
categ_authInstantBuy INT)
BEGIN
SET #bg_mere := (SELECT categ_bg FROM categ_basic WHERE categ_id = id_mere);
#bg_mere+2,categ_level_bideep,categ_statut,categ_adult,categ_authSmallBid,categ_authBid,categ_authInstantBuy);
SELECT '1' AS code_retour; END IF;
ecetera.........
END $$
DELIMITER ;
a) You need to DECLARE any variables on the first lines of the procedure, including their datatype:
DECLARE bg_mere INT;
b) To fetch a value from the database into a variable, you use SELECT ... INTO syntax:
SELECT categ_bg INTO bg_mere FROM categ_basic WHERE categ_basic.categ_id = id_mere;
c) You have an END IF without the corresponding IF.
d) The closing END needs a semicolon (not BEGIN though), only then do you need a delimiter to finish the entire statement, and finally you should reset the delimiter back to normal:
BEGIN
# body of the stored procedure goes here
END;
$$
DELIMITER ;
Your parameters are missing the keyword IN such as: ...(IN id_mere INT, IN lib_categ ...). Also, you need to configure your OUT variable for #bg_mere in the initial parameter list such as (IN xxx, ..., OUT bg_mere VARCHAR/INT/WHATEVER).