SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Delete ALL records where table join produces values


Delete ALL records where table join produces values

Author
Message
DerbyNeal
DerbyNeal
Old Hand
Old Hand (378 reputation)Old Hand (378 reputation)Old Hand (378 reputation)Old Hand (378 reputation)Old Hand (378 reputation)Old Hand (378 reputation)Old Hand (378 reputation)Old Hand (378 reputation)

Group: General Forum Members
Points: 378 Visits: 909
Hi,

I need to delete ALL records from 'Column 0' in table 'dbo.abn_extract' if the following statement is true/returns a value.

SELECT E.[Column 0] FROM dbo.abn_extract E JOIN dbo.ABN_File_Header H ON
SUBSTRING(E.[Column 0], 1, 13) = H.Header_Id



Any help please?

Thanks in advance,

Neal
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (116K reputation)SSC Guru (116K reputation)SSC Guru (116K reputation)SSC Guru (116K reputation)SSC Guru (116K reputation)SSC Guru (116K reputation)SSC Guru (116K reputation)SSC Guru (116K reputation)

Group: General Forum Members
Points: 116925 Visits: 41440
Untested, of course... Wink
 DELETE dbo.abn_extract 
FROM dbo.abn_extract E
INNER JOIN dbo.ABN_File_Header H
ON SUBSTRING(E.[Column 0], 1, 13) = H.Header_Id



SQL Server is one of the few RDBMS's that have a FROM clause (2 actually) available... everyone else has to use correlated sub-queries. Lookup DELETE in Books Online for a better understanding of the statement.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
DerbyNeal
DerbyNeal
Old Hand
Old Hand (378 reputation)Old Hand (378 reputation)Old Hand (378 reputation)Old Hand (378 reputation)Old Hand (378 reputation)Old Hand (378 reputation)Old Hand (378 reputation)Old Hand (378 reputation)

Group: General Forum Members
Points: 378 Visits: 909
Thanks Jeff,

Your solution deletes the 1 record in the dbo.abn_extract table that exists in the WHERE clause... However, I need to delete ALL records in the dbo.abn_extract table where there is a match.
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (116K reputation)SSC Guru (116K reputation)SSC Guru (116K reputation)SSC Guru (116K reputation)SSC Guru (116K reputation)SSC Guru (116K reputation)SSC Guru (116K reputation)SSC Guru (116K reputation)

Group: General Forum Members
Points: 116925 Visits: 41440
How did you verify that? Is it just because you got a rows affected of 1? Perhaps there was only one row that met the condition?

What do you get when you run your original query of...

SELECT E.[Column 0] FROM dbo.abn_extract E JOIN dbo.ABN_File_Header H ON
SUBSTRING(E.[Column 0], 1, 13) = H.Header_Id



Sometimes, the devil is in the data... please read and heed the link in my signature line to get better help quicker. Thanks, Neal.

--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
DerbyNeal
DerbyNeal
Old Hand
Old Hand (378 reputation)Old Hand (378 reputation)Old Hand (378 reputation)Old Hand (378 reputation)Old Hand (378 reputation)Old Hand (378 reputation)Old Hand (378 reputation)Old Hand (378 reputation)

Group: General Forum Members
Points: 378 Visits: 909
My original query is fine...

There is NO problem with the data.

In both mine and your queries there is (currently) one record match.

What I'm saying is that if there is a match (a value is returned) then delete ALL records from the dbo.abn_extract table.

If
SUBSTRING(E.[Column 0], 1, 13) = H.Header_Id

is true on one dbo.abn_extract record then I need to delete ALL other [Column 0] records as well (even though only 1 [Column 0] record matches).
ChrisM@Work
ChrisM@Work
SSC-Insane
SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)SSC-Insane (21K reputation)

Group: General Forum Members
Points: 21987 Visits: 19709
Hi Neal

I think there's some confusion here because of this...

delete ALL records from 'Column 0'


and this

delete ALL other [Column 0] records


I reckon what you mean to say is this: if a matching row is found in dbo.ABN_File_Header, then delete all rows in dbo.abn_extract, otherwise don't delete any.
The all-or-nothing nature of this requirement suggests the use of IF:

IF (SELECT COUNT(*) 
FROM dbo.abn_extract E
JOIN dbo.ABN_File_Header H
ON SUBSTRING(E.[Column 0], 1, 13) = H.Header_Id) > 0
BEGIN
DELETE FROM dbo.abn_extract
END



“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
DerbyNeal
DerbyNeal
Old Hand
Old Hand (378 reputation)Old Hand (378 reputation)Old Hand (378 reputation)Old Hand (378 reputation)Old Hand (378 reputation)Old Hand (378 reputation)Old Hand (378 reputation)Old Hand (378 reputation)

Group: General Forum Members
Points: 378 Visits: 909
Thanks Chris,

Thats excellent, Just the job!
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search