Hola a todos, tengo la siguiente tabla
Tabla1
idReq (AUTONUMERICO, Clave)
idMat INT,
Marca NVARCHAR(70)
Color NVARCHAR(20)
Prov1 NVARCHAR(10)
Precio1 MONEY
Prov2 NVARCHAR(10)
Precio2 MONEY
Prov3 NVARCHAR(10)
Precio3 MONEY
VBObra BIT
Necesito crear un Trigger que me permita en caso de ingresar y/o editar un
insumo ya existente en la tabla (idMat, Marca, Color), me asigne el ultimo
Prov1, Precio1, Prov2, Precio2, Prov3, Precio3 asignado a ese insumo.
He Intentado con este trigger, pero no me resulta y no se como resolver el
caso de que se inserte un nuevo registro
CREATE TRIGGER CompletarPrecios ON [dbo].[tblLogistica1]
FOR UPDATE
AS
DECLARE @Prov1 NVARCHAR(10)
DECLARE @Prov2 NVARCHAR(10)
DECLARE @Prov3 NVARCHAR(10)
DECLARE @Precio1 MONEY
DECLARE @Precio2 MONEY
DECLARE @Precio3 MONEY
DECLARE @idMat INT
DECLARE @Marca NVARCHAR(70)
DECLARE @Color NVARCHAR(20)
DECLARE @idReq INT
SELECT @idReq=idReq, @idMat=idMaterial, @Marca=Marca,
@Color=Color FROM Inserted
SELECT TOP 1 @Prov1=Prov1, @Precio1=Precio1, @Prov2=Prov2, @Precio2 Precio2, @Prov3=Prov3, @Precio3=Precio3, @Prov4=Prov4, @Precio4=Precio4
FROM tblLogistica1 WHERE idMat=@idmat AND
Marca=@Marca AND Color=@Color AND idReq<>@idReq ORDER BY idReq DESC
IF @Precio1 IS NULL
set @Precio1=0
IF @Precio2 IS NULL
set @Precio2=0
IF @Precio3 IS NULL
set @Precio3=0
IF @Precio4 IS NULL
set @Precio4=0
IF UPDATE(VBObra)
BEGIN
UPDATE tblLogistica1 SET Prov1=@Prov1, Precio1=@Precio1, Prov2=@Prov2,
Precio2=@Precio2,
Prov3=@Prov3, Precio3=@Precio3 WHERE idReq=@idReq
END
Gracias por los aportes
Leer las respuestas