I need to do the following in ZOHO reports using cursors.
declare @SemanaActual money;
declare @SemanaAnterior money;
declare @SaldoFinal money;
declare @SaldoInicial money;
DECLARE vendor_cursor CURSOR FOR
select distinct A.Semana,A.SemanaAnterior FROM AdventureWorks.dbo.
OperacionesJuan A
OPEN vendor_cursor
FETCH NEXT FROM vendor_cursor
INTO @SemanaActual,@SemanaAnterior
WHILE @@FETCH_STATUS = 0
BEGIN
if (@SemanaActual=1409)--semana inicial
begin
select
@SaldoFinal=coalesce((select SUM(CASE
WHEN Codigo='SI' THEN Saldo_inicial_del_banco
ELSE 0
END ) FROM AdventureWorks.dbo.
OperacionesJuan where SemanaAnterior= @SemanaAnterior)+
SUM(Ingresos)+ SUM(Egresos),0)
FROM AdventureWorks.dbo.
OperacionesJuan
where Codigo='SI' and SemanaAnterior= @SemanaAnterior
select SUM(Saldo_inicial_del_banco) as 'SaldoInicial',
coalesce(SUM(Ingresos),0) as 'Ingresos' ,
coalesce(SUM(Egresos),0) as 'Egresos',
@SaldoFinal as 'SaldoFinal',
@SemanaActual as 'SemanaActual',
@SemanaAnterior as 'SemanaAnterior'
FROM AdventureWorks.dbo.
OperacionesJuan
where Codigo='SI' and SemanaAnterior= @SemanaAnterior
end
else
begin
set @SaldoInicial=@SaldoFinal;
select
@SaldoFinal=coalesce((@
SaldoInicial + SUM(Ingresos)+ SUM(Egresos)),0)
FROM AdventureWorks.dbo.
OperacionesJuan
where SemanaAnterior= @SemanaAnterior
select @SaldoInicial as 'SaldoInicial',
coalesce(SUM(Ingresos),0) as 'Ingresos' ,
coalesce(SUM(Egresos),0) as 'Egresos',
coalesce(@SaldoFinal ,0) as 'SaldoFinal',
@SemanaActual as 'SemanaActual',
@SemanaAnterior as 'SemanaAnterior'
FROM AdventureWorks.dbo.
OperacionesJuan
where SemanaAnterior= @SemanaAnterior
end
-- Get the next vendor.
FETCH NEXT FROM vendor_cursor
INTO @SemanaActual,@SemanaAnterior
END
CLOSE vendor_cursor;
DEALLOCATE vendor_cursor;