I have three tables 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
I need a TRIGGER to make a record in the Inventario
table when a record is made in the table Detalle_Compra
and / or Detalle_Venta
.
I already tried to do it but this one just lets me do an insert and then I get the message:
INSERT statement in conflict with the FOREIGN KEY constraint "FK__Inventory__32E0915F". The conflict has appeared in the database "Inventario_Erikar", table "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