sqlsql-serverinsertsql-server-2008-r2associative-table

SQL insert to fill an associative table with conditions


I have two tables and an associative table between them (let's call them Tab1, Tab2 and ATab).

Tab1 and Tab2 have those very same fields (for example purpose) :

In my ATab, I want to insert record to associates Tab1 and Tab2 with their Ids.

In order to do so, I would like to write my query in a sql script that say something like :

I can manage to do something like :

INSERT INTO ATab(Tab1Id, Tab2Id) 
SELECT Tab1.Id, ????? 
FROM Tab1 WHERE Tab1.Name='Foo';

But I'm selecting only the Foo record of my first table...

How would I manage to perform a "double" where clause ? Is it possible ?


Solution

  • by using AND

    INSERT INTO ATab(Tab1Id, Tab2Id) 
    (SELECT Tab1.Id, Tab2.Id
    FROM Tab1, Tab2 WHERE Tab1.Name = 'Foo' AND Tab2.Name = 'Bar')