Tengo tres tablas Detalle_Compra
, Detalle_Venta
e Inventario
:
CREATE TABLE Detalle_Compra
(
Pedido int,
Producto int,
Cantidad int not null,
Precio int,
foreign key (Producto) references Productos,
foreign key (Pedido) references Pedidos,
Primary key (Pedido, Producto)
)
Go
CREATE TABLE Detalle_Venta
(
FacturaV int,
Producto int,
Cantidad int,
Precio int,
Primary Key (FacturaV, Producto),
foreign key (Producto) references Productos,
foreign key (FacturaV) references Facturas_V,
)
Go
CREATE TABLE Inventario
(
Factura int,
Operacion varchar (6) not null,
Producto int,
cantidad int not null,
Observacion Nvarchar (200),
Foreign key (Factura, Producto) references Detalle_Compra,
Foreign key (Factura, Producto) references Detalle_Venta,
Primary key (Factura, Operacion,Producto)
)
Go
Necesito un TRIGGER para que realice un registro en la tabla Inventario
cuando se realice un registro en la tabla Detalle_Compra
y/o Detalle_Venta
.
Ya trate de hacerlo pero este solo me deja hacer un inserción y luego me sale el mensaje:
Instrucción INSERT en conflicto con la restricción FOREIGN KEY "FK__Inventario__32E0915F". El conflicto ha aparecido en la base de datos "Inventario_Erikar", tabla "dbo.Detalle_Compra".
CREATE TRIGGER DIS_InventarioVenta ON Detalle_Venta
FOR INSERT
AS
DECLARE @Factura int
DECLARE @Operacion varchar (6)
DECLARE @Producto int
DECLARE @cantidad int
SELECT @Factura = i.FacturaV FROM Detalle_Venta i;
SELECT @Producto = i.Producto FROM Detalle_Venta i;
SELECT @cantidad = i.Cantidad FROM Detalle_Venta i;
INSERT INTO inventario (Factura, Operacion,Producto,cantidad,Observacion)
VALUES (@Factura,'', @Producto,@cantidad,'')
GO