当前位置: 动力学知识库 > 问答 > 编程问答 >

postgresql - Create PLPGSQL Function from the Result of Another Function

问题描述:

Does anyone know how to set the body of a PLPGSQL function to the text of the result of another function? For example, this is what I have tried:

CREATE FUNCTION "Foo"() RETURNS TEXT AS $$

BEGIN

RETURN 'BEGIN END;';

END;

$$ LANGUAGE PLPGSQL;

CREATE FUNCTION "Bar"() RETURNS TRIGGER AS "Foo"() LANGUAGE PLPGSQL;

What I would like is for "Bar" to be created such that it is equivalent to the following:

CREATE FUNCTION "Bar"() RETURNS TRIGGER AS $$

BEGIN END;

$$ LANGUAGE PLPGSQL;

Does anyone know how to accomplish such a thing (e.g., define a function body based on the result of another function)? Thanks,

网友答案:

Thanks to Craig Ringer for the suggestion. This is the code that I ended up writing to generate the function:

CREATE FUNCTION "Foo"() RETURNS VOID AS $$
    BEGIN
        EXECUTE
            'CREATE FUNCTION ' || QUOTE_IDENT('Bar') || '() RETURNS TRIGGER AS $a$' || chr(10) ||
            'BEGIN' || chr(10) ||
            'END;' || chr(10) ||
            '$a$ LANGUAGE PLPGSQL;';
    END;
$$ LANGUAGE PLPGSQL;
分享给朋友:
您可能感兴趣的文章:
随机阅读: