Article View: borland.public.interbase.sql
Article #824Re: DELETEing orphaned records
From: Van den Wouwer Danny
Date: Sun, 13 May 2007 08:46
Date: Sun, 13 May 2007 08:46
64 lines
2748 bytes
2748 bytes
Hi Paul,
You can't use joins in a delete:
I have 2 solutions in mind:
1) DELETE
FROM "Tasks" T
WHERE NOT EXISTS (
SELECT *
FROM "Servers" S
WHERE S."ClientName" = T."ClientName"
AND S."ServerName" = T."ServerName")
--> This isn't the fasted way to go, because the table "Tasks" will be
scanned naturally.
2) Write a small stored procedure
create procedure delete_records_example
as
declare variable db_key CHAR(8); /* Or use PK of "Tasks" */
begin
for
select T.rdb$db_key
from "Tasks" T
left outer join "Servers" S on
S."ClientName" = T."ClientName"
and S."ServerName" = T."ServerName"
where S."ClientName" is null
and S."ServerName" is null
into db_key
/* Normally I test if PK of S is null, but you didn't gave this
information. */
do begin
delete
from "Tasks" T
where T.rdb$db_key = :db_key;
end
end
Normally this is the fasted way do delete record with complex logic. Of
course it depends on the execution of your select statement.
Van den Wouwer Danny
Peopleware NV
> LEFT OUTER JOIN "Servers" S
> ON S."ClientName"=T."ClientName" AND S."ServerName"=T."ServerName"
> WHERE S."ClientName" IS NULL AND S."ServerName" IS NULL
Paul Embleton wrote:
> I need to delete records from one table, for which there are no
> corresponding records in a second table. I get 'token unknown LEFT' with
> the following:
>
> DELETE FROM "Tasks" T
> LEFT OUTER JOIN "Servers" S
> ON S."ClientName"=T."ClientName" AND S."ServerName"=T."ServerName"
> WHERE S."ClientName" IS NULL AND S."ServerName" IS NULL
>
> tia
> Paul
Technical Details
Message-ID:
<4646d064$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news1.google.com!news.glorb.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mailReferences:
<4646cba7@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Thread: 114a0e,917a31690354dae3 X-Google-Attributes: gid114a0e,public X-Google-Language: ENGLISH,ASCII-7-bit Date: Sun, 13 May 2007 10:46:27 +0200 From: Van den Wouwer Danny <danny.van.den.wouwer@pandora.be> User-Agent: Thunderbird 1.5.0.10 (Windows/20070221) MIME-Version: 1.0 Newsgroups: borland.public.interbase.sql Subject: Re: DELETEing orphaned records References: <4646cba7@newsgroups.borland.com> In-Reply-To: <4646cba7@newsgroups.borland.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Antivirus: avast! (VPS 000739-3, 11/05/2007), Outbound message X-Antivirus-Status: Clean NNTP-Posting-Host: 81.164.43.116 Message-ID: <4646d064$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1179045988 81.164.43.116 (13 May 2007 01:46:28 -0700) Lines: 64 Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news1.google.com!news.glorb.com!newsfeeds.ihug.co.nz!ihug.co.nz!newsgroups.borland.com!not-for-mail