开发者

how to replace multiple words at once in postgresql

开发者 https://www.devze.com 2023-03-05 01:45 出处:网络
to replace multiple words, i\'m using looping thru all candidates, and replacing one by one. is there a better way?

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

精彩评论

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