For Example, say I have two tables
Product(id, name)
Company(id, name)
I want to loop through every product and every company in order to build a related table(already built so I would use an insert here).
CompanyProduct(companyId, productId)
I have never done a loop with TSQL. I know easily how to do this with LINQ, but TSQL should be faster for the amount of records I want to do this with.An
Seeing as I am looking at having to build upwards of 80,000 - 150,000 new records every time this is used what is the most efficient way I could go about this? Thanks for your help
答案 0 :(得分:2)
If you simply want a combination of every Product and Company, you are looking for a CROSS JOIN, like this:
INSERT INTO CompanyProduct
SELECT P.id, C.id FROM Product as P CROSS JOIN Company AS C;
This will be very fast.
Hope that helps,
Ash