0% found this document useful (0 votes)
18 views

Dbms Program 11

The document discusses creating and using cursors in SQL. A cursor allows iterating through the rows of a database table. The document shows how to declare, open, fetch from, and close a cursor. An example stored procedure is provided to implement a cursor that returns names from a table.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Dbms Program 11

The document discusses creating and using cursors in SQL. A cursor allows iterating through the rows of a database table. The document shows how to declare, open, fetch from, and close a cursor. An example stored procedure is provided to implement a cursor that returns names from a table.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

PROGRAM – 11

OBJECTIVE – Creating Cursor.

CURSOR – Cursor is a Temporary Memory or Temporary Work Station.


It is Allocated by Database Server at the Time of Performing DML(Data
Manipulation Language) operations on Table by User.
Cursors are used to store Database Tables.

CREATE A CURSOR –
1. Declare Cursor Object -
Syntax -
DECLARE cursor_name CURSOR FOR SELECT * FROM
table_name;

2. Open Cursor Connection -


Syntax -
OPEN cursor_name;

3. Fetch Data from cursor -


Syntax -
FETCH FROM cursor_name INTO var_name;

4. Close Cursor Connection -


Syntax -
CLOSE cursor_name;

5. Deallocate Cursor Memory -


Syntax -
DEALLOCATE cursor_name;
CREATING TABLE -
CREATE TABLE cursorTest(id int, name varchar(20), contact_no bigint);

INSERTING VALUES -
INSERT INTO cursorTest VALUES(10, "Mohan", 8320145675);

SHOW TABLE -
SELECT * FROM cursorTest;

IMPLEMENTING CURSOR -
To implement cursor, We need to create a stored procedure.
The given cursor returns the names of the students in present in the “cursorTest”
Table.

CURSOR IMPLEMENTATION -
DELIMITER $$
CREATE PROCEDURE list_name ( INOUT nameList VARCHAR(4000))
BEGIN
DECLARE is_done INTEGER default 0;
DECLARE s_name varchar(100) default "";
DECLARE stud_cursor CURSOR FOR SELECT name from
cursorTest;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET is_done = 1;
OPEN stud_cursor;

get_list : LOOP
FETCH stud_cursor INTO s_name;
IF is_done = 1 THEN
LEAVE get_list;
END IF;
SET nameList = CONCAT(s_name, ';', nameList);
END LOOP get_list;
CLOSE stud_cursor;
END $$

DELIMITER ;
SET @name_list ="";
CALL list_name(@name_list);
SELECT @name_list;

You might also like