开发者

how to replace multiple words at once in postgresql

to replace multiple words, i'm using looping thru all candidates, and replacing one by one. is there a better way?

CREATE OR REPLACE FUNCTION "myReplace"(text, _varchar, _varchar) RETURNS "pg_catalog"."text" AS 
$BODY$
DECLARE
    i INT;
    result TEXT;
BEGIN
    result = $1;
    FOR i IN 1 .. ARRAY_LENGTH( $2, 1 ) LOOP
        result = REPLAC开发者_运维百科E( result, $2[ i ], $3[ i ] );
    END LOOP;

    RETURN RESULT;
END
$BODY$
LANGUAGE 'plpgsql';


Besides making it STABLE and STRICT, and switching to using pl/perl or C, there isn't much optimization you can do to make this any faster.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