Here’s a view with a hard-coded equality:
create or replace view v1 as select * from mytable where x=3;
Is it possible create a "parameterized" equivalent of this in Postgresql?
create or replace SOMETHING v2(value) as
select * from mytable where x=value;
(Update: We have good answers for Postgresql here, and none for the other databases, so I’m amending the question to be Posgtresql-specific.)
3
Answers
Postgresql version
That is very product dependent, but for postgres you can use functions, but views work the same way, so there are not really any need to do it
and you can use materialized views as well https://www.postgresql.org/docs/current/sql-creatematerializedview.html
fiddle
I’ll respond to the subject line of the question and suggest that you’ve answered your own question. A view is just a query, so you "parameterize" it the same way you would any other query–with a where statement.
In the same way you would write:
you can write:
Suppose view one is defined as:
then the second query above is as if you wrote:
As you can see, these are all functionally equivalent.
I hope this helps. If, on the other hand, you really are looking for a mechanism to create views, then as the other answers suggest, you’ll want to use a procedural language.