Subject Re: Stored Procedure & Plan & Temporary Table
Author Svein Erling
> PLAN SORT (JOIN (HK1 INDEX (IDX_HAREKETKODLARI_TIPI),M1 HK INDEX
> (PK_HAREKETKODLARI_HARKODU),M1 MUH INDEX (FK_MUHASEBE_HAREKETKODU)))

Hmm, seems like a very good plan to me, but I am puzzled that the plan
reports three tables when the query only contain two. Of course, there
is a view involved...

Maybe we can change your SP_RUNTHIS so that SP_RUNMANYTIMES is
executed no more than neccessary? What about something like:

CREATE PROCEDURE SP_RUNTHIS
RETURNS (
MUH_HESAPAD VARCHAR (50),
HAREKETKODU VARCHAR (50),
PARENT VARCHAR (50),
GIDERTOPLAM DOUBLE PRECISION,
GELIRTOPLAM DOUBLE PRECISION,
TIP VARCHAR (10))
AS
declare variable TIPI VARCHAR(10);
BEGIN
FOR
select distinct
HK.HAREKETKODU, HK.HAREKETKODU_TIPI
FROM HESAPLAR HS
JOIN hareketkodlari HK
ON HS.HESAPLAR_HESAPAD = HK.HAREKETKODU_HESAPAD
WHERE (HS.HESAPLAR_HESAPAD IN (SELECT HESAPAD FROM
sp_hesaplar_talikodlar))
INTO :HAREKETKODU, :TIPI
do begin
if ((TIPI ='GELIR') or (TIPI ='GIDER') then
BEGIN
SELECT SP.GELIRTOPLAM
FROM SP_RUNMANYTIMES('1.1.2003','1.5.2003') SP
WHERE (SP.MUH_HESAPAD = :HAREKETKODU)
INTO :GIDERTOPLAM;
END ELSE GIDERTOPLAM=NULL;

FOR
select
HS.HESAPLAR_HESAPAD,
HS.HESAPLAR_PARENT,
FROM HESAPLAR HS
JOIN hareketkodlari HK
ON HS.HESAPLAR_HESAPAD = HK.HAREKETKODU_HESAPAD
WHERE (HS.HESAPLAR_HESAPAD IN (SELECT HESAPAD FROM
sp_hesaplar_talikodlar))
and hk.hareketkodu = :hareketkodu
and hk.tipi = :tipi
INTO
:MUH_HESAPAD, :PARENT
do begin
suspend;
end
GIDERTOPLAM=NULL;
HAREKETKODU=NULL;
TIPI=NULL;
FOR
select
HS.HESAPLAR_HESAPAD,
HS.HESAPLAR_PARENT,
FROM HESAPLAR HS
WHERE (HS.HESAPLAR_HESAPAD IN (SELECT HESAPAD FROM
sp_hesaplar_talikodlar))
and not exists(select 1 from hareketkodlari HK where HS.
HESAPLAR_HESAPAD = HK.HAREKETKODU_HESAPAD)
INTO
:MUH_HESAPAD, :PARENT
do begin
suspend;
end

end;

There will be errors in this code, I am lousy at stored procedures and
notepad isn't the best syntax editor I've ever used, but you get the
idea.

HTH,
Set