Subject Re: SP syntax problems
Author Alexander V.Nevsky
--- In ib-support@y..., Alan J Davies <Aldis@c...> wrote:
> CREATE PROCEDURE Ajd_AUTOALLOCATE
> (
> LEDGERTYPE CHAR(10)
> )
> AS
> Declare Variable LedgerAcno CHAR(5);
> Declare Variable DEBITTOTAL NUMERIC(9, 2);
> Declare Variable CREDITTOTAL NUMERIC(9, 2);
> begin
> If (Ledgertype='Sales ') then
> begin
> For Select Acno
> From Sledger
> Where Balance=0
> into :LedgerAcno
> do
> begin
> For Select Sum(Total)
> From Sposting
> Where Acno=:LedgerAcno
> And Dr_Cr='D'
> into :DEBITTOTAL
> do
> suspend;
> For Select Sum(Total)
> From Sposting
> Where Acno=:LedgerAcno
> And Dr_Cr='C'
> into :CREDITTOTAL
> do
> suspend;
> end
> if (:DEBITTOTAL-:CREDITTOTAL=0) then
> begin
> Update Sposting
> Set Paid='Y',Total=0
> Where Acno=:LedgerAcno;
> end
> end
> end

Alan,
a) Suspends are useless here - they are used to stop procedure, return
values of output parameters gained at this point and resume procedure
running. You have no any output parameters there.
b) In Delphi code you make update within loop on Sledger, why did you
decided to place it after loop is finished in SP?
c) For Select Sum(Total)
From Sposting
Where Acno=:LedgerAcno
And Dr_Cr='D'
into :DEBITTOTAL
will return singular result - sum of Total column in all records which
conforms to condition in Where. So, For is superfluous here.
d) Usage of colons with variables is needed inside SQL statements
only, in SP code it is not needed.

I think you want:
CREATE PROCEDURE Ajd_AUTOALLOCATE
(
LEDGERTYPE CHAR(10)
)
AS
Declare Variable LedgerAcno CHAR(5);
Declare Variable DEBITTOTAL NUMERIC(9, 2);
Declare Variable CREDITTOTAL NUMERIC(9, 2);
begin
If (Ledgertype='Sales ') then
begin
For Select Acno
From Sledger
Where Balance=0
into :LedgerAcno
do
begin
Select Sum(Total)
From Sposting
Where Acno=:LedgerAcno
And Dr_Cr='D'
into :DEBITTOTAL

Select Sum(Total)
From Sposting
Where Acno=:LedgerAcno
And Dr_Cr='C'
into :CREDITTOTAL

if (DEBITTOTAL-CREDITTOTAL=0) then
Update Sposting
Set Paid='Y',Total=0
Where Acno=:LedgerAcno;

end
end
end

And one more: I don't know how NUMERIC(9, 2) is mapped in dialect 3
database, but in dialect 1 it is Float, so

if (NUMERIC(9, 2)-NUMERIC(9, 2)=0) then

will never be true, correct is

if (Abs(NUMERIC(9, 2)-NUMERIC(9, 2))<Wanted_Precision) then

Best regards, Alexander V.Nevsky.