0

Here's the script:

create procedure sp_DescuentoAlquiler
as
declare @IDAlquiler int, @NumeroPelicula int, @MontoTotal float

declare cursorAlquiler cursor for
select a.ID, count(d.ID) as @NumeroPelicula 
from Alquiler a inner join DetalleAlquiler d on a.ID = d.IDAlquiler
group by a.ID

open cursorAlquiler
    fetch next from cursorAlquiler into @IDAlquiler, @NumeroPelicula
    while @@FETCH_STATUS = 0
        begin
            if(@NumeroPelicula >= 3)
            begin
                select @MontoTotal = SUM(d.PrecioAlquiler)
                from DetalleAlquiler d where d.IDAlquiler = @IDAlquiler
                update Alquiler set MontoTotal = @MontoTotal * 0.3
                where ID = @IDAlquiler
            end
            fetch next from cursorAlquiler into @IDAlquiler, @NumeroPelicula
        end

close cursorAlquiler
deallocate cursorAlquiler

I'm getting an error in Line 6 after count(d.ID), on @NumeroPelicula:

Msg 102, Level 15, State 1, Procedure sp_DescuentoAlquiler, Line 6 Incorrect syntax near '@NumeroPelicula'.

Any suggestions?

1
  • This of course is something that should not be done in a cursor. Commented Nov 24, 2010 at 15:57

3 Answers 3

1

Remove the @ from the column alias for your count.

select a.ID, count(d.ID) as NumeroPelicula 
from Alquiler a inner join DetalleAlquiler d on a.ID = d.IDAlquiler
group by a.ID
Sign up to request clarification or add additional context in comments.

Comments

0

Try removing the @ symbol below

declare cursorAlquiler cursor for 
select a.ID, count(d.ID) as @NumeroPelicula 

should be

declare cursorAlquiler cursor for 
select a.ID, count(d.ID) as NumeroPelicul

Comments

0

I'd need data examples to be sure (anf thorough testing) but this seems as if it might do the job in a set-based manner. Cursors are a very bad choice for this kind of processing due to performance problems especially as the data set gets large.

   update A 
   set MontoTotal = sum(d.PrecioAlquiler) * 0.3 
   From Alquiler A
   join (select a.ID, count(d.ID) as NumeroPelicula  
            from Alquiler a inner join DetalleAlquiler d on a.ID = d.IDAlquiler 
            group by a.ID ) b
    on a.id = b.id
   JOIN DetalleAlquiler d 
    ON d.IDAlquiler = b.ID
   where b.NumeroPelicula  >=3

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.