Instances that use cursors in MYSQL stored procedures

Source: Internet
Author: User

MYSQL stored procedures can be used to implement many functions. The following describes an instance of using cursors in MYSQL stored procedures.

DELIMITER $

Drop procedure if exists getUserInfo $

Create procedure getUserInfo (in date_day datetime)
--
-- Instance
-- MYSQL stored procedure name: getUserInfo
-- Parameter: date_day Date Format: 2008-03-08
--
BEGIN
Declare _ userName varchar (12); -- User Name
Declare _ chinese int; -- chinese
Declare _ math int; -- Mathematics
Declare done int;

-- Define a cursor
DECLARE rs_cursor cursor for select username, chinese, math from userInfo where datediff (createDate, date_day) = 0;

Declare continue handler for not found set done = 1;

-- Get the date of yesterday
If date_day is null then
Set date_day = date_add (now (), interval-1 day );
End if;

Open rs_cursor;
Cursor_loop: loop
FETCH rs_cursor into _ userName, _ chinese, _ math; -- get data

If done = 1 then
Leave cursor_loop;
End if;

-- Update table
Update infoSum set total = _ chinese + _ math where UserName = _ userName;
End loop cursor_loop;
Close rs_cursor;


END $

DELIMITER;
 

The above is an example of using a cursor in the MYSQL stored procedure.

Mysql Stored Procedure Call Method

Use of mysql ifnull Functions

MySQL date functions

Learn more about mysql CONCAT () Functions

How to view the three MySQL character sets

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.