mysql stored procedure cursor

BEGIN
	DECLARE _sid_ varchar(32);
  DECLARE _name_ varchar(32);


	
	-- traverse the end of data flag
  DECLARE done INT DEFAULT FALSE;

  

	#define cursor
	DECLARE cur_table cursor for select c.SID_,c.NAME_ from open_clubs c;
  -- bind the end marker to the cursor
  DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
	

	#Temporary table data is mainly to save the information of the current paging data
	DROP TABLE IF EXISTS TEP_CLUBS_INFO;
	CREATE TEMPORARY TABLE TEP_CLUBS_INFO(   
	 SID VARCHAR(255),
	 NAME_ VARCHAR(255)
	);
	
	OPEN cur_table;

   -- start loop
  read_loop: LOOP
    -- Extract the data in the cursor, there is only one here, and the same is true for multiple;
    FETCH cur_table INTO _sid_, _name_;
    -- at the end of the statement
    IF done THEN
      LEAVE read_loop;
    END IF;
    -- insert temporary table
    insert into TEP_CLUBS_INFO(sid,name_)values(_sid_,_name_);
  END LOOP;
  -- close cursor
  CLOSE cur_table;

	-- Query the temporary table
	select * from TEP_CLUBS_INFO;

END

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326879990&siteId=291194637