-1

How to convert a SQL Server cursor to MySQL?

ALTER function [dbo].[Terlambat](@proses int) returns @tempKK table (krk int,terlambat int) as 
begin
declare @uid int, @total_hari_kerja int
select @total_hari_kerja = SUM(alokasi)  from proses 
declare krk_uid cursor for select distinct krk from kartu_kendali where proses <= 8
open krk_uid
fetch next from krk_uid into @uid
while (@@FETCH_STATUS = 0)
begin
insert into @tempKK select top 1 krk, dbo.GetTerlambat(realisasi_tgl_terima, @total_hari_kerja) terlambat from kartu_kendali where proses = 1 and krk = @uid order by RecID
fetch next from krk_uid into @uid
end 
close krk_uid
deallocate krk_uid
return 
end
HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47
Hidayat
  • 33
  • 1
  • 5

1 Answers1

0

For changing a cursor to MySQL that has no cursor you should follow this solution:

  1. Add a Row Number to your SELECT statement that help you to fetch data from your SELECT over it.

  2. Now, you can use a variable that starts with 1 and grows inside the WHILE in each step by 1 and filter your SELECT statement over Row Number and this variable.

Community
  • 1
  • 1
shA.t
  • 16,580
  • 5
  • 54
  • 111