1

I am trying to insert a non duplicate records into a table but which doing so I am unable to insert it it gives me (0 row(s) affected). And actually the table itself is empty and I am insert new records. To ignore inserting duplicate records I am doing it in the query which I have added below.

Insert Into OrderDetails (CustomerID, CustomerName, OrderID, OrderDate, ProductID, ProductName, Unit, Price)
SELECT c.CustomerID, c.CustomerName, i.OrderID, i.OrderDate, p.ProductID, p.ProductName, p.Unit, p.Price
FROM Customers c
INNER JOIN items i ON c.CustomerID=i.CustomerID
INNER JOIN itemsDetails id ON i.OrderID = id.OrderID
INNER JOIN Products P ON p.ProductID = id.ProductID
Inner Join OrderDetails od ON c.CustomerID = od.CustomerID
where c.CustomerID != od.CustomerID

Am I Inserting correctly?

2 Answers2

0

try:

Insert Into OrderDetails (CustomerID, CustomerName, OrderID, OrderDate, ProductID, ProductName, Unit, Price)
SELECT c.CustomerID, c.CustomerName, i.OrderID, i.OrderDate, p.ProductID, p.ProductName, p.Unit, p.Price
FROM Customers c
INNER JOIN items i ON c.CustomerID=i.CustomerID
INNER JOIN itemsDetails id ON i.OrderID = id.OrderID
INNER JOIN Products P ON p.ProductID = id.ProductID
where c.CustomerID NOT IN (SELECT customerid FROM orderdetails)
LuckAss
  • 114
  • 7
0

The error lies in the two last rows of your statement:

Inner Join OrderDetails od ON c.CustomerID = od.CustomerID
where c.CustomerID != od.CustomerID

In the inner join, you require the CustomerID be the same. In the where clause, you require the CustomerID to be different.

Remove the inner join of the table OrderDetails.

Samuel Renold
  • 302
  • 1
  • 8