Почему этот PL / PgSql недействителен? - PullRequest
0 голосов
/ 17 сентября 2018

N00bie PL / PgSql вопрос:

create or replace function foo() returns int as $$
declare fff int;
begin
    declare fff int;
    select count(*) into fff from mytable;
    return fff;
end 
$$ language plpgsql

выход:

ERROR:  syntax error at or near "*"
LINE 5:   select count(*) into fff from mytable;
                 ^
CONTEXT:  invalid type name "count(*) into fff from mytable"

Что я делаю не так?

1 Ответ

0 голосов
/ 17 сентября 2018

Вам необходимо удалить declare из begin/end блока:

create or replace function foo() returns int as $$
declare fff int;
begin
    --   declare fff int;
    select count(*) into fff from mytable;
    return fff;
end 
$$ language plpgsql

дб <> скрипка

Ах. Правильно. Так вы не можете объявить переменную внутри блока BEGIN ... END?

Вы объявляете переменные на declare части. Если вам нужно, вы можете вкладывать блоки:

create or replace function foo() returns int as $$
declare fff int;
begin
    declare xxx INT;
    begin
    select count(*) into xxx from mytable;
    return xxx;
    end;
end 
$$ language plpgsql
...