今天用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 ;
|
阅读(2935) | 评论(0) | 转发(0) |