日期:2014-05-18  浏览次数:20581 次

创建一个视图,包括表A中存在但表B中不存在的数据

表A:
id 
1
2
3

表B:
id
1
3

那么希望视图中是:
id
2

------解决方案--------------------
SQL code
select id from tb1
except
select id from tb2

------解决方案--------------------
SQL code

create view [view_name]
as
select a.id
from 表A a 
left join 表B b on a.id=b.id
where b.id is null

------解决方案--------------------
SQL code
create tigger test on a
for delete
as
begin
delete from b where exists(select 1 from instered where id=b.id)
end

------解决方案--------------------
探讨
追问一下,如果两表都包含1,2,3,如果表A删除了2,我想建立一个trigger,让表B也自动删除2,应该怎么做呢