Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 turn a select to a delete

Author  Topic 

WJHamel
Aged Yak Warrior

651 Posts

Posted - 2012-01-24 : 14:12:05
So i'm using the following query to identify entries, based on unique value, which are in one table but not another:

select * FROM civil.dbo.ref2table t1
WHERE receiptid NOT IN (SELECT receiptid FROM civil.dbo.reftable t2 WHERE t2.receiptid=t1.receiptid)

My intent was to then delete those results from that table, leaving behind the entries i want. It seemed like i would either use:

DELETE * FROM civil.dbo.ref2table t1
WHERE receiptid NOT IN (SELECT receiptid FROM civil.dbo.reftable t2 WHERE t2.receiptid=t1.receiptid)

or

DELETE FROM civil.dbo.ref2table t1
WHERE receiptid NOT IN (SELECT receiptid FROM civil.dbo.reftable t2 WHERE t2.receiptid=t1.receiptid)

Neither works. Syntax.

Where am i going wrong?

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-01-24 : 14:19:21
DELETE t1
FROM civil.dbo.ref2table t1
WHERE receiptid NOT IN (SELECT receiptid FROM civil.dbo.reftable t2 WHERE t2.receiptid=t1.receiptid)

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page
   

- Advertisement -