I am trying to insert all the rows of cart Table of userId=1 into Order and OrderItem table the Order table consists of following columns:
ORDER TABLE
1.OrderId (int)
2.OrderDate(datetime)
3.userId (int)
4.orderStatus (varchar)
the user_tbltable is reference to the Order table with PK of userId and
cart table is also reference with userId
CART TABLE
Id
userId
Quantity
productID
ORDERITEM TABLE
id
orderid
productid
qty
ITEM Table
ProductId
Name
SizeId
ColorId
Price
Imageurl
I have tried following but this giving me conflict error:
The INSERT statement conflicted with the FOREIGN KEY constraint "FK_OrderItem_Item". The conflict occurred in database "db", table "dbo.Item", column 'Id'
insert into [Order] values(getdate(),@userId,@status);
set @orderId = @@IDENTITY
insert into OrderItem (OrderId ,ItemID,Qty,ColorId,SizeId)
select @orderId,Cart.ProductId,cart.Quantity,cart.ColorId,cart.SizeId
from [Cart]
where UserId=@userId ;