将 select 值分配给 PostgreSQL 9.3 中的变量
Assign select value to variable in PostgreSQL 9.3
我有以下 table:
示例:
create table test
(
id int,
name varchar(10),
city varchar(10)
);
我想将 table 中的 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;
使用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
我有以下 table:
示例:
create table test
(
id int,
name varchar(10),
city varchar(10)
);
我想将 table 中的 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;
使用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