Ver Mensaje Individual
  #10 (permalink)  
Antiguo 05/02/2009, 17:29
Santi-Sfe
 
Fecha de Ingreso: febrero-2009
Mensajes: 9
Antigüedad: 15 años, 4 meses
Puntos: 0
Respuesta: Consulta NOVATO MSSQL

Así es, aca hay algo que hice ayer con cursores y transacciones en MSSQL, es pequeño pero sirve para expresar la idea.


BEGIN TRANSACTION

DECLARE curTit CURSOR
FOR SELECT title,type,price,ytd_sales FROM titles
FOR UPDATE

OPEN curTit


SET NOCOUNT ON

DECLARE @va integer,
@tipo varchar(20),
@titulo varchar(20),
@precio money
SET @va = 0

FETCH NEXT FROM curTit INTO @titulo,@tipo,@precio,@va

WHILE @@fetch_status = 0
BEGIN
IF @@error <> 0
BEGIN
ROLLBACK TRANSACTION
RETURN
END
IF @va > 1000 UPDATE titles SET price = price * 0.9 WHERE CURRENT OF curTit
ELSE
BEGIN
IF @tipo = 'popular_comp' OR @titulo LIKE ('%computer%') UPDATE titles SET price = price * 0.5 WHERE CURRENT OF curTit
ELSE UPDATE titles SET price = price * 0.75 WHERE CURRENT OF curTit
END
FETCH NEXT FROM curTit INTO @titulo,@tipo,@precio,@va
END

CLOSE curTit

DEALLOCATE curTit

COMMIT TRANSACTION

GO


Faltan validaciones, pero la idea general es esa.