MySQL 我们如何在存储过程中使用 mysql_affected_rows()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4458264/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 18:01:42  来源:igfitidea点击:

How we can use mysql_affected_rows() in stored procedure

mysql

提问by Null Pointer

How we can use mysql_affected_rows()in stored procedure..

我们如何mysql_affected_rows()在存储过程中使用..

回答by martin clayton

Use the ROW_COUNT()information function.

使用ROW_COUNT()信息函数。

ROW_COUNT() returns the number of rows changed, deleted, or inserted by the last statement if it was an UPDATE, DELETE, or INSERT. For other statements, the value may not be meaningful.

The ROW_COUNT() value is the same as the value from the mysql_affected_rows() C API function and the row count that the mysql client displays following statement execution.

ROW_COUNT() 返回由最后一条语句更改、删除或插入的行数(如果它是 UPDATE、DELETE 或 INSERT)。对于其他语句,该值可能没有意义。

ROW_COUNT() 值与来自 mysql_affected_rows() C API 函数的值以及 mysql 客户端在语句执行后显示的行数相同。

回答by egemen

example

例子

BEGIN
DECLARE countRow INT;
DECLARE roomTypeId INT;
        INSERT INTO room_type (room_type)
SELECT * FROM (SELECT paramRoomType) AS tmp
WHERE NOT EXISTS (
    SELECT room_type_id FROM room_type WHERE room_type = paramRoomType 
) LIMIT 1;
SET countRow =  ROW_COUNT();

IF(countRow >  0) THEN
    SET roomTypeId =  LAST_INSERT_ID();
    INSERT hotel_has_room_type (hotel_id,room_type_id) VALUES (paramHotelId,roomTypeId);
END IF;
END

回答by rkg

You cannot use mysql_affected_rows()in a stored procedure since it's a C API function. You can use FOUND_ROWS()function which gives a similar functionality. Refer this linkfor more details.

您不能mysql_affected_rows()在存储过程中使用,因为它是一个 C API 函数。您可以使用FOUND_ROWS()提供类似功能的函数。有关更多详细信息,请参阅此链接