All pastes #728377 Raw Edit

Mine

public text v1 · immutable
·published 2007-10-06 22:35 UTC
rendered paste body
CREATE OR REPLACE FUNCTION get_parent(INTEGER,INTEGER) RETURNS INTEGER AS $$
DECLARE
    person ALIAS FOR $1;    -- person ID
    rel_type ALIAS FOR $2;  -- gender code (1=male, 2=female)
    par INTEGER;            -- person ID of parent, returned by func
BEGIN
    SELECT parent_fk INTO par FROM relations
        WHERE child_fk = person AND relation_type = rel_type;
    RETURN COALESCE(par,0); -- will return parent ID if it exists, 0 otherwise
END;
$$ LANGUAGE plpgsql STABLE;