我在 PostgreSQL 中有一个存储过程
CREATE OR REPLACE FUNCTION show_senti_lang_setting(IN _senti_id bigint)
RETURNS TABLE(lang_code character, native_name character varying, is_active boolean) AS
$BODY$
BEGIN
RETURN QUERY
SELECT
l.lang_code,
l.native_name,
(CASE WHEN s.senti_id is NULL THEN FALSE
ELSE TRUE
END) is_active
FROM
language l
LEFT JOIN senti_lang s
ON s.lang_code=l.lang_code
AND s.senti_id=_senti_id;
END;
$BODY$
LANGUAGE plpgsql VOLATILE STRICT;
错误是:
ERROR: syntax error at or near "$1"
LINE 1: ...HEN s.senti_id is NULL THEN FALSE ELSE TRUE END) $1 FROM l...
^
QUERY: SELECT l.lang_code, l.native_name, (CASE WHEN s.senti_id is NULL THEN FALSE ELSE TRUE END) $1 FROM language l LEFT JOIN senti_lang s ON s.lang_code=l.lang_code AND s.senti_id= $2
CONTEXT: SQL statement in PL/PgSQL function "show_senti_lang_setting" near line 13
********** Error **********
ERROR: syntax error at or near "$1"
SQL state: 42601
Context: SQL statement in PL/PgSQL function "show_senti_lang_setting" near line 13
似乎错误是由于CASE
plpgsql 引起的。相同的函数在 SQL 中运行良好:
CREATE OR REPLACE FUNCTION show_senti_lang_setting(bigint)
RETURNS TABLE(lang_code character, native_name character varying, is_active boolean) AS
$BODY$
SELECT
l.lang_code,
l.native_name,
CASE WHEN s.senti_id is NULL THEN FALSE
ELSE TRUE
END is_active
FROM
language l
LEFT JOIN senti_lang s
ON s.lang_code=l.lang_code
AND s.senti_id=$1;
$BODY$
LANGUAGE sql VOLATILE STRICT;