function in foreach loop
Posted in 2004
Topics: Versions, Editions & End-of-Life, Jobs, Consulting & Announcements
I have a function that uses a return with resume to return 2 columns and usually between 2 and 7 rows. I now want to use this function in a foreach loop in another function to use each returned value in additional processes. I keep getting syntax errors with the following style code: FOREACH Let x,y = function(parameters); do some more stuff return y,z END FOREACH x,y, and z have all been defined. Is this supported? IDS 9.4FC3 on HPUX11i TIA, Randy sending to informix-list
Kennedy, Randy wrote:
> I have a function that uses a return with resume to return 2 columns and
> usually between 2 and 7 rows. I now want to use this function in a foreach
> loop in another function to use each returned value in additional processes.
> I keep getting syntax errors with the following style code:
>
> FOREACH
> Let x,y = function(parameters);
> do some more stuff
> return y,z
> END FOREACH
>
> x,y, and z have all been defined.
>
> Is this supported? IDS 9.4FC3 on HPUX11i
>
> TIA,
> Randy
> sending to informix-list
OK, so I changed the names and what the functions do but the following
script works (9.40TC2):
DROP FUNCTION innerLoop();
CREATE FUNCTION innerLoop()
RETURNING varchar(128) as tabname, char(32) as owner;
DEFINE m_tabname VARCHAR(128);
DEFINE m_owner CHAR(32);
FOREACH SELECT tabname, owner INTO m_tabname, m_owner from systables
RETURN m_tabname, m_owner WITH RESUME;
END FOREACH;
END FUNCTION;
DROP FUNCTION outerLoop() ;
CREATE FUNCTION outerLoop()
RETURNING varchar(128) as tabname, char(32) as owner; DEFINE m_tabname VARCHAR(128);
DEFINE m_owner CHAR(32);
DEFINE l_tabname VARCHAR(128);
DEFINE l_owner CHAR(32);
LET l_tabname = 'A';
LET l_owner = 'A';
FOREACH EXECUTE FUNCTION innerLoop() INTO m_tabname, m_owner
IF (LENGTH(m_tabname) > length(l_tabname)) THEN
LET l_tabname = m_tabname;
END IF;
IF (LENGTH(m_owner) > length(l_owner)) THEN
LET l_owner = m_owner;
END IF;
END FOREACH;
RETURN l_tabname, l_owner;
END FUNCTION;
EXECUTE FUNCTION outerLoop();
--
Rob Wilson
rob_wilson@ameritech.net.invalid (remove .invalid)