Home Forums SQL Server 2017 SQL Server 2017 - Development Delete rows based on a second table RE: Delete rows based on a second table
February 19, 2018 at 10:16 am
Phil Parkin - Monday, February 19, 2018 8:23 AMxxx-593414 - Monday, February 19, 2018 8:02 AMI have two Tables (In Ms Sql Server)
Table Name Column Names
MasterCities Countryy City
VisitedCities Country CityI want to delete from the MasterCities table any Country/Cities I have visited
I don't have any primary keys as the second table is coming from an outside source so need to be able to match by Coountry and City in both tables
So I want to end up , in the MasterCities table, all the Countries/Cities that originally exist in the Master Table, except those that are in the VisitedCities
Have tried various selects where not in etc. but cannot get syntax right or its deleting too much stuffSomething like this (untested)
delete mc
from MasterCities mc
where exists (select 1 from VisitedCities vc where vc.Country = mc.Country and vc.City = mc.City)
That's perfect and worked fine
Many thanks