[英]How to create view with the name which can be pass through function as a parameter in postgresql?
I am trying to create a view in postgreSQL using function. 我试图在postgreSQL中使用函数创建一个视图。 I want a view name in such a way that, which should be pass by the function as a parameter.
我想要一个视图名称,它应该作为参数传递给函数。 For example..
例如..
--Table - 表
create table test
( rollno int,
name text);
--Function to create a view - 创建视图的功能
create or replace function fun_view(roll int)
returns void as
$Body$
declare
rec record;
begin
for rec in select * from test where rollno=roll loop
create or replace view "---Name of view should be roll---" as
select rollno from test;
end loop;
$Body$
language plpgsql;
You need PL/PgSQL's dynamic SQL EXECUTE
command: 您需要PL / PgSQL的动态SQL
EXECUTE
命令:
EXECUTE format('create or replace view %I as ...', roll::text);
See: dynamic SQL in PL/PgSQL in the docs. 请参阅:文档中的PL / PgSQL中的动态SQL 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.