开发者

How we can use mysql_affected_rows() in stored procedure

开发者 https://www.devze.com 2023-01-31 09:02 出处:网络
How we can use mysql_affected_rows() in stored p开发者_开发百科rocedure..Use the ROW_COUNT() information function.

How we can use mysql_affected_rows() in stored p开发者_开发百科rocedure..


Use the ROW_COUNT() information function.

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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消