Instances of cursors used in Mysql stored procedures _mysql

Source: Internet
Author: User

Copy Code code as follows:

DELIMITER $$

DROP PROCEDURE IF EXISTS getuserinfo $$

CREATE PROCEDURE GetUserInfo (in Date_day datetime)
--
--Example
--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;

--Defining cursors
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 yesterday's date
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; --Take 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;

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.