>>> left outer
>>> join $schema.animals an_calf
[quoted text clipped - 22 lines]
> code", which uses the dam_bhid and recip_bhid indices to minimize the
> time required (in no actual language that I know of):
CREATE FUNCTION purplecow(...) RETURNS VARCHAR(10)
READS SQL DATA
BEGIN ATOMIC
> found = -1
DECLARE found INTEGER;
> while (SELECT * FROM animals WHERE dam_bhid=an1.bhid) {
> if(recip_bhid=0} continue
[quoted text clipped - 3 lines]
> break
> }
SET found = (SELECT bhid FROM animals
WHERE dam_bhid=an1.bhid
AND recip_bhid <> 0
AND activex='Y'
AND birth_date >= current_date-280 days
FETCH FIRST ROW ONLY)
> if(found=-1) {
> while (SELECT * FROM animals WHERE recip_bhid=an1.bhid) {
[quoted text clipped - 4 lines]
> }
> }
SET found = (SELECT bhid FROM animals
WHERE recip_bhid=an1.bhid
AND activex = 'Y'
AND birth_date >= current_date - 280 days
AND found IS NULL
FETCH FIRST ROW ONLY);
> if(found=-1) {nomatch)
> else (matched the current record, with bhid=found}
RETURN CASE WHEN found IS NULL THEN 'No match'
ELSE CHAR(found) END;
END
@
VALUES purplecow();
If you want to take the optimizer out of the equation SQL Functions
rock. Need to be careful with DPF, but in serial they contribute their
fair share to our TPC-C results.
Cheers
Serge

Signature
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab
Bob Stearns - 13 Apr 2006 00:44 GMT
> CREATE FUNCTION purplecow(...) RETURNS VARCHAR(10)
> READS SQL DATA
[quoted text clipped - 29 lines]
> Cheers
> Serge
Could I easily return the entire row found rather than its key? It would
dave 1 join. How do I use it in the middle of a long JOIN sequence?
from an1
.
.
.
left outer join values purplecow(an1.bhid)
as tx(bhid)
on tx.bhid is not null
left outer join animals an_calf
on an_calf.bhid=tx.bhid
.
.
.
is my best guess, but I'm sure that's not quite right
Serge Rielau - 13 Apr 2006 07:49 GMT
>> CREATE FUNCTION purplecow(...) RETURNS VARCHAR(10)
>> READS SQL DATA
[quoted text clipped - 46 lines]
>
> is my best guess, but I'm sure that's not quite right
If you want to return the entire row you sue a TABLE function.
Simply change the function to:
RETURNS TABLE (pk INT, C1 INT, ...)
and the return to:
RETURN VALUES (......);
..
of course you will also have to extend the SET statements
SET (...) = (SELECT ....)
take a look here for examples:
http://www-128.ibm.com/developerworks/db2/library/techarticle/dm-0411rielau/
Cheers
Serge

Signature
Serge Rielau
DB2 Solutions Development
IBM Toronto Lab