我有这个PL/pgSQL函数,它必须返回一些用户信息。
CREATE OR REPLACE FUNCTION my_function(
user_id integer
) RETURNS TABLE(
id integer,
firstname character varying,
lastname character varying
) AS $$
DECLARE
ids character varying;
BEGIN
ids := '';
--Some code which build the ids string, not interesting for this issue
RETURN QUERY
EXECUTE 'SELECT
users.id,
users.firstname,
users.lastname
FROM public.users
WHERE ids IN (' || ids || ')';
END;
$$ LANGUAGE plpgsql;
我面临的问题是函数的结果是一个像这样的单列表:
╔═══╦═════════════════════╗
║ ║my_function ║
╠═══╬═════════════════════╣
║ 1 ║ (106,Ned,STARK) ║
║ 2 ║ (130,Rob,STARK) ║
╚═══╩═════════════════════╝
虽然我期望:
╔═══╦════════════╦════════════╦═════════════╗
║ ║ id ║ firstname ║ lastname ║
╠═══╬════════════╬════════════╬═════════════╣
║ 1 ║ 106 ║ Ned ║ STARK ║
║ 2 ║ 103 ║ Rob ║ STARK ║
╚═══╩════════════╩════════════╩═════════════╝
我认为(但不确定)问题来自EXECUTE
声明,但我不知道该怎么做。
有任何想法吗?