Ошибка при создании скрипта SQL - PullRequest
0 голосов
/ 24 ноября 2010

Вот сценарий:

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

Я получаю сообщение об ошибке в строке 6 после подсчета (d.ID) на @NumeroPelicula:

Сообщение 102, уровень15, состояние 1, процедура sp_DescuentoAlquiler, строка 6 Неверный синтаксис рядом с'@NumeroPelicula '.

Есть предложения?

Ответы [ 3 ]

1 голос
/ 24 ноября 2010

Удалите @ из псевдонима столбца для вашего счета.

select a.ID, count(d.ID) as NumeroPelicula 
from Alquiler a inner join DetalleAlquiler d on a.ID = d.IDAlquiler
group by a.ID
0 голосов
/ 24 ноября 2010

Мне нужны примеры данных, чтобы быть уверенным (и тщательного тестирования), но похоже, что это может сделать работу на основе набора. Курсоры - очень плохой выбор для такого рода обработки из-за проблем с производительностью, особенно когда набор данных становится большим.

   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
0 голосов
/ 24 ноября 2010

Попробуйте удалить символ @ ниже

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

должно быть

declare cursorAlquiler cursor for 
select a.ID, count(d.ID) as NumeroPelicul
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...