0

I want to parametrize the name of a view using postgresql. In particular, I want to create a view inside a function and use the parameter for the name of the view.

CREATE OR REPLACE FUNCTION function_to_be(certain_date  date)   RETURNS void as $$

BEGIN
CREATE OR REPLACE VIEW name_of_schema.test_view AS
SELECT * from test_table;   
END;

$$ LANGUAGE plpgsql;

So the desired name of the view should be name_of_schema.test_view_2019_07_29 after executing the function with the parameter .

However, the name should be schema-qualified - such that I could not directly pass a whole string for the name.

Many thanks in advance!

3
  • This sounds strange. What exactly are you trying to achieve with that? Commented Jul 29, 2019 at 12:53
  • I want to call the function and get a separate table each time for each certain_date. Commented Jul 29, 2019 at 12:58
  • Why create a view for each date then? At the end you wind up with thousands of views. You could create a function that returns the data you want by applying a restriction using the passed date. Commented Jul 29, 2019 at 13:00

1 Answer 1

2

It sounds a little stange to me having a lot of views in the end, that all are defined with the same query...

But you can try and use EXECUTE.

...
EXECUTE 'CREATE OR REPLACE VIEW name_of_schema.test_view_' || to_char(certain_date, 'yyyy_mm_dd') || ' AS SELECT * from test_table;';
...
Sign up to request clarification or add additional context in comments.

Comments

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.