Click here to Skip to main content
15,893,923 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
ALTER PROCEDURE Inventory
AS
BEGIN
INSERT INTO Inventory (FoodID, FoodName, ItemID, ItemName)
Select DISTINCT f.FoodID, f.FoodName, i.ItemID, I.Name from Food f CROSS JOIN Item i
WHERE f.FoodID=i.ItemID
END

How to avoid duplicate values of all.
Posted

avoid cross join. try to use inner join or outer join if possible use primary/foreign key relation between two tables and do join with that particular column..


something like below

SQL
SELECT     Table1.column1 , Table2.column2
FROM         Table1 INNER JOIN
                      Table2 ON Table1 .column1= Table2.column2
 
Share this answer
 
Comments
hushhush1 5-Feb-14 0:58am    
But there are no common column between Food and Item table, I just want to insert foodID from Food and itemid from item into Inventory table
ravikhoda 5-Feb-14 1:06am    
may be you can change your data structure then...
hushhush1 5-Feb-14 1:11am    
nope I cannot change. Is there any other solution?
Cross Join gives you all possible combinations of the two table. That is why you are getting duplicate rows. Check this - SQL Cross Joins[^].

You may want to use INNER JOIN[^] instead.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900