0

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;

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much Mr.Craig Ringer. It really works fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.