Dbms Program 11
Dbms Program 11
CREATE A CURSOR –
1. Declare Cursor Object -
Syntax -
DECLARE cursor_name CURSOR FOR SELECT * FROM
table_name;
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;