MySQL: Declare a Cursor

  • Post author:
  • Post category:MySQL
  • Post comments:1 Comment
MySQL declare a cursor

In this MySQL tutorial explains how to declare a cursor in MySQL with syntax and examples.

Description

A cursor is a SELECT statement that is defined within the declaration section of your stored program in MySQL.

Syntax

The syntax to declare a cursor in MySQL is:

DECLARE cursor_name CURSOR FOR
  select_statement;

Parameters or Arguments

cursor_nameThe name to assign to the cursor.select_statementThe SELECT statement is associated with the cursor.

Example

Let’s look at how to declare a cursor.

For example:

DECLARE c1 CURSOR FOR
  SELECT site_id
  FROM sites
  WHERE site_name = name_in;

The result set of this cursor is all site_id values where the site_name matches the name_in variable.

Below is a function that uses this cursor.

DELIMITER //

CREATE FUNCTION FindSiteID ( name_in VARCHAR(50) )
RETURNS INT

BEGIN

   DECLARE done INT DEFAULT FALSE;
   DECLARE siteID INT DEFAULT 0;

   DECLARE c1 CURSOR FOR
     SELECT site_id
     FROM sites
     WHERE site_name = name_in;

   DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

   OPEN c1;
   FETCH c1 INTO siteID;

   CLOSE c1;

   RETURN siteID;

END; //

DELIMITER ;

You could then call your new function (that contains the cursor) as follows:

SELECT FindSiteID ('adglob.in');

Next Topic : Click Here

This Post Has One Comment

Leave a Reply