prepare 和 row_count()一起使用的误区

今天用PREPARE动态处理了UPDATE语句后,
发现ROW_COUNT()函数返回的老是-1 ,检查了下原来是把row_count()放到了
deallocate 语句后面了。

DELIMITER $$

DROP PROCEDURE IF EXISTS `test`.`sp_test_prepare`$$

CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_test_prepare`(IN f_id int,
 IN f_name varchar(64), IN f_str varchar(255),IN f_str2 varchar(255),
 OUT f_error_code boolean)
BEGIN
  -- Determinate whether update is successful or failed.
  declare cnt int default 0;
  -- Update to new record.
  set @stmt = concat('update lk5 set `name` =''',f_name,''', str = ''',f_str,'''');
  set f_error_code = FALSE;
  if char_length(f_str2) != 0 then
    set @stmt = concat(@stmt,', str2 = ',f_str2);
  end if;
  set @stmt = concat(@stmt, ' where id = ',f_id);
  prepare s1 from @stmt;
  execute s1;
  -- Must be above of the deallocate statement.
  -- 只能放在这里才能显示出正确的结果。
  set cnt = row_count();
  deallocate prepare s1;
  -- Get the last record.
  if cnt > 0 then
    set f_error_code = TRUE;
   end if;
END$$

DELIMITER ;

本文出自 “上帝,咱们不见不散!” 博客,转载请与作者联系!

你可能感兴趣的:(mysql,职场,prepare,休闲,row_count)