I have an updateable view using an instead of trigger for insert/update. That trigger uses Merge. I'm finding that the Merge statement isn't applying the default constraints from the underlying physical table, although the merge documentation suggests it should.
The following example demonstrates:
create table tblTest
(
id uniqueidentifier not null primary key default newid(),
forename varchar(20),
surname varchar(20) not null default 'xxyyzz'
)
go
create view vwTest as select * from tblTest
go
create Trigger vwTest_trigger_insteadof_insert_update On vwTest
Instead of Insert, Update As
begin
set nocount on
Merge tblTest t
Using
inserted i On (t.id = i.id)
When Matched Then
Update
Set
t.forename = i.forename,
t.surname = i.surname
When Not Matched By Target Then
Insert
(
id,
forename,
surname
)
Values
(
i.id,
i.forename,
i.surname
)
OUTPUT $action, Inserted.*, Deleted.*
;
end
go
--Inserts to physical table work as expected
insert into tblTest (id) values (newid())
insert into tblTest (surname) values ('smith')
--Inserts into updateable view fail as no defaults are set
--from the underlying physical table
insert into vwTest (id) values (newid())
insert into vwTest (surname) values ('jones')
I see someone had something similar in Using default values in an INSTEAD OF INSERT trigger and solved it by copying the rows in inserted into a temporary and then altering the temp table to add in the default constraints from the physical table. I'm not sure I could tolerate the performance issues of these additional steps.