我想在 plpgsql 函数中使用 ltree 参数执行查询......但我不明白如何在这个函数中使用引号......
CREATE OR REPLACE FUNCTION f_select(BIGINT) RETURNS setof categories AS
$$
DECLARE
s_cat ALIAS FOR $1;
queryText TEXT;
result categories%ROWTYPE;
BEGIN
queryText := 'SELECT * FROM categories WHERE cat_tree ~ \'*.\'|| s_cat::text ||\'.*\'';
FOR result IN EXECUTE queryText
LOOP
RETURN NEXT result;
END LOOP;
RETURN;
END
$$
LANGUAGE plpgsql;
这个怎么做 ???
在 psql 中执行此代码后,我收到错误:
ERROR: syntax error at or near "."
LINE 10: ... := 'SELECT * FROM categories WHERE cat_tree ~ \'*.\'|| s_ca...
最终工作版本:
CREATE OR REPLACE FUNCTION f_select(BIGINT) RETURNS setof categories AS
$$
DECLARE
s_cat ALIAS FOR $1;
queryText TEXT;
result categories%ROWTYPE;
BEGIN
queryText := 'SELECT * FROM categories WHERE cat_tree ~ ''' || ('*.'|| s_cat::text || '.*')::lquery || '''';
FOR result IN EXECUTE queryText
LOOP
RETURN NEXT result;
END LOOP;
RETURN;
END
$$
LANGUAGE plpgsql;