mysql中的存储过程不起作用

sfd*_*eta 2 mysql sql stored-procedures

我正在尝试插入一个值,以防未插入,否则尝试更新其某些字段。只使用了一个变量。尽管 ion 调用存储过程,但它显示插入了一行,但未插入值。请帮助我,第一次尝试 SP。

这是我的存储过程

CREATE DEFINER=`root`@`localhost` PROCEDURE `InsertLocation`(in IpAddress varchar(45))
BEGIN     
  if (SELECT count(*) as count 
      FROM mbuzzz.location_byhits 
      where IpAddress = IpAddress
      having count>0)    
  then     
      UPDATE location_byhits SET Hits=Hits+1 where  IpAddress=IpAddress;    
  else     
      insert into location_byhits(IpAddress,Date_current) 
      values (IpAddress,CURTIME());     
  End if ;    
end
Run Code Online (Sandbox Code Playgroud)

jue*_*n d 5

重命名您的输入参数,以便在您指的是参数和列名称时让数据库引擎清楚。

where IpAddress = IpAddress 
Run Code Online (Sandbox Code Playgroud)

始终为真,因为引擎认为您将一列与其自身进行比较。

尝试

delimiter |
CREATE DEFINER=`root`@`localhost` PROCEDURE `InsertLocation`(in IpAddrParam varchar(45))
BEGIN     
  if ((SELECT count(*) FROM mbuzzz.location_byhits where IpAddress = IpAddrParam)>0)    
  then     
      UPDATE location_byhits SET Hits=Hits+1 where IpAddress=IpAddrParam;    
  else     
      insert into location_byhits(IpAddress,Date_current) 
      values (IpAddrParam, CURTIME());     
  End if ;    
end
|
delimiter ;
Run Code Online (Sandbox Code Playgroud)