Solved

MS SQL Syntax

Posted on 2013-12-03
6
177 Views
Last Modified: 2013-12-04
I am trying to delete orphan records where the parent has been deleted (there is no FK defined).

This works:

select * from tblBVMeterReadingErrors a where not exists (Select 0 from tblBVMeterReadings b
where a.BV_Meter_Reading_ID = b.BV_Meter_Reading_ID);

But this doesn't:

delete from tblBVMeterReadingErrors a where not exists (Select 0 from tblBVMeterReadings b
where a.BV_Meter_Reading_ID = b.BV_Meter_Reading_ID);

What is wrong please?
0
Comment
Question by:slimguy
6 Comments
 
LVL 15

Expert Comment

by:gplana
ID: 39693901
Try this way:

delete from tblBVMeterReadingErrors where BV_Meter_Reading_ID NOT IN (Select BV_Meter_Reading_ID from tblBVMeterReadings
where b.BV_Meter_Reading_ID IS NOT NULL);

Open in new window

0
 
LVL 15

Accepted Solution

by:
gplana earned 100 total points
ID: 39693905
Sorry, I made a mistake: please remove the b.  from my previous solution:

delete from tblBVMeterReadingErrors where BV_Meter_Reading_ID NOT IN (Select BV_Meter_Reading_ID from tblBVMeterReadings
where BV_Meter_Reading_ID IS NOT NULL);

Open in new window

0
 
LVL 6

Expert Comment

by:ButlerTechnology
ID: 39693906
Select * from tblBVMeterReadingErros 
where bv_meterReading_ID not in (Select bv_MeterReadingID from tblBVMeterReading)

Delete from tblBVMeterReadingErros 
where bv_meterReading_ID not in (Select bv_MeterReadingID from tblBVMeterReading)

Open in new window


The above code should work.
Tom
0
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
LVL 15

Expert Comment

by:gplana
ID: 39693927
ButlerTechnology, this is exactly my solution. I just only  add a filter to avoid NULL values which some times could cause problems in comparisions inside the NOT IN.
0
 
LVL 9

Expert Comment

by:QuinnDex
ID: 39693943
this should do it for you check you are getting the correct results with the select before using the delete

select * from tblBVMeterReadingErrors where not in(Select 0 from tblBVMeterReadings
where tblBVMeterReadingErrors .BV_Meter_Reading_ID = tblBVMeterReadings.BV_Meter_Reading_ID);

Open in new window




delete from tblBVMeterReadingErrors where not in(Select 0 from tblBVMeterReadings
where tblBVMeterReadingErrors .BV_Meter_Reading_ID = tblBVMeterReadings.BV_Meter_Reading_ID);

Open in new window

0
 

Author Closing Comment

by:slimguy
ID: 39694870
Many thanks, I should have seen that myself - got a mental block last night!
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

813 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