PostgreSQL从存储过程执行匿名代码时遇列不存在错误求助
问题解决步骤
1. 修正匿名代码的参数引用
原tbl_tokens中的匿名块未正确定义输出参数的引用方式,执行时会报错p_out does not exist,需要修改tokens内容,改用位置参数接收输出值:
update tbl_tokens set tokens = 'do $$ declare id_cnt int; begin select count(1) into id_cnt from emp where id = $1; if(id_cnt>0) then $2 := 'true'; else $2 := 'false'; end if; end; $$' where id=1;
2. 调整存储过程的执行逻辑
确保存储过程中EXECUTE语句的参数传递语法正确:
CREATE OR REPLACE PROCEDURE public.prc_token(IN in_token public.tbl_tokens) LANGUAGE plpgsql AS $procedure$ DECLARE v_out varchar(10); BEGIN execute in_token.tokens using 1, OUT v_out; if(v_out = 'true') then raise info '1'; end if; END; $procedure$;
3. 正确调用存储过程
错误根源是直接传入表名tbl_tokens,存储过程需要的是行类型参数,必须传入具体的行记录:
-- 调用单条记录(以id=1为例) call public.prc_token((select * from tbl_tokens where id=1));
如果需要执行表中所有token代码,可通过循环遍历实现:
DO $$ DECLARE rec tbl_tokens%rowtype; BEGIN FOR rec IN SELECT * FROM tbl_tokens LOOP CALL public.prc_token(rec); END LOOP; END $$;
错误原因说明
call public.prc_token(tbl_tokens);执行时,PostgreSQL会将tbl_tokens解析为当前上下文的列名,而非表名或行类型值,因此抛出"column "tbl_tokens" does not exist"错误,必须传入具体的行记录而非表名。
内容的提问来源于stack exchange,提问作者MAK
相关产品推荐
相关产品推荐

