Function Postgresql将select查询分配给函数中的变量

Function Postgresql将select查询分配给函数中的变量,function,postgresql,variables,Function,Postgresql,Variables,我正在使用Postgresql 9.3,并编写了如下函数: create or replace function test(building text,floor text) returns void as $$ Declare id integer; num integer := 0; Begin num=num+100 id :=select to_number( (select (

我正在使用Postgresql 9.3,并编写了如下函数:

    create or replace function test(building text,floor text) returns void as $$
    Declare
    id integer;
    num integer := 0;
    Begin

    num=num+100

    id :=select to_number(
          (select 
              (select code from buildings where name=building) || floor 
              || (select num::text)),'99999999'
    );

    update table set col1=id;

    End;
    $$
    language plpgsql;
我所期望的是,我的id变量将被分配一个数值,例如:从select to_编号中分配12502100。。。询问

然而,我得到了下面的错误

ERROR:  syntax error at or near ":="
LINE 10: source :=(select code from buildings where name='I3')
如何将带有一些字符串操作的查询结果分配给变量id

我也无法选择进入id。。。方法。

功能评估不需要使用SELECT

id := to_number((SELECT code FROM buildings WHERE name = building) 
                                                      || floor || num::text,
                '999999999');
另一种可能,通常更好的方法是在表达式列表结果字段列表中使用函数

id := (SELECT to_number(code || floor || num::text, '99999999') 
          FROM buildings WHERE name = building)

仅当需要查询数据时才使用“选择”,而不用于函数或变量计算

您的第二个方法在或附近给了我错误不匹配的括号,第一个方法正在工作。@Xianlin:对不起,修复了还有一个括号的问题。如何处理多个变量和一个状态?i、 e.您希望使用从名称=建筑物的建筑物中选择的字段1、字段2填充var1和var2;var1应该包含field1结果,var2应该包含field2结果。@泰晤士河:您必须对多个变量使用SELECT INTO语句