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;