问题描述
我有一个存储过程
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 eles part end if; end; return -- return my process result here $BODY$ LANGUAGE plpgsql VOLATILE
在上面的过程中我需要存储
'SELECT name FROM test_table where id='||id;
这个查询返回结果到变量名
如何处理?请告诉我
推荐答案
我想你正在寻找 SELECT INTO:
select test_table.name into name from test_table where id = x;
这将从 test_table 中提取 name ,其中 id 是您的函数的参数,并将其保留在 name 变量中.不要在 test_table.name 上遗漏表名前缀,否则您会收到关于不明确引用的投诉.
问题描述
How to store the query result in variable using postgresql Stored procedure
i have a stored procedure
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 eles part end if; end; return -- return my process result here $BODY$ LANGUAGE plpgsql VOLATILE
in the above procedure i need to store
'SELECT name FROM test_table where id='||id;
this query returning result to the variable name
how to process this? please let me know
推荐答案
I think you're looking for SELECT INTO:
select test_table.name into name from test_table where id = x;
That will pull the name from test_table where id is your function's argument and leave it in the name variable. Don't leave out the table name prefix on test_table.name or you'll get complaints about an ambiguous reference.