ᴺᴱᴼᴺ
ᴺᴱᴼᴺ

Reputation: 35

Complexity of inserting

ERD

My issue with an old customer plus outdated system.Part of issue. The 3 tables contain existing data in database. Let me explain, what was the scenario and then I hope you guys can help me get this job done.

Scenario :

Explanation :

If i understand correctly i have to massive insert ProductZone and in this case by complicate T-SQL both filter condition and insert statement.

I'd be glad to take any suggestions,Thanks in advance.

Upvotes: 2

Views: 175

Answers (1)

Romil Kumar Jain
Romil Kumar Jain

Reputation: 20745

This query will populate the Zones for existing products available in ProductZone Table and the new products available in Product table.

INSERT   INTO ProductZone 
SELECT ZoneNo, 
       ProductNo 
FROM   Product a 
       CROSS JOIN ZONE b 
WHERE  NOT EXISTS (SELECT 1 
                   FROM   ProductZone c 
                   WHERE  a.ProductNo = c.ProductNo 
                          AND a.zone = c.zone) 

Upvotes: 1

Related Questions