PostgreSQL存储过程创建多表报错:relation mastertable不存在
修复PostgreSQL存储过程中"relation does not exist"错误
针对你遇到的ERROR: relation "mastertable" does not exist错误,从以下几个方向排查修复:
1. 确认表的存在与拼写
- 先验证数据库中是否真的存在
mastertable,执行查询:SELECT table_name FROM information_schema.tables WHERE table_name = 'mastertable'; - 注意PostgreSQL的表名大小写规则:如果创建表时用了双引号(比如
CREATE TABLE "MasterTable"),引用时必须保持双引号和大小写一致,修改存储过程中的语句为:select * from "MasterTable";
2. 检查权限与执行用户
执行存储过程的用户需要拥有mastertable的SELECT权限,若没有则执行授权语句:
GRANT SELECT ON mastertable TO 你的用户名;
3. 指定Schema(若表不在默认搜索路径)
如果mastertable位于非默认Schema(比如不是public),需要在表名前加上Schema前缀,例如:
select * from my_schema.mastertable;
或者在存储过程开头添加搜索路径设置:
SET search_path TO my_schema, public;
修正存储过程中的语法错误
你的存储过程还存在两处语法问题,需一并修正:
- 每个
CREATE TABLE语句末尾缺少分号 with some filter不是合法语法,需替换为WHERE条件
修正后的完整存储过程示例:
create or replace procedure test() language sql as $$ -- 若需要指定Schema,添加搜索路径 -- SET search_path TO my_schema, public; drop table if exists a1; create table a1 as select * from mastertable; drop table if exists a2; create table a2 as select * from a1 where 你的过滤条件; -- 替换为实际WHERE条件 drop table if exists a3; create table a3 as select * from a2 where 你的过滤条件; -- 替换为实际WHERE条件 ;$$
内容的提问来源于stack exchange,提问作者Awesome
相关产品推荐
相关产品推荐

