Solved

How To Delete Mutiple Records Found Be

Posted on 2016-08-04
6
23 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
sql join/ assign small # first 10 80
using t-sql EXISTS 8 39
email the result out from a T-SQL queries 29 62
sql Total query 2 14
     When we have to pass multiple rows of data to SQL Server, the developers either have to send one row at a time or come up with other workarounds to meet requirements like using XML to pass data, which is complex and tedious to use. There is a …
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

930 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

12 Experts available now in Live!

Get 1:1 Help Now