create table test1 (descr varchar(32))
go
insert test1
select 'Quick demo of' union
select 'instead of triggers'
go
create trigger tr_test1_io on test1 instead of update as
BEGIN
select descr as 'Inserted Columns' from inserted
select descr as 'Deleted Columns' from deleted
select descr as 'Table test1' from test1
END -- trigger def
go
update test1 set descr = NULL
go
drop table test1
create table country (
countryID int identity,
countryName varchar(32),
}
go
create table city (
cityID int identity ,
CityName varchar(32),
countryID int,
)
go
create view v_geography as
select countryName, cityName
from country inner join city on country.countryID = city.countryID
go
create trigger tr_v_geography_io on v_geography instead of insert as
BEGIN
insert country (countryName)
select distinct inserted.countryName
from inserted left join country on inserted.countryName = country.countryName
where country.countryName IS NULL /*** Exclude countries already in the table ***/
insert city (cityName, countryID)
select distinct inserted.cityName, country.countryID
from inserted inner join country on inserted.countryName = country.countryName
left join city on inserted.cityName = city.cityName
where city.cityName IS NULL /*** Exclude cities already in the table ***/
END -- trigger def
go
insert v_geography
select 'England', 'London' UNION
select 'England', 'Manchester' UNION
select 'Japan', 'Tokyo' UNION
select 'Japan', 'Osaka' UNION
select 'USA', 'Washington DC' UNION
select 'USA', 'New York'