I want to dump view data to a table.I have a VIEW that does a lot of case/algorithms/ like this: (dont concern yourself with this code just yet…)
select c1.a as car, c1.b as boat, c1.c as tree convert(char(1),
dbo.aa(l1+l12+l314)) as something1, convert(varchar(9), dbo.bb(l1+l1+l13+l14, 2))
as something2, bo.bb(l1+l12+l13+l14, 3) as something3, convert(char(1),
dbo.aa(l1+l1+l1+l14)) as something4, case when dbo.aa(l1+l12+l13+l14, 2) like
'ttt' then convert(varchar(20), bb(l1+l12+l13+l14, 5))
else null ............... etc..
On insert I want this VIEW’s data to be dumped into a table.
DO I hard code a cascade on insert when I create this view? OR
DO I create a instead of trigger on insert to dump to table?
Either way, how would I code it?
Don’t insert via the view. If the view has complex logic in it, it may be impossible to map back to the underlying table/s and an insert might not be possible.
For example, using your sample: if the view is updated to have a different value for the
something1field, how is this value to be de-constructed to the correct underlying values forl1,l12andl314?Use a stored procedure instead and ensure that the application uses that and does not attempt to update the view.
You can create triggers on all the underlying tables that will query the view and update the data in the extra table you want, though this seems to defeat the point of the view.