[<<][sql][>>][..]
Sat Sep 21 16:18:20 EDT 2013

table as function

Is there a quick way to abstract a table with a functional dependency
into a function?

I guess a natural join can do most of what i'm looking for, though it
would be nice to find that out..

Ha, I did this before:

DROP FUNCTION IF EXISTS cconv;
DELIMITER |
CREATE FUNCTION cconv(date_in DATE, den_in VARCHAR(3))
RETURNS DECIMAL(10,6)
DETERMINISTIC
BEGIN
        DECLARE rv DECIMAL(10,6) default 1;
        IF NOT den_in = 'USD' THEN
           SELECT rate INTO rv FROM exchange
           WHERE   year(date)=year(date_in) and
                   month(date)=month(date_in) and
                   den=den_in;
        END IF;      
        RETURN rv;
END
|
DELIMITER ;


For the apachelogs id<->text maps it is:

DROP FUNCTION IF EXISTS ip;
DELIMITER |
CREATE FUNCTION ip(id MEDIUMINT)
RETURNS TEXT
DETERMINISTIC
BEGIN
        DECLARE rv TEXT default '';
        SELECT ip INTO rv FROM ip WHERE ip.id_ip = id;
        RETURN rv;
END
|
DELIMITER ;




[Reply][About]
[<<][sql][>>][..]