Using/accessing cursors
Posted: Fri 26 Aug 2011 20:05
I have the following function:
CREATE OR REPLACE FUNCTION company_select(IN v_companynr integer, INOUT swv_refcur refcursor, INOUT swv_refcur2 refcursor)
RETURNS record AS
$BODY$
DECLARE
v_Count SMALLINT;
BEGIN
select Count(*) INTO v_Count From Company Where Company.CompanyNr = v_CompanyNr;
If v_Count = 0 then
open SWV_RefCur for Select 1 AS Err, 'Company not found. (Company_Select)' AS Msg;
Return;
end if;
select Count(*) INTO v_Count From CompanyAccount Where CompanyNr = v_CompanyNr;
If v_Count = 0 then
Insert Into CompanyAccount(CompanyNr) Values(v_CompanyNr);
end if;
open SWV_RefCur2 for Select *,
(select ObjText from ObjDescrCoy
where CompanyNr = 0 and ObjName = 'BANK' and ObjCode = BankNr) AS BankName
From Company, CompanyAccount
Where Company.CompanyNr = v_CompanyNr
And CompanyAccount.CompanyNr = v_CompanyNr;
Insert Into ObjDescrCoy(CompanyNr, ObjName, ObjCode, ObjText)
Select v_CompanyNr, 'COMPUTER', Coalesce(Max(ObjCode),0)+1, SYS_CONTEXT('userenv','host')
From ObjDescrCoy
Where CompanyNr = v_CompanyNr
And ObjName = 'COMPUTER'
And Not Exists(Select * From ObjDescrCoy x
Where x.CompanyNr = v_CompanyNr
And x.ObjName = 'COMPUTER'
And x.ObjText = SYS_CONTEXT('userenv','host'));
Insert Into ObjDescrCoy(CompanyNr, ObjName, ObjCode, ObjText)
Select v_CompanyNr, 'USER', Coalesce(Max(ObjCode),0)+1, CURRENT_USER
From ObjDescrCoy
Where CompanyNr = v_CompanyNr
And ObjName = 'USER'
And Not Exists(Select * From ObjDescrCoy x
Where x.CompanyNr = v_CompanyNr
And x.ObjName = 'USER'
And x.ObjText = CURRENT_USER);
END; $BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
...................
Now, how in the world can I access swv_refcur, swv_refcur2 in Delphi?
CREATE OR REPLACE FUNCTION company_select(IN v_companynr integer, INOUT swv_refcur refcursor, INOUT swv_refcur2 refcursor)
RETURNS record AS
$BODY$
DECLARE
v_Count SMALLINT;
BEGIN
select Count(*) INTO v_Count From Company Where Company.CompanyNr = v_CompanyNr;
If v_Count = 0 then
open SWV_RefCur for Select 1 AS Err, 'Company not found. (Company_Select)' AS Msg;
Return;
end if;
select Count(*) INTO v_Count From CompanyAccount Where CompanyNr = v_CompanyNr;
If v_Count = 0 then
Insert Into CompanyAccount(CompanyNr) Values(v_CompanyNr);
end if;
open SWV_RefCur2 for Select *,
(select ObjText from ObjDescrCoy
where CompanyNr = 0 and ObjName = 'BANK' and ObjCode = BankNr) AS BankName
From Company, CompanyAccount
Where Company.CompanyNr = v_CompanyNr
And CompanyAccount.CompanyNr = v_CompanyNr;
Insert Into ObjDescrCoy(CompanyNr, ObjName, ObjCode, ObjText)
Select v_CompanyNr, 'COMPUTER', Coalesce(Max(ObjCode),0)+1, SYS_CONTEXT('userenv','host')
From ObjDescrCoy
Where CompanyNr = v_CompanyNr
And ObjName = 'COMPUTER'
And Not Exists(Select * From ObjDescrCoy x
Where x.CompanyNr = v_CompanyNr
And x.ObjName = 'COMPUTER'
And x.ObjText = SYS_CONTEXT('userenv','host'));
Insert Into ObjDescrCoy(CompanyNr, ObjName, ObjCode, ObjText)
Select v_CompanyNr, 'USER', Coalesce(Max(ObjCode),0)+1, CURRENT_USER
From ObjDescrCoy
Where CompanyNr = v_CompanyNr
And ObjName = 'USER'
And Not Exists(Select * From ObjDescrCoy x
Where x.CompanyNr = v_CompanyNr
And x.ObjName = 'USER'
And x.ObjText = CURRENT_USER);
END; $BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
...................
Now, how in the world can I access swv_refcur, swv_refcur2 in Delphi?