我有下表:
示例:
create table test
(
id int,
name varchar(10),
city varchar(10)
);
我想在函数中将ID值从表分配给变量。
功能:
create or replace function testing(ids int,names varchar(10),citys varchar(10)
returns void as
$body$
Declare
ident int;
BEGIN
ident := SELECT ID FROM test;
raise info '%',ident;
END;
$body$
Language plpgsql;
错误详情:
ERROR: syntax error at or near "SELECT"
LINE 12: ident := SELECT ID from test;
答案 0 :(得分:4)
使用select ... into
create or replace function testing(ids int,names varchar(10),citys varchar(10)
returns void as
$body$
Declare
ident int;
foo text;
BEGIN
SELECT ID, some_col
into ident, foo
FROM test;
raise info '%',ident;
END;
$body$
Language plpgsql;
手册中有更多细节和示例:
http://www.postgresql.org/docs/current/static/plpgsql-statements.html#PLPGSQL-STATEMENTS-SQL-ONEROW