Jump to content

Returning multiple rows from LOOP inside procedure


Recommended Posts

Hi,

 

I have written the following procedure:

 

 

DELIMITER $$

DROP PROCEDURE IF EXISTS countInstructors$$

CREATE PROCEDURE countInstructors()
BEGIN
	DECLARE done INT DEFAULT 0;
	DECLARE a INT;
	DECLARE cur1 CURSOR FOR SELECT country_id FROM country;
	DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;

	OPEN cur1;

	read_loop: LOOP
		FETCH cur1 INTO a;

		IF done THEN
			LEAVE read_loop;
		END IF;

		SELECT count(*) FROM instructors WHERE country_id = a;

	END LOOP;

	CLOSE cur1;
END$$

DELIMITER ;

 

However i only get 1 row returned.  What i'm trying to do is to get a row returned for each time the query in the loop is run (SELECT count(*)...).  How would i go about doing this?  Or am I barking up the wrong tree?

 

Also i get an error about the last line (DELIMITER ;) telling me to check my syntax.  Could anyone give me a pointer there too please?

 

Many thanks,

 

- Oliver

 

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.