Kasun Siyambalapitiya Kasun Siyambalapitiya - 1 month ago 7
MySQL Question

mysql stored procedure with the if condition gives errors

I have the following code of creating a stored procedure in

mysql
.

mysql>delimiter |
mysql>create procedure GetCustomerLevel()
-> if(select count(name) from s_marks)<4
-> then
-> begin
-> select 'inside the if statement' as'';
-> select 'there are lassee than 4 students' as '';
-> end
-> else
-> select 'there are more than 4 stu' as '';
-> end if|


But it gives me 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 'else
select 'there are more than 4 stu' as '';
end if' at line 15


what I am doing wrong? Thanks in advance

Answer

its working as below

CREATE DEFINER=`root`@`localhost` PROCEDURE `GetCustomerLevel`()
BEGIN
         if(select count(name) from s_marks)<4
      then

        select 'inside the if statement' as'';
        select 'there are lassee than 4 students' as '';

      else
         select 'there are more than 4 stu' as '';
      end if;
END
Comments