Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 是否可以将MSSQL中的表与基于同一表的视图合并?_Sql Server_View_Merge - Fatal编程技术网

Sql server 是否可以将MSSQL中的表与基于同一表的视图合并?

Sql server 是否可以将MSSQL中的表与基于同一表的视图合并?,sql-server,view,merge,Sql Server,View,Merge,像这样的 create view V1 as select * from (T1 union T2) join T3 where "some condition on T3" Then later in the process: merge T2 using V1 when not matched by Target then insert when not matched by Source then delete MS SQL server是否足够智能来处理此问题?可以在合并语句中使

像这样的

create view V1 as 
select * from (T1 union T2) join T3 where "some condition on T3"

Then later in the process:
merge T2 using V1 
when not matched by Target then insert
when not matched by Source then delete

MS SQL server是否足够智能来处理此问题?

可以在合并语句中使用视图(引用目标表):

create table t1
(
    id int,
    colA int
)
go

create table t2
(
    id int,
    colB int
)
go

create view dbo.testview
as
select id, colB
from t2
except
select id, colA
from t1
go


insert into t2(id, colB) values (1, 2)
go

--populate t1 with the values of t2 (that do not exist yet in t1)
merge t1 as t
using testview as s on t.colA = s.colB
when not matched by target then 
insert(id, colA)
values(id, colB);
go
select *
from t2;
select *
from t1;
go

insert into t2(id, colB) values (1, 2)

merge t1 as t
using testview as s on t.colA = s.colB
when not matched by target then 
insert(id, colA)
values(id, colB);
go

select *
from t2;
select *
from t1;
go

drop view testview;
drop table t1;
drop table t2;
go

“MS SQL server是否足够聪明来处理此问题?”这是无效的语法。不能在
视图中执行DML语句。当然,合并不在视图中。只是自动格式将其放在视图定义的正下方。两个步骤-1)创建视图,2)在中使用视图merge@Larnu这是个玩笑吗?对不起,不确定……这是什么玩笑,@Qlk?不,我不是在开玩笑,@Qlk;不能在
视图中执行DML语句
MERGE
是一个DML语句。