Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4161109
  • 博文数量: 240
  • 博客积分: 11504
  • 博客等级: 上将
  • 技术积分: 4277
  • 用 户 组: 普通用户
  • 注册时间: 2006-12-28 14:24
文章分类

全部博文(240)

分类: Mysql/postgreSQL

2007-11-30 10:31:56

今天用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 ;

阅读(2908) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~