我试图创建一个函数来执行此操作:

drop table t_rv_openitem;
select * into t_rv_openitem from rv_openitem;
select * from t_rv_openitem;

有时我对PostgreSQL中的函数感到困惑,并得到此错误:



我知道这似乎是一项简单的任务,但是我想尽办法解决这个问题。

这是完整功能的create语句:
CREATE OR REPLACE FUNCTION adempiere.update_t_rv_openitem()
  RETURNS rv_openitem AS
$BODY$

Drop table t_rv_openitem;
select * into t_rv_openitem from rv_openitem;
select * From t_rv_openitem;

$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION adempiere.update_t_rv_openitem() OWNER TO adempiere;

最佳答案

只需添加BEGIN和END

CREATE OR REPLACE FUNCTION adempiere.update_t_rv_openitem()
  RETURNS rv_openitem AS
$BODY$

BEGIN -- ADD THIS

Drop table t_rv_openitem;
select * into t_rv_openitem from rv_openitem;
select * From t_rv_openitem;

END; -- AND THIS

$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;
ALTER FUNCTION adempiere.update_t_rv_openitem() OWNER TO adempiere;

如果您使用LANGUAGE sql,则不需要BEGIN和END块,但是如果您使用LANGUAGE plpgsql,则需要这些块

更新

关于ERROR: syntax error at "t_rv_openitem" DETAIL: Expected record variable...。您的代码上没有语法错误,您只需要更改以下内容:
select * into t_rv_openitem from rv_openitem;

对此:
create table t_rv_openitem as
select * from rv_openitem;

仅当在PLPGSQL之外使用SELECT * INTO tablehere FROM tableSource创建表时,该表才有效。当该代码结构位于PLPGSQL内部时,其语义将有所不同,这意味着:
SELECT * INTO declaredVariableHere FROM tableSource;

要使表创建在独立语句和PLPGSQL内均有效,只需使用:
CREATE TABLE AS SELECT * FROM tableSourceHere;

10-08 00:47