Skip to content
Advertisement

inserting into table with conditions

SQL. i have two tables residents (primary key rid) and dependents (primary key did). dependents is connected to residents through rid. I want to insert rows into dependents table only if the rid in the insert statement matches with any of the rid in the residents table. i tried the code below, but there is some error with the where clause.

Advertisement

Answer

To my mind, it doesn’t make sense to insert a value that already exists, so I think that you might want actually want update:

If you want insert, then use insert . . . select:

If you want to insert all residents, then:

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement