Article View: borland.public.interbase.sql
Article #11349FOR SELECT LOOP
From: Werner Debrie
Date: Sat, 23 Nov 2002 17:07
Date: Sat, 23 Nov 2002 17:07
72 lines
3006 bytes
3006 bytes
Somewhere in this newsgroup, a solution for merging two query's resulting in
one table (where the result of the queries is in different column of the
table) was to use a FOR SELECT LOOP in a procedure.
I tried this because it looked like it was the ideal solution for a problem
that I had.
So I tried this;
(Invoices is a table that hold the number and date of the invoices.
InvoiceD holds the detail of the invoices and has a returncode field. I
want to make a total of the different returncodes for a period grouped by
date)
CREATE PROCEDURE TESTFORLOOP
RETURNS (
TOTAL3 NUMERIC(11,2),
TOTAL2 NUMERIC(15,2),
TOTAL1 NUMERIC(15,2),
DAT DATE)
AS
begin
/* Procedure Text */
FOR SELECT DISTINCT Invoices.date
FROM Invoices
WHERE Invoices.month= '200209'
INTO :DAT
do
begin
select SUM(InvoiceD.total)
from Invoices Join InvoiceD ON Invoices.invnum = InvoiceD.number
where Invoices.date = :DAT and InvoiceD.returnc = 1
GROUP BY Invoices.date
INTO :TOTAL1;
end
begin
select SUM(InvoiceD.total)
from InvoicesJoin InvoiceD ON Invoices.invnum = InvoiceD.InvoiceD.number
where Invoices.date = :DAT and InvoiceD.returnc = 2
GROUP BY Invoices.date
INTO :TOTAL2;
end
begin
select SUM(InvoiceD.total)
from Invoices Join InvoiceD ON Invoices.invnum =
InvoiceD.InvoiceD.number
where Invoices.date = :DAT and InvoiceD.returnc = 3
GROUP BY Invoices.date
INTO :TOTAL3;
end
end
I end up with one row with the correct data, altough there have to be 19
rows.
If I use only the first subquery and at 'suspend' it works.
If I add suspend to all the subqueries , I just have one row
It's like the 2nd and 3rd subqueries aren't procesed (with or without
'suspend').
In the data definition guide the explanation for a FOR SELECT is:
repeats the statement or block following DO for every qualifying row
retrieved by the SELECT statement.
So it should work!
What am I doing wrong?
Werner Debrie
Technical Details
Message-ID:
<3ddfb5ea$1@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-01!sn-xit-08!supernews.com!newshosting.com!news-xfer1.atl.newshosting.com!proxad.net!proxad.net!fr.clara.net!heighliner.fr.clara.net!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 114a0e,93dd659978b88a04,start X-Google-Attributes: gid114a0e,public X-Google-ArrivalTime: 2002-11-23 09:10:23 PST From: "Werner Debrie" <werner.debrie@pandora.be> Newsgroups: borland.public.interbase.sql Subject: FOR SELECT LOOP Date: Sat, 23 Nov 2002 18:07:55 +0100 Lines: 74 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2600.0000 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000 NNTP-Posting-Host: 213.224.178.24 Message-ID: <3ddfb5ea$1@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1038071274 213.224.178.24 (23 Nov 2002 09:07:54 -0800) Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-01!sn-xit-08!supernews.com!newshosting.com!news-xfer1.atl.newshosting.com!proxad.net!proxad.net!fr.clara.net!heighliner.fr.clara.net!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail