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"
我究竟做错了什么?
你需要从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