这是脚本:
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
我在@NumeroPelicula上计数(d.ID)后第6行出错:
Msg 102,Level 15,State 1,Procedure sp_DescuentoAlquiler,第6行不正确 '@NumeroPelicula'附近的语法。
有什么建议吗?
答案 0 :(得分:1)
从计数的列别名中删除@。
select a.ID, count(d.ID) as NumeroPelicula
from Alquiler a inner join DetalleAlquiler d on a.ID = d.IDAlquiler
group by a.ID
答案 1 :(得分:0)
尝试删除
下方的@符号declare cursorAlquiler cursor for select a.ID, count(d.ID) as @NumeroPelicula
应该是
declare cursorAlquiler cursor for select a.ID, count(d.ID) as NumeroPelicul
答案 2 :(得分:0)
我需要确保数据示例(并且需要彻底测试),但这看起来似乎可能以基于集合的方式完成工作。由于性能问题,游标是这种处理的一个非常糟糕的选择,特别是当数据集变大时。
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