2010-11-24 3 views
0
create procedure sp_DescuentoCategoriaInvierno 
as 
declare @IDProductoOfertado int, @ProductoNombre nvarchar(256), @CategoriaNombre nvarchar(256), @SubcategoriaNombre nvarchar(256), @Precio float 

declare cursorProducto cursor for 
select o.IDProducto, p.Nombre, c.Nombre, s.Nombre, o.Precio from OfertaProducto as o 
inner join Producto as p on o.IDProducto = p.ID 
inner join Subcategoria as s on p.IDSubcategoria = s.ID 
inner join Categoria as c on s.IDCategoria = c.ID 
order by p.Nombre 

open cursorProducto 
    fetch next from cursorProducto into @IDProductoOfertado, @ProductoNombre, @CategoriaNombre, @SubcategoriaNombre, @Precio 
    while @@FETCH_STATUS = 0 
     begin 
      if(@CategoriaNombre='Invierno') 
      begin   
       select @Precio --TROUBLE IS HERE. 
       from OfertaProducto --WHAT SHOULD I DO? 
       update OfertaProducto set Precio = @Precio * 0.5     
      end 
      fetch next from cursorProducto into @IDProductoOfertado, @ProductoNombre, @CategoriaNombre, @SubcategoriaNombre, @Precio 
     end 

close cursorProducto 
deallocate cursorProducto 

Dies einfach genug ist, ich versuche nur jede OferredProduct in meinem dabase zu haben, die eine Kategorie von ‚invierno‘ hat einen reduzierten Preis:Ärger mit diesem einfachen Stored Procedure

Hier ist das Modell :

alt text

Also, was ich es durch jeden OfferedProduct iterieren möchte, wenn es eine Kategorie von ‚Invierno‘ reduzieren den Preis auf 50% aufweist. Ich vermisse etwas Kleines, da bin ich mir sicher. : P

Vielen Dank!

Antwort

2

Wie Jeff schon sagte, benötigen Sie keinen Cursor dafür, es wäre besser, nur eine UPDATE-Anweisung dafür. Versuchen Sie etwas wie folgt:

UPDATE o 
SET o.Precio = o.Precio * 0.5 
from OfertaProducto as o 
inner join Producto as p on o.IDProducto = p.ID 
inner join Subcategoria as s on p.IDSubcategoria = s.ID 
inner join Categoria as c on s.IDCategoria = c.ID 
WHERE c.Nombre = 'Invierno' 
0

ich das nicht verstehen:

select @Precio 
from OfertaProducto 
update OfertaProducto set Precio = @Precio * 0.5 

@Precio bereits durch den Cursor ausgewählt ist, also warum Sie sie erneut auswählen. Und Sie müssen auch eine ID in der Update-Anweisung angeben. Ansonsten werden alle Zeilen aktualisiert. Ich denke, das würde es tun:

update OfertaProducto set Precio = @Precio * 0.5 where IDProducto = @IDProductoOfertado 
0

Sie haben nicht die Select-Anweisung benötigen, aber Sie tun müssen, eine where-Klausel auf dem Update-Anweisung:

update OfertaProducto set Precio = @Precio * 0.5 
where [email protected] 

Beachten Sie, dass dies mit einem besseren erreicht werden könnte, einzelne UPDATE-Anweisung, die auf den gesamten Satz gleichzeitig wirkt

+0

In Ihrem Beispiel ist IDProducto der Fremdschlüssel in meiner rechten Tabelle? –

+0

IDProducto ist das Feld in der OfertaProducto-Tabelle. @IDProductOfertado ist die Variable, die Sie bereits in Ihrer gespeicherten Prozedur definiert haben und die in der FETCH-Anweisung für den Cursor verwendet wird. –