PostgreSQL-我可以使用函数返回名称作为视图名称吗

时间:2019-01-08 17:10:30

标签: postgresql

这就是我想要做的:

CREATE OR REPLACE FUNCTION getViewName ()
RETURNS text LANGUAGE plpgsql AS $name$
DECLARE
    name text ;
BEGIN 
    SELECT 'test_schema.backup_' || to_char(CURRENT_DATE,'yyyymmdd')  into name ;
    RETURN name ;
END ; $name$;

成功创建一个函数。

=> select getViewName();
          getviewname
-------------------------------
 test_schema.backup_20190108
(1 row)

但是当我尝试这样做时:

=> create or replace view getViewName() AS select * from test_schema.backup ;
ERROR:  syntax error at or near ")"
LINE 1: create or replace view getViewName() AS select * from ...

我不能使用函数的返回值作为视图名称吗?

我不想使用this之类的解决方案,因为实际上代码看起来像:

create or replace view getViewName() AS select
.....

其中....超过一千行代码,我不想将其放入格式中。

还有其他选择吗?

编辑:

我尝试使用固定名称创建临时视图。

然后执行以下操作:

DO
$$
BEGIN
EXECUTE format( 'CREATE OR REPLACE VIEW %I '
                ' AS SELECT * FROM test_schema.mytemp_view', getViewName()  ) ;
END ;
$$ LANGUAGE plpgsql ;

但是接下来的问题是,由于新的动态视图依赖于它,因此我无法删除mytemp_view。

0 个答案:

没有答案
相关问题