Solved

How To Delete Mutiple Records Found Be

Posted on 2016-08-04
6
21 Views
Last Modified: 2016-10-08
I will try to make this as clear as possible.  My main objective is to "Identify" and then "Delete" records found between two tables.  I've solved the first part of "Identifying" the records that needs to be deleted, however, I'm a little stuck on how to delete the 370K records found.  The below query is what I use to locate the records:  It works fine.

SELECT TABLE1.COLUMN1 AS ACCOUNTS, TABLE2.COLUMN1 AS COMPANY_HQ
FROM TABLE1, TABLE2
WHERE TABLE1.ACCOUNT + '%%%%%%' LIKE '%%%%%%' + TABLE2.COMPANY_HQ + '%%%%%%'
ORDER BY TABLE2.COMPANY_HQ ASC;

(see attached file for results)

Now...I'm using the below query to delete the records found in Table2:

DELETE FROM TABLE2
WHERE TABLE2.ACCOUNT + '%%%%%%' LIKE '%%%%%%' + TABLE1.COMPANY_HQ + '%%%%%%'

BUT...I get the below error message:

Msg 4104, Level 16, State1, Line 2
The multi-part identifier "TABLE2.COMPANY_HQ" could not be bound.

I apologize for any confusion.  Any assistance in this matter would be greatly appreciated.
SQL-Result.PNG
0
Comment
Question by:Member_2_7970395
  • 2
  • 2
6 Comments
 
LVL 65

Assisted Solution

by:Jim Horn
Jim Horn earned 250 total points
ID: 41742826
Both queries you posted are missing the JOIN ... ON criteria that tells SQL Server how the two tables are related.

<knee-jerk answer> You're missing the JOIN in your DELETE Table, so assming just id..
DELETE Table2
FROM TABLE2 t2
  JOIN Table1 t1 ON t2.id = t2.id
WHERE TABLE2.ACCOUNT + '%%%%%%' LIKE '%%%%%%' + TABLE1.COMPANY_HQ + '%%%%%%'

Open in new window

For DELETE with JOIN formatting help check out this Pinal Dave blog.
1
 
LVL 20

Accepted Solution

by:
Russ Suter earned 250 total points
ID: 41742977
The clearest way (I think) to do this is to use a subquery. I don't mess around with DELETE statements. I want them to be as clear as possible. What I like about this approach is that you can run the SELECT statement independently to verify that what you're about to delete is correct. The syntax looks something like this:
DELETE FROM
  Table2
WHERE
  Table2.Table1ForeignKey IN
  (
  SELECT
    Table1.PrimaryKey
  FROM
    Table1
  WHERE
    SomeCondition = True
  )

Open in new window

1
 

Author Comment

by:Member_2_7970395
ID: 41743067
It worked!  Thank you very much!!
0
 

Author Comment

by:Member_2_7970395
ID: 41743098
Don't if I'm entering this correctly, but if not, I want to THANK all of you who responded.
0
 
LVL 20

Expert Comment

by:Russ Suter
ID: 41743124
Glad we could help.

Looks like you're trying to close this question. I think they've made that more confusing. You can either click "Close Question" at the bottom of your browser or click "Best Answer" in the comment you want to accept. You can then edit to accept multiple solutions.

Happy SQLing!
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now