MySQL: IF in stored procedure - mysql

I am just getting my feet wet with stored procedures. According to the tutorials that I have seen, this should be valid (MySQL 5.5):
CREATE PROCEDURE someFunction ( a VARCHAR(256), b VARCHAR(256) )
BEGIN
DECLARE haveAllVariables INT;
SET haveAllVariables = 1;
IF a = "" THEN SET haveAllVariables = 0
ELSEIF b = "" THEN SET haveAllVariables = 0
END IF;
However, it is throwing this error:
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use near 'ELSEI
F b = "" THEN SET haveAllVariables = 0
Where is the error in my syntax?
Thanks.

You're missing a semicolon
CREATE PROCEDURE someFunction ( a VARCHAR(256), b VARCHAR(256) )
BEGIN
DECLARE haveAllVariables INT;
SET haveAllVariables = 1;
IF a = "" THEN SET haveAllVariables = 0;
ELSEIF b = "" THEN SET haveAllVariables = 0;
END IF;

Stored procedures are a bit tricky. But here is an example I tested and posted for you. In your example you were missing a couple of semicolons and the final "END".
DELIMITER $$
CREATE PROCEDURE someFunction ( a VARCHAR(256), b VARCHAR(256) )
BEGIN
DECLARE haveAllVariables INT;
SET haveAllVariables = 1;
IF a = '' THEN
SET haveAllVariables = 0;
ELSEIF b = '' THEN
SET haveAllVariables = 0;
END IF;
END $$

Related

Error Code: 1064. You have an error in your SQL syntax; for the right syntax to use near 'DECLARE #maxdate DATETIME = (SELECT Max [duplicate]

I am trying to create and set a variable:
DECLARE myId INT;
SET myId = 5;
However, I am getting invalid syntax complaint in MySQL Workbench:
SQL syntax error near 'DECLARE myId INT;'
I have tried the following variants:
DECLARE myId INT(4);
SET myId = 5;
DECLARE #myId INT;
SET #myId = 5;
DECLARE #myId INT(4);
SET #myId = 5;
What is wrong?
Try
SET #myId := 100;
Then if you do
select #myId;
You will get
100
As in the comment says Declare is only valid into stored programs like procedures, functions.
here you have an example of a store procedure and its call.
DELIMITER $$
CREATE PROCEDURE sp1 (x VARCHAR(5))
BEGIN
DECLARE xname VARCHAR(5) DEFAULT 'bob';
DECLARE myId INT;
SET myId = 5;
SELECT CONCAT(xname,' -- ',myId);
END;
$$
DELIMITER ;
call sp1('MY NAME');
I experienced the same problem. The variables must be declared at the beginning of the script.
DELIMITER &&
DROP PROCEDURE IF EXISTS PS_HANDLERS;
CREATE PROCEDURE PS_HANDLERS(IN ID_USER INT, OUT isError INT)
BEGIN
DECLARE USER_EMAIL VARCHAR(50);
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION
BEGIN
SET IsError = 1;
END;
SET USER_EMAIL = CONCAT(RAND(),'#',RAND(),'.com');
SET isError = 0;
INSERT INTO tbl_user VALUES(ID_USER, 'ipsum','lorem','ipsum#lorem.com','password','ROLE_USER');
SELECT
u.*
FROM
tbl_user u;
END &&
DELIMITER ;

What is the correct version of this sql function?

