Avoiding cursors in store procedures
I recently has joined a new company in which i have been assigned the task of converting Ms-SQL store procedures to MySql Store Procedures. There has been a number of things irritating in MySQL out of those one is that, you only have on SQLSTATE handler for cursor.
If you have nested cursors then you can’t loop thoroughly. At least that was the case with me. So the problem was that how can i avoid creating nested cursors?
I searched the net and came up with a nice technique.
Suppose, here was the thing that i was using in my cursor and wanted to convert it in non cursor technique.
CREATE virtual CURSOR FOR SELECT * FROM someTable;
Then we make a HANDLER and loop through it.
Now to remove this cursor i made a Temporaray Table, Inserted all the values that i am getting through the above query and then traverse it … 🙂
CREATE TEMPORARY TABLE `tempTable` (
`rowId` int(11) NOT NULL auto_increment,
`col1` varchar(8000) default NULL,
`col2` bigint default NULL,
`col3` int(11) default NULL,
`col4` varchar(8000) default NULL,
`col5` varchar (2000) default NULL,
PRIMARY KEY (`rowId`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO tempTable (col1, col2, col3, col4, col5) (
select * from someTable
The actuall thing was pretty complex, i am posting a dumy query just to explain. Now all the data that has been collected using select statement will be inserted in this temporary Table.
Now you have to apply your own logic to traverse through it. Here is mine.
First get the total rows inserted in the temporary column.
SELECT COUNT(*) AS RowCount INTO TotalTableRows FROM tempTable;
You remember that i have used a rowID as primary key and it is autoincremented, this will help us a lot. We will get the min of rowID and increment it, then we will get the record for that incremented value, if the record exist, we will use our logic to utilise the record and increment in the tempRowCount. This loop will continue until we have tempRowCount <= TotalTableRows.
SELECT MIN(rowId) INTO tempRowId FROM tempTable;
WHILE (tempRowCount < TotalTableRows) DO
SELECT col1 INTO tempCol1 FROM tempTable where rowId = tempRowId;
/*Explamation Reference 2*/
IF ISNULL(col1) = 0 THEN
SET tempRowCount = tempRowCount + 1;
-- your business logic
SET tempRowId = tempRowId + 1;
Now you may be wandering what i am doing with ISNULL IF. Well, we are selecting the table with a query having a where clause at RowID (which is autoincremented). Suppose, we don’t have a value against the value 1 but have a value against value 2. In the first case, there will be not data will be selected so the ISNULL IF condition will not execute. I then incremented in temoRowID to make it 2. The select statement execute with some data and the IF ISNULL condition will eventually become true causing it to execute the block.
I may not be good in explanation. But I have tried to give you a good idea.
I’ll try to re-write this topic with more elaboration. Right now, it is enough to give an idea. Thanks.