MySQL Dynamic Cursor

Source: Internet
Author: User

Mysql itself does not support dynamic cursors, but it can be achieved through (Prepared statements + views + static cursors) methods.

A simple example:

-- Create a test table and data create table webuser (username varchar (10); insert into webuser values ('a1'), ('a2 '), ('a3 '), ('b1 '), ('b2'), ('b3'); Commit; -- create a stored procedure drop procedure if exists dynamic_cursor; delimiter // create procedure dynamic_cursor (in p_name varchar (10) Begin declare done int default 0; declare v_username varchar (10); declare cur cursor for (select username from webuser_view ); declare continue handler for not found set done = 1; drop view if exists webuser_view; Set @ sqlstr = "create view webuser_view as"; Set @ sqlstr = Concat (@ sqlstr, "select username from webuser where username like '", p_name, "%'"); Prepare stmt from @ sqlstr; execute stmt; deallocate prepare stmt; open cur; f_loop: loop fetch cur into v_username; if done then leave f_loop; end if; select v_username; end loop f_loop; close cur; end; // delimiter; -- Test Call dynamic_cursor ('A ');

 

Related Article

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.