MySQL Procedure Not Working - mysql

I'm working with procedures for the first time in MySQL, but for some reason I keep getting NULL. My test procedure is a simple one, it just adds.
delimiter $$
create procedure adds(in r double, out a double)
begin
set a = r + r;
end $$
delimiter ;
CALL adds(5, #a);
SELECT #a;
Not sure if I'm doing this right. For #a it just prints out NULL.

A procedure is linked to a database.
You have not specified one, and therefor it will probably be attached to a different database than the one you are expecting.
When you change databases, MySQL will not longer find your stored procedure because it only looks in the correct DB.
Remember to always specify your database when declaring a stored proc
create procedure mydatabase.adds(in r double, out a double)
^^^^^^^^^^^

Related

Simple Mysql "insert" stored procedure won't work

I can't make this stored procedure load/save, which is very strange because the insert one line version of the procedure works with no problem. I am NOT trying to overload, I gave both procedures and all the variables different names The workbench is telling me that the problem lies with the parenthesis after "flavidtwo". Reversing the order of the inserts doesn't fix the problem, it just starts complaining about the end of the other line instead.
CREATE PROCEDURE `LinkTwoFlavors`(in selzidtwo INT, in flavidone INT, in flavidtwo INT)
BEGIN
INSERT INTO `readinga_seltzer`.`AscSeltzerFlavor` (`SeltzerID`,`FlavorID`) VALUES (selzidtwo, flavidtwo);
INSERT INTO `readinga_seltzer`.`AscSeltzerFlavor` (`SeltzerID`,`FlavorID`) VALUES (selzidtwo, flavidone);
END
Actually, it's complaining about the semi-colon, not the parenthesis. You need to set a different DELIMITER before your procedure (resetting it again afterwards) so that mysql knows to treat the whole procedure as a single statement otherwise it thinks it should end after your first INSERT statement. Your procedure should then look like this
DELIMITER //
CREATE PROCEDURE `LinkTwoFlavors`(in selzidtwo INT, in flavidone INT, in flavidtwo INT)
BEGIN
INSERT INTO `readinga_seltzer`.`AscSeltzerFlavor` (`SeltzerID`,`FlavorID`) VALUES (selzidtwo, flavidtwo);
INSERT INTO `readinga_seltzer`.`AscSeltzerFlavor` (`SeltzerID`,`FlavorID`) VALUES (selzidtwo, flavidone);
END //
DELIMITER ;

MySQL Stored Procedure - if then not functioning

I'm converting SQL Server stored procedures to MySQL and running into issues. I have a stored procedure with an IF THEN ELSE that, while not giving errors, is not returning any data either and I'm not seeing the problem to fix it. The queries by themselves are correct and return data but don't seem to work in the stored procedure. This is a simplified version of the real query just FYI.
The SQL for creating the stored procedure is:
DROP PROCEDURE IF EXISTS `sp_GetVolunteerAwardsList`;
DELIMITER //
CREATE PROCEDURE `sp_GetVolunteerList`( IN glAward_in int)
BEGIN
DECLARE glAward_In INT;
DECLARE awardType_In varchar(100);
DECLARE awardActive INT;
IF (glAward_In) = 0 THEN
SELECT * FROM tbl_volunteer
ELSEIF (glAward_In) = 1 THEN
SELECT * FROM tbl_volunteerpositions
END IF;
END
//
As always, any assistance would be most appreciated.
Check the glAward_In parameter or variable.
The SP is receiving the parameter glAward_in, i in lower case.
Then there is a DECLARE that declares a different variable glAward_In, i in upper case.
The if is done using the glAward_In in uppercase which is not set in any place of the SP. And the parameter in lower case is not used anywhere in the SP.
I think you have to remove the DECLARATION of the variable in upper case, and use the parameter in lowercase for the IF evaluation.

Calling storedprocedure with in a mysql function

Can i call a stored procedure inside a mysql function. I tried but it throws syntax error. Is it possible?
DELIMITER $$
DROP FUNCTION IF EXISTS `fn_abcd`$$
CREATE FUNCTION `fn_abcd`(orderItem BIGINT(45),quantity INT) RETURNS double
BEGIN
DECLARE TotalNetValue DOUBLE;
set TotalNetValue = call sp_abcd(orderItem,quantity);
RETURN TotalNetValue;
END$$
DELIMITER ;
A stored procedure has no return value. You simply cannot do this:
SET someVar = CALL someProcedure();
If you need to retreive some value computed in the procedure, you need to either:
have your stored procedure write the data in a session variable (SELECT something INTO #var) (this is ugly, but it works)
use an INOUT parameter
rewrite the procedure as a function
It is understood that most drivers (JDBC, PDO) will treat the result of the last SELECT statement executed in a stored procedure, as the result of a query like CALL some_procedure(). This feature is specific to the MySQL client/server protocol, the MySQL SQL syntax has no equivalent.

Entity Framework stored procedure: Function Import to Complex Type, Error on Get Column Information

I am working on a project in VS2012 while using MySQL database and Entity Framework 5.0.0.
When I try to create a new Complex type from a stored procedure, I get an error when clicking "Get Column Information" button:
The selected stored procedure or function returns no columns.
My stored procedure code is as following:
DROP PROCEDURE IF EXISTS SearchAlgemeenSet;
Delimiter //
CREATE PROCEDURE SearchAlgemeenSet(IN in_searchQuery VARCHAR(255))
BEGIN
SELECT Blokken, Jaargang, Werk_Uren
FROM algemeensets
WHERE Blokken LIKE in_searchQuery
OR Jaargang LIKE in_searchQuery
OR Werk_Uren LIKE in_searchQuery;
END
//
Delimiter ;
I'm positive that it returns columns if the in_searchQuery parameter has a match.
In my research, I have found plenty solutions for Microsoft SQL database. But none of those solutions apply to MySQL database.
How to solve this?
I'm positive that it returns columns, if the in_searchQuery
parameter has a match.
Because you are not using any wild card for partial search, unless it finds an exact match, for in_searchQuery value, no rows will be returned. For partial match to happen, you need to use wild card symbol '%' with the in_searchQuery value.
Modified procedure, should be like the following:
DROP PROCEDURE IF EXISTS SearchAlgemeenSet;
Delimiter //
CREATE PROCEDURE SearchAlgemeenSet(IN in_searchQuery VARCHAR(255))
BEGIN
set #search_criteria := concat( '%', in_searchQuery, '%' );
SELECT Blokken, Jaargang, Werk_Uren
FROM algemeensets
WHERE Blokken LIKE #search_criteria
OR Jaargang LIKE #search_criteria
OR Werk_Uren LIKE #search_criteria;
END;
//
Delimiter ;
When no search criteria has any matches found, and empty set would be returned.
In your scripting language, you have to check, in advance, whether the procedure has returned any results or not. Based on that, you can show a message that no data found or you can perform other actions.

How to call a stored procedure using while loop in mysql

My question may sound lame but still I cant get whats wrong with this code. I have made a stored procedure in sqlyog and now I want to call it. I have tried almost everything but it doesn't work.
SET #x=1;
WHILE #x<=10 DO
CALL mypro();
SET #x=#x+1;
END WHILE;
Thanks in advance.
Flow control statements like IF, WHILE need to be executed in context of a function or stored procedure. If you wish to execute mypro() in a loop, that action itself must be created as a procedure. Below I will create a procedure called call_mypro_x10() which calls your original stored procedure in a WHILE loop.
DELIMITER $$
CREATE PROCEDURE call_mypro_x10()
BEGIN
SET #x = 1;
WHILE #x <= 10 DO
CALL mypro();
SET #x := #x + 1;
END WHILE;
END$$
DELIMITER ;
Then call this procedure which wraps the other one:
CALL call_mypro_x10();
Note that the DELIMITER statements may not be necessary in all clients, and some MySQL clients supply a different method of specifying an alternate delimiter needed in stored procedure and function definitions. Apparently SQLyog supports DELIMITER
There is an open feature request to permit flow control statements as part of normal query execution.