如何将查询结果分配给 PL/pgSQL(PostgreSQL 的过程语言)中的变量?
我有一个函数:
CREATE OR REPLACE FUNCTION test(x numeric)
RETURNS character varying AS
$BODY$
DECLARE
name character varying(255);
begin
name ='SELECT name FROM test_table where id='||x;
if(name='test')then
--do somthing
else
--do the else part
end if;
end;
return -- return my process result here
$BODY$
LANGUAGE plpgsql VOLATILE
在上面的函数中我需要存储这个查询的结果:
'SELECT name FROM test_table where id='||x;
到变量name
。
如何处理?
最佳答案
我认为您正在寻找 SELECT select_expressions INTO
:
select test_table.name into name from test_table where id = x;
这将从 test_table
中提取 name
,其中 id
是您函数的参数,并将其保留在 name
中多变的。不要遗漏 test_table.name
上的表名前缀,否则您会收到关于引用不明确的投诉。
关于database - 在 PL/pgSQL 中使用变量存储查询结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12328198/