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"
我在做什么错了?
答案 0 :(得分:2)
您需要从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