I know how to do this in MySQL and MSSQL but I'm struggling to do the same in Postgres 10.12. I have about 200 views to create and the only difference in each one of them is the schema location and the where clause.
For example:
create or replace view SCHEMA1.myview as
select * from SHAREDSCHEMA.sometable where MYCOL = 'ABC';
So what I need to do is create that view around 200 times with each iteration being:
SCHEMA1.myview
SCHEMA2.myview
SCHEMA3.myview
etc...
MYCOL = 'ABC'
MYCOL = 'EFG'
MYCOL = 'HIJ'
etc...
Hope that makes sense. Again, the only difference between each iteration is the schema name changes and the where clause changes.
I could probably write some Python script or something to generate the code but I'd rather do it in Postgres if possible.
I know this isn't valid code, but essentially this is what I am trying to do:
do $$
declare
myschema varchar(10) := 'SCHEMA1';
mywhere varchar(3) := 'ABC';
begin
create or replace view @[email protected] as
select * from SHAREDSCHEMA.sometable where MYCOL = @mywhere@;
end $$;
Then each time I run that, I simply change @myschema and @mywhere.
Any help would be appreciated.
Thanks!