• aiki4ever-796329 (6/27/2013)


    /* I'm trying to change "not in" to "not exists" and I don't have the syntax quite right. The first statement updates multiple rows

    while the second statement updates 0 rows. This is for SQL Server. */

    update VehicleWorld set EstimatedDeletedDate=1

    where Vehicle_ID not in (select Vehicle_ID from CFS_Production..CORE_VehicleAdSearch ads)

    update VehicleWorld set EstimatedDeletedDate=1

    where not exists

    (select 1 from VehicleWorld car, CFS_Production..CORE_VehicleAdSearch ads

    where car.Vehicle_ID = ads.Vehicle_ID)

    One important point here is that you're selecting from VehicleWorld in the subquery and then joining your table you check to it. This means that it won't join to the outer VehicleWorld table, so your update won't work. The one below should do the update because the subquery is correlated to the outer table.

    update VehicleWorld

    set EstimatedDeletedDate = 1

    where not exists (select 1

    from CFS_Production..CORE_VehicleAdSearch ads

    where VehicleWorld.Vehicle_ID = ads.Vehicle_ID);

    All that being said, I completely agree with Lowell...the syntax that allows you to select before firing an update is a good safety measure to make sure you aren't surprised by firing the update.