简体   繁体   中英

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. 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:

EXECUTE format('create or replace view %I as ...', roll::text);

See: dynamic SQL in PL/PgSQL in the docs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM