How to convert a SQL Server cursor to MySQL equivalent -
how can convert procedure below mysql format?
here piece converted:
declare @currentfirstname varchar(300) declare @currentage int declare cursorname cursor fast_forward select firstname,age customers open cursorname fetch next cursorname @currentfirstname, @currentage while @@fetch_status = 0 begin if @age>60 /*this stupid can apply complex condition here*/ begin insert eldercustomers values (@currentfirstname,@currentage) end fetch next cursorname @currentfirstname,@currentage end close cursorname deallocate cursorname
sorry in advance if there wrong above
the mysql equivalent this:
begin declare currentfirstname varchar(300); declare currentage int; declare done int default false; declare cursorname cursor select firstname, age customers; declare continue handler not found set done = true; open cursorname; myloop: loop fetch cursorname currentfirstname, currentage; if done leave myloop; end if; if currentage > 60 insert eldercustomers values (currentfirstname,currentage); end if; end loop; close cursorname; end;
the big difference in loop, using continue handler set flag when there no more rows fetch, , exiting loop when flag set. (that looks ugly, that's way it's done in mysql.)
this example begs question why isn't written (more efficiently, in both sql server , mysql) as:
insert eldercustomers (firstname, age) select firstname, age customers age > 60
Comments
Post a Comment