Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

MS SQL Syntax

Posted on 2013-12-03
6
Medium Priority
?
183 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 400 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
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 
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

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
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.

721 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