MySQL Stored Procedure - if then not functioning - mysql

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.

Related

MySQL Cursors - having errors in MySQL Workbench

I am trying to write a mysql program, which has cursors in it. Due to an error in writing an DECLARE query, MySQL Workbench is always showing me the DECLARE is not valid at this position, expected EOF, ALTER, ANALYZE, BEGIN, BINLOG, CACHE, ...
Could you help me solve this problem?
Here is my code:
DELIMITER //
BEGIN
declare Naslov_knjige VARCHAR(24);
declare Cena_knjige DECIMAL(8,2);
DECLARE cursor_cene CURSOR
FOR SELECT
Naslov,
Cena
FROM
prvi_test_v2.knjige;
OPEN cursor_cene //
FETCH NEXT FROM cursor_cene INTO
#Naslov_knjige,
#Cena_knjige //
WHILE ##FETCH_STATUS = 0
BEGIN
PRINT #Naslov_knjige + CAST(#Cena_knjige AS VARCHAR) //
FETCH NEXT FROM cursor_cene INTO
#Naslov_knjige,
#Cena_knjige //
END //
CLOSE cursor_cene //
DEALLOCATE cursor_cene //
END //
DELIMITER ;
Thanks for your help!
I will assume you omitted a line for CREATE PROCEDURE, because in MySQL a BEGIN...END block must be part of a stored routine. See https://dev.mysql.com/doc/refman/8.0/en/begin-end.html
BEGIN ... END syntax is used for writing compound statements, which can appear within stored programs (stored procedures and functions, triggers, and events).
You changed the DELIMITER:
DELIMITER //
Using this delimiter terminates the whole CREATE PROCEDURE statement. You should not do this after the first statement in the body of the procedure. You need to use the normal ; terminators for each statement within the body of the procedure. The reason for changing the delimiter is so you can use ; for each statement in the procedure without ending the CREATE PROCEDURE.
See examples and documentation here: https://dev.mysql.com/doc/refman/8.0/en/stored-programs-defining.html
That's the reason for the error you got. You used // to terminate OPEN cursor_cene // which ended the CREATE PROCEDURE, but clearly there was more to that procedure.
There are other problems with your procedure. You seem to be using Microsoft SQL Server syntax, but MySQL is different.
Naslov_knjige is not the same variable as #Naslov_knjige in MySQL. Don't use the # sigil in front of local variables. If you use the # sigil, this refers to a user-defined variable.
The WHILE ##FETCH_STATUS = 0 syntax is specific to Microsoft SQL Server. MySQL has different syntax for running a cursor loop. See example in the documentation: https://dev.mysql.com/doc/refman/8.0/en/cursors.html
That's as far as I got. There may be more problems, but I am not going to look for them.

Error 1064 declaring variable inside trigger

I'm having trouble declaring a variable inside a trigger.
SET DELIMITER ;;
BEGIN
DECLARE qtyNow INT;
SET qtyNow = (
SELECT qty
FROM warehouse
WHERE bin_id = 'GA66'
);
DECLARE need INT;
SET need = (
SELECT min_level
FROM warehouse
WHERE bin_id = 'GA66'
);
END;;
SET DELIMITER ;
I get error #1064 which means illegal syntax. I don't see where I went wrong. I even removed all that bulk and just had
DECLARE qtyNow INT;
And this single line still pops the error.
I see two problems.
One, you have a BEGIN...END block but you are not declaring a trigger.
https://dev.mysql.com/doc/refman/5.7/en/begin-end.html says:
BEGIN ... END syntax is used for writing compound statements, which can appear within stored programs (stored procedures and functions, triggers, and events).
You can't use BEGIN...END as a bare statement. It must be part of a CREATE TRIGGER, CREATE PROCEDURE, CREATE FUNCTION, or CREATE EVENT.
Two, you have two DECLARE statements in your block, with a SET in between.
https://dev.mysql.com/doc/refman/5.7/en/declare.html says:
DECLARE is permitted only inside a BEGIN ... END compound statement and must be at its start, before any other statements.
You are trying to use a second DECLARE after you have done another statement, SET. Do all your DECLAREs up front.

User-Defined Functions Returning Table in MySQL

PostgreSQL allows you to define a function that returns a table. Does MySQL provide a similar feature? My research suggests not, but I'd be grateful if someone could show me otherwise.
Essentially, I want to add a "running-total" column to a rowset, and this is one of the options I'm investigating.
You can not return a table using MySQL function, but you can using a stored procedure, I got something like this:
DELIMITER $$
CREATE DEFINER=`root`#`%` PROCEDURE `sp_Name`(OUT po_ErrMessage VARCHAR(200))
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
SET po_ErrMessage = 'Error in procedure sp_Name';
END;
SELECT * FROM table_name;
END
And for more information please refer to this link

Correct syntax for calling a stored procedure with parameters from within another stored procedure

I am having difficulty establishing the correct syntax for calling a stored procedure within a stored procedure. I thought this syntax was correct:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[newProc](
#param1 varchar(50),
#param2 nvarchar(2000),
#param3 int,
#param4 int,
#param5 int,
#param6 int
)
AS
BEGIN
SET NOCOUNT ON;
EXEC [dbo].[externProc1] #param1, #param2;
GO
EXEC [dbo].[externProc2] #param3, #param4;
GO
EXEC [dbo].[insertProc3] #param5, #param6;
GO
END
GO
But when I try this, I keep getting
"Incorrect syntax near ';' Must declare the scalar variable #param3.
Must declare the scalar variable #param5."
What is the correct syntax for calling a stored procedure with parameters from within another stored procedure?
The GO keyword in T-SQL states that a block of t-sql statements ends and executes that block. This means that a variable defined in one block is not longer "in scope" for a statement occurring after that block has been ended/executed with GO.
GO is not actually a transact sql statement:
http://msdn.microsoft.com/en-us/library/ms188037(v=sql.105).aspx
Users must follow the rules for batches. For example, any execution of
a stored procedure after the first statement in a batch must include
the EXECUTE keyword. The scope of local (user-defined) variables is
limited to a batch, and cannot be referenced after a GO command.

MySQL Procedure Not Working

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)
^^^^^^^^^^^