MySQL Stored Procedure IF multiple statement

Hi,

this is my stored procedure on database MySql version 8.0.17

CREATE DEFINER=`root`@`%` PROCEDURE `SP`(IN tun CHAR(100), tmonth CHAR(100))
BEGIN

IF RIGHT(tun,2) = '00' THEN

SET @s = CONCAT('SELECT * FROM `t_ri` WHERE LEFT (t.un, 2) = LEFT(\'',tun,'\',2);');
	
ELSE
	
SET @s = CONCAT('SELECT * FROM `t_ri` WHERE t.un = \'',tun,'\';');	
	
END IF;

PREPARE stmt FROM @s;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

SELECT @s;

END

I need insert a new IF condition in stored procedure when the value of variable tmonth is not null.

If variable tmonth is not null and RIGHT(tun,2) = '00' the query it should be

SET @s = CONCAT('SELECT * FROM `t_ri` WHERE LEFT (t.un, 2) = LEFT(\'',tun,'\',2) AND tmonth = \'',tmonth,'\';');

else

SET @s = CONCAT('SELECT * FROM `t_ri` WHERE t.un = \'',tun,'\' AND tmonth = \'',tmonth,'\';');

I have tried multiple conditions but none have ever worked… can you help me?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.