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

时间:2023-01-16 16:38:59

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

如何在存储过程中使用mysql_affected_rows()。

3 个解决方案

#1


18  

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.

如果是更新、删除或插入,ROW_COUNT()将返回更改、删除或插入的行数。对于其他语句,该值可能没有意义。

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()值与mysql_affected_rows() C API函数的值以及mysql客户端在执行语句后显示的行数相同。

#2


6  

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

#3


3  

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 link for more details.

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

#1


18  

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.

如果是更新、删除或插入,ROW_COUNT()将返回更改、删除或插入的行数。对于其他语句,该值可能没有意义。

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()值与mysql_affected_rows() C API函数的值以及mysql客户端在执行语句后显示的行数相同。

#2


6  

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

#3


3  

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 link for more details.

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