It can't be automated using the way you've stored the data in columns.
Perhaps this is a clue that you should store the data in a different way.
Another clue is that the changes occur so frequently that you need adding and dropping of the columns to be automated!
Here's an example:
Table1
dataset attribute value
1 col2 True
1 col3 'Foo'
2 col2 False
2 col3 'Bar'
3 col2 False
3 col3 'Baz'
This is also called an Entity-Attribute-Value table. It has plenty of drawbacks too, but its advantage is that you can add or drop attributes just by inserting or deleting rows.
But you also need a table to record the canonical set of attributes that you allow.
Metadata
attribute type default
col2 bool False
col3 string ''
In this table too, adding a new attribute is just an insert. You can make the attribute in the first table a cascading foreign key, so if you delete a row from the Metadata table it automatically deletes matching rows that reference it.
You also need a table to annotate which attributes are to be included in each view:
Viewership
view attribute show
'admin' col1 True
'admin' col2 True
'admin' col3 True
'sales' col1 True
'sales' col1 False
'sales' col1 False
'default' col1 False
'default' col2 True
'default' col3 False
You are still required to manually insert rows to this table if you add a new attribute. But that's easier than adding columns.
You might be able to automate this a bit with triggers so when you insert a new row to the Metadata table, it adds a row for each view to this table. But in my experience, it's not worth the effort to use triggers. Just code it that way in your application.
(You should also assume a default that if an attribute fails to be added to the view table, it is hidden by default.)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…