error 1064 mysql (42000) when omitting a semicolon in `if then ; else ; end if` [closed]

StackOverflow https://stackoverflow.com/questions/20627660

  •  02-09-2022
  •  | 
  •  

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 'if ;
end' at line 18
CREATE PROCEDURE UnitIdCheckForDelete( 
p_unitId varchar(50))
begin
if(
   (
    (
     SELECT ifnull(count(tbl_Product.unitId),0) 
     FROM  tbl_Product 
     WHERE   unitId=p_unitId
    ) > 0
   ) OR 
   (
    (
     SELECT ifnull(count(tbl_UnitConversion.unitId),0) 
     FROM tbl_UnitConversion 
     WHERE  tbl_UnitConversion.unitId=p_unitId
    ) > 0 
   )  
  )
then
SELECT 'true' ;
else
SELECT 'false'
end if ;
end ; //
有帮助吗?

解决方案

Try this:

DELIMITER //
CREATE PROCEDURE UnitIdCheckForDelete( 
p_unitId varchar(50))
begin
if(
   ((SELECT ifnull(count(tbl_Product.unitId),0) 
     FROM  tbl_Product 
     WHERE   unitId=p_unitId)>0) OR 
    ((SELECT ifnull(count(tbl_UnitConversion.unitId),0) 
      FROM tbl_UnitConversion 
      WHERE  tbl_UnitConversion.unitId=p_unitId)> 0 )  
    )
then SELECT 'true' ;
else SELECT 'false' ;
end if ;
end ; //

其他提示

You are missing semicolun at the end of your following statement,

SELECT 'false'

This statement should be like:(as @nrathaus has mentioned in his comments)

SELECT 'false';
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top