mysql如何儲存過程返回記錄的更新條數
阿新 • • 發佈:2020-11-23
#ROW_COUNT()返回被前面語句升級的、插入的或刪除的行數。 #這個行數和 mysql 客戶端顯示的行數及 mysql_affected_rows() C API 函式返回的值相同。 use test; create table t(id int,NAME varchar(200),addr varchar(200)); INSERT INTO t VALUES(1),(2),(3); #未查詢到dml的行數,應該是資料已經提交導致 SELECT ROW_COUNT(); DELETE FROM t WHERE id IN(1,2); SELECT ROW_COUNT(); ---------------------------------------------- #呼叫procedure,f_error_code為輸出引數 #if char_length(f_str2) != 0 then的引數傳遞時需要加3個單引號,應該是bug call `sp_test_prepare`(1,'un1','ustr11','''ustr2''',@f_error_code); call `sp_test_prepare`(1,'n1','str','''str2''',@f_error_code); SELECT ROW_COUNT(); SELECT @f_error_code; create table lk5(id int,name varchar(100),str varchar(100),str2 varchar(100)); truncate lk5; insert into lk5 values(1,'n1','str11','str21'),(2,'n2','str12','str22'); select * from lk5; ------------ #定義分隔符 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 declare cnt int default 0; 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; if cnt > 0 then set f_error_code = TRUE; end if; END$$ #DELIMITER ;