首先,你的查询根本不能建立,按照你的写法,有两个相同的字段名id其次,即使改正了,也不可能在SQL中同时添加数据.你是怎么做的? 我测试不行,下面是我的测试语句: create table t1(id int,s1 char(10))create table t2(id int,s2 char(10)) gocreate view tt as SELECT t1id=dbo.t1.id, dbo.t1.s1, t2id=dbo.t2.id, dbo.t2.s2 FROM dbo.t1 INNER JOIN dbo.t2 ON dbo.t1.id = dbo.t2.id goinsert into tt values(1,'aa',1,'bb')
在SQL SERVER中,要实现这个功能,要用触发器来实现.下面是例子:--创建表 create table t1(id int,s1 char(10)) create table t2(id int,s2 char(10)) go--创建视图 create view tt as SELECT dbo.t1.id, dbo.t1.s1, dbo.t2.s2 FROM dbo.t1 INNER JOIN dbo.t2 ON dbo.t1.id = dbo.t2.id go--创建数据处理触发器 create trigger t_insert on tt instead of insert as insert into t1 select id,s1 from inserted insert into t2 select id,s2 from inserted go--测试插入数据 insert into tt values(1,'aa','bb')--显示插入结果 select * from tt go drop table t1,t2 drop view tt
create view au_title (author_au_id, au_lname, au_fname, contract, title_au_id, title_id, au_ord , royaltyper) as select a.au_id, a.au_lname, a.au_fname, a.contract, t.au_id, t.title_id, t.au_ord , t.royaltyper from authors a, titleauthor t where a.au_id = t.au_id若要执行INSERT 语句则在同一个语句只能对属于同一个表的列执行操作所以 若向视图au_title 中插入一行数据只能分别执行以下语句 insert into au_title (author_au_id, au_lname, au_fname, contract) values ('234-34-4611','John','Smith', 1) insert into au_title (title_au_id, title_id, au_ord, royaltyper) values ('234-34-4611','BU1111',1,50)
上面只处理了插入数据的情况,如果要考虑更新和删除,就更改触发器,注意, 这里,对于更新数据,是采用先删除旧数据,再插入新数据的方法: --创建数据处理触发器 create trigger t_insert on tt instead of insert,update,delete as delete from t1 where id in(select id from deleted) delete from t2 where id in(select id from deleted)insert into t1 select id,s1 from inserted insert into t2 select id,s2 from inserted go
我测试不行,下面是我的测试语句:
create table t1(id int,s1 char(10))create table t2(id int,s2 char(10))
gocreate view tt
as
SELECT t1id=dbo.t1.id, dbo.t1.s1, t2id=dbo.t2.id, dbo.t2.s2
FROM dbo.t1 INNER JOIN
dbo.t2 ON dbo.t1.id = dbo.t2.id
goinsert into tt values(1,'aa',1,'bb')
create table t1(id int,s1 char(10))
create table t2(id int,s2 char(10))
go--创建视图
create view tt
as
SELECT dbo.t1.id, dbo.t1.s1, dbo.t2.s2
FROM dbo.t1 INNER JOIN
dbo.t2 ON dbo.t1.id = dbo.t2.id
go--创建数据处理触发器
create trigger t_insert on tt
instead of insert
as
insert into t1 select id,s1 from inserted
insert into t2 select id,s2 from inserted
go--测试插入数据
insert into tt values(1,'aa','bb')--显示插入结果
select * from tt
go
drop table t1,t2
drop view tt
royaltyper)
as
select a.au_id, a.au_lname, a.au_fname, a.contract, t.au_id, t.title_id, t.au_ord , t.royaltyper
from authors a, titleauthor t
where a.au_id = t.au_id若要执行INSERT 语句则在同一个语句只能对属于同一个表的列执行操作所以
若向视图au_title 中插入一行数据只能分别执行以下语句
insert into au_title (author_au_id, au_lname, au_fname, contract)
values ('234-34-4611','John','Smith', 1)
insert into au_title (title_au_id, title_id, au_ord, royaltyper)
values ('234-34-4611','BU1111',1,50)
这里,对于更新数据,是采用先删除旧数据,再插入新数据的方法:
--创建数据处理触发器
create trigger t_insert on tt
instead of insert,update,delete
as
delete from t1 where id in(select id from deleted)
delete from t2 where id in(select id from deleted)insert into t1 select id,s1 from inserted
insert into t2 select id,s2 from inserted
go