postgresql - plpgsql 函数错误

标签 postgresql plpgsql

create function dept_count (dept_name varchar(20))
returns integer as $$
begin
declare d_count integer;
select count(*) into d_count
from instructor;
return d_count; end; 
$$ language plpgsql;

这是我的代码。这给出了一个错误

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

这是什么原因?

最佳答案

DECLARE must come before BEGIN.

否则你的函数会工作 - 除了声明一个未引用的参数没有多大意义,但这可能只是为了这个问题的目的的简化。

关于postgresql - plpgsql 函数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25470959/

相关文章:

postgresql - 如何使用嵌套配置docker/pgadmin

sql-server - 如何为不同的数据库多次运行 NUnit onetimesetup

regex - 从 PostgreSQL 中的字段中提取数字

python - 在 Django 中使用 ThreadPoolExecutor 时出现数据库 "is being accessed by other users"错误

PostgresQL 错误 : relation <table> doesn't exist

postgresql - 如何在不创建函数的情况下执行 pl/pgsql 代码?

postgresql - 从点数组创建 postgres postgis 多边形

sql - 将列添加到 %rowtype 的记录变量

php - 如何将自定义类型数组传递给 Postgres 函数

postgresql - 在 psql 中为 Postgres 函数体访问\设置变量