Delete SQL

jstevens
GoldLounger
Posts: 2628
Joined: 26 Jan 2010, 16:31
Location: Southern California

Delete SQL

Post by jstevens »

I can successfully delete all records from table_A based on records in table_main. My challenge is when I try to delete records in table_A based on a processing date in table_main.

Example code:

This works but deletes all records in table_A
DELETE FROM table_A WHERE EXISTS (SELECT DISTINCT [Item] FROM table_main WHERE [Item] = table_A.[Item])

I would like to delete records with a specific processing data captured in table_main. The common field between table_main and table_A is the [Item] field. I was thinking of something like this:

DELETE FROM table_A WHERE EXISTS (SELECT DISTINCT [Item] FROM table_main WHERE [Item] = table_A.[Item] AND table_main.[ProcessingDate] = 7/24/2014)


Thanks for taking a look,
John
Regards,
John

User avatar
HansV
Administrator
Posts: 78447
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Delete SQL

Post by HansV »

Literal dates must be enclosed in #s:

... AND table_main.[ProcessingDate] = #7/24/2014#)

Alternatively:

DELETE Item FROM table_A WHERE Item In (SELECT Item FROM table_Main WHERE ProcessingDate = #7/24/2014#)
Best wishes,
Hans

jstevens
GoldLounger
Posts: 2628
Joined: 26 Jan 2010, 16:31
Location: Southern California

Re: Delete SQL

Post by jstevens »

Hans,

Your suggestion worked.

Thank you,
John
Regards,
John