I have a database that contains two or more schema's. Each schema contains several views.
In schema ALPHA
we find the following views:
- vw_a_schools
- vw_a_classes
- vw_a_teachers
In schema BETA
we find the following views:
- vw_b_schools
- vw_b_classes
- vw_b_automobiles
What I am looking for is a procedure that will create a schema ALLTOGETHERNOW
that unions all views with the same name.
So schema ALLTOGETHERNOW
will look like this:
- vw_schools = (select * from vw_a_schools UNION ALL select * from vw_b_schools)
- vw_classes = (select * from vw_a_classes UNION ALL select * from vw_b_classes)
- vw_teachers = (select * from vw_a_teachers)
- vw_automobiles = (select * from vw_b_automobiles)
The following apply:
- the amount of schemas is not known beforehand. If necessary though we can make a table that lists all the schema's to be joined
- the amount of views is not known beforehand.
- Views always look like
vw_<something>_viewname
. We do know for sure that views with the same name have the same columns.
SO the idea is to somehow loopt through the schemas, collect all views, substract the suffix (the vw_<something>
part, then create a statement that will create the view that unions all the views.
I have been playing with information schemas and such and can create a list with viewnames. But there I hit a dead end....