What is the correct way to generate a new id as varchar? Here I'm trying to convert from SQL Server to MySQL database.
Help needed.
I tried internet but it did not solve my problem.
--*GENERATES ANY ID WHEN NEED*
DELIMITER //
CREATE FUNCTION getNewID(needTable VARCHAR(20)) RETURNS VARCHAR(10)
BEGIN
DECLARE lastvalue VARCHAR(10);
DECLARE i INT;
DECLARE newId VARCHAR(10);
IF needTable = 'Item'
SELECT lastvalue = MAX(resourceID) FROM Item;
SELECT MAX(resourceID) INTO lastvalue FROM Item;
IF IS NULL(lastvalue)
SET lastvalue = 'I00000000';
SET i = RIGHT(lastvalue,9) + 1;
SET newId = 'S' + RIGHT('00000000'+CONVERT(VARCHAR(10),i),9);
RETURN newId;
END; //
DELIMITER ;
SELECT getNewID ('Item');
DROP FUNCTION getNewID
The error says:
Error code 1064, SQL state 42000
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SELECT MAX(resourceID) INTO lastvalue FROM Item;
IF IS NULL(lastval' at line 10
Line 3, column 1
Execution finished after 0 s, 1 error(s) occurred.
Try this
DELIMITER //
CREATE FUNCTION getNewID(needTable VARCHAR(20)) RETURNS VARCHAR(10)
BEGIN
DECLARE lastvalue, newId VARCHAR(10);
DECLARE i INT;
SELECT MAX(resourceID) INTO lastvalue FROM Item where needTable = 'Item';
IF(lastvalue IS NULL) THEN
SET lastvalue = 'I00000000';
SET i = RIGHT(lastvalue,9) + 1;
SET newId = 'S' + RIGHT('00000000'+CONVERT(VARCHAR(10),i),9); -- what you are trying to do here?
END IF;
-- what you need to return whenlastvalue is not null?
RETURN newId;
END //
DELIMITER ;

MySQL script inside set statement

I got a hint from O.jones and went to create a stored function.
That's what I created:
DELIMITER $$
CREATE FUNCTION change_int(colres VARCHAR(500)) RETURNS INT(11)
BEGIN
DECLARE res int(11);
DECLARE leng int(11);
DECLARE newres int(11);
DECLARE mult int(11);
DECLARE temp1 int(11);
DECLARE temp2 int(11);
SET res = CAST(colres AS UNSIGNED);
SET leng = CHAR_LENGTH(CAST( colres AS CHAR));
SET newres = 0;
SET mult = 1;
SET temp1 = 0;
SET temp2 = 0;
WHILE (res > 0) DO
SET temp1 = MOD(res , 10 );
SET res = (res DIV 10);
SET temp2 = MOD(res , 10 );
SET newres = (newres +((temp1 + temp2 ) * mult));
SET mult = mult*10;
END WHILE;
SET newres = SUBSTRING (newres, 1, leng );
RETURN newres;
END $$
DELIMITER ;
But I get error when I ty to run it on line 6 :
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''res' = CAST(colres AS CHAR)' at line 6
Added a delimiter, new error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SET newres = SUBSTRING (newres, 1, leng );
RETURN newres;
END' at line 27
Greate now it worked.
But I can not evoke her:
Ran a test with:
SHOW FUNCTION STATUS;
And it exists.
When I try to ran it like this:
UPDATE `table` SET `col1` = function_name(`col1`);
Also tried:
UPDATE `table` SET `col1` = db.function_name(`col1`);
No luck.
You have missing column before END WHILE. It must be END WHILE;
Working example

MySQL syntax-error in nested loop block. Can't find the error.

I tried to code a simple nested loop as a mysql block. The server got me a syntax error, but i can't find it.
Does anybody see it?
The error message was:
Query: END LOOP get_invoice
Error Code: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'END LOOP get_invoice' at line 1
BEGIN
DECLARE v_id INT DEFAULT 0;
DECLARE v_finished1 INT DEFAULT 0;
DECLARE invoices_cursor CURSOR FOR
SELECT id_invoice FROM invoice i WHERE invoice_type = 'RE' AND EXISTS (SELECT 1 FROM invoice ma WHERE invoice_type = 'MA' AND ma.origin = i.id_invoice);
DECLARE CONTINUE HANDLER
FOR NOT FOUND SET v_finished1 = 1;
OPEN invoices_cursor;
get_invoice: LOOP
FETCH invoices_cursor INTO v_id;
IF v_finished1 = 1 THEN
CLOSE invoices_cursor;
LEAVE get_invoice;
END IF;
DECLARE v_finished2 INT DEFAULT 0;
DECLARE v_dunning_level INT DEFAULT 1;
DECLARE dunning_cursor CURSOR FOR
SELECT id_invoice FROM invoice ma WHERE invoice_type = 'MA' AND ma.origin = v_id ORDER BY `create` ASC;
DECLARE CONTINUE HANDLER
FOR NOT FOUND SET v_finished2 = 1;
OPEN dunning_cursor;
get_dunnings: LOOP
DECLARE v_id_dun BIGINT DEFAULT NULL;
FETCH dunning_cursor INTO v_id_dun;
IF v_finished = 1 THEN
LEAVE get_dunnings;
END IF;
UPDATE invoice SET dunning_level = v_dunning_level WHERE invoice_type = 'MA' AND id_invoice = v_id_dun;
SET v_dunning_level = v_dunning_level + 1;
END LOOP get_dunnings ;
CLOSE dunning_cursor;
END LOOP get_invoice ;
END;

MySQL Syntax Error on Stored Procedure

#1064 - You have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version for the right syntax
to use near = -1 else SELECT audit_id INTO t_new_id FROM a_audit_reg at line 11
delimiter //
CREATE FUNCTION get_audit_id (p_pubco_id int(10),
p_audit_id int(10),
p_fiscal_date date)
RETURNS int(10)
BEGIN
DECLARE t_new_id int(10);
#check paremeters here
if p_pubco_id = 0 or p_audit_id = 0
then set t_new_id = -1
else
set t_new_id = (SELECT audit_id
FROM a_audit_reg
WHERE p_pubco_id = a_audit_reg.pubco_id
and p_audit_id = a_audit_reg.audit_id
and p_fiscal_period = a_audit_reg.fiscal_period_date);
if found_rows() = 0
then
insert into a_audit_reg (pubco_id, audit_id, fiscal_period_date)
values (p_pubco_id, p_audit_id, p_fiscal_date);
set t_new_id = last_insert_id();
end if;
end if;
return t_new_id;
END //
delimiter ;
change the DELIMITER
use SET
query,
DELIMITER //
CREATE FUNCTION get_audit_id
(
p_pubco_id INT,
p_audit_id INT,
p_fiscal_date DATE
)
RETURNS INT
BEGIN
DECLARE t_new_id INT;
IF p_pubco_id = 0 or p_audit_id = 0 THEN
SET t_new_id = -1;
ELSE
SET t_new_id = (SELECT audit_id
FROM a_audit_reg
WHERE p_pubco_id = a_audit_reg.pubco_id
and p_audit_id = a_audit_reg.audit_id
and p_fiscal_period = a_audit_reg.fiscal_period_date);
IF found_rows() = 0 then
insert into a_audit_reg (pubco_id, audit_id, fiscal_period_date)
values (p_pubco_id, p_audit_id, p_fiscal_date);
SET t_new_id = last_insert_id();
end if;
end if;
return t_new_id;
END //
DELIMITER ;