Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Converting "not in" to "not exists" Expand / Collapse
Author
Message
Posted Thursday, June 27, 2013 8:58 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, August 28, 2014 10:07 AM
Points: 9, Visits: 82

/* 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)
Post #1468207
Posted Thursday, June 27, 2013 9:03 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 6:48 AM
Points: 12,915, Visits: 32,075
i prefer to use aliases for my updates, because the format for SQL makes it easy to include a SELECT statement very easily, which lets me do a quick Sanity Check by running the SELECT portion with the identical criteria.

how does this work for you?

UPDATE MyTarget
SET MyTarget.EstimatedDeletedDate = 1
--highlight and execute starting at the SELECT
--SELECT MyTarget.EstimatedDeletedDate,*
FROM VehicleWorld MyTarget
WHERE NOT EXISTS
(SELECT 1 FROM VCFS_Production..CORE_VehicleAdSearch ads
WHERE MyTarget.Vehicle_ID = ads.Vehicle_ID)
AND MyTarget.EstimatedDeletedDate <> 1



Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1468211
Posted Thursday, June 27, 2013 9:47 AM
SSCarpal Tunnel

SSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal TunnelSSCarpal Tunnel

Group: General Forum Members
Last Login: Today @ 11:20 AM
Points: 4,316, Visits: 3,358
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.



Tally Tables - Performance Personified
String Splitting with True Performance
Best practices on how to ask questions
Post #1468237
Posted Thursday, June 27, 2013 10:32 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, August 28, 2014 10:07 AM
Points: 9, Visits: 82
I see my problem. I had two tables in my subquery instead of one. That syntax is certainly confusing. Thank you gentlemen for your kind assistance. I'm quite glad that there are people in the world who understand SQL better than I do and who are willing to spread their knowledge.
Post #1468256
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse