将选择值分配给PostgreSQL 9.3中的变量

我有下表:

范例

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;
a_horse_with_no_name

使用 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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章