step4,创建分布式水平分区视图
create view dbo.view_Person
as
select [PersonID]
,[PersonType]
,[FirstName]
,[MiddleName]
,[LastName]
from [dbo].[Person]  with(nolock)
where [PersonType] in('IN','EM','SP')
union all
select [PersonID]
,[PersonType]
,[FirstName]
,[MiddleName]
,[LastName]
from db1.[DBTest2].[dbo].[Person] with(nolock)
where [PersonType] in('SC','VC','GC')
with check OPTION;
  Step5,查询分布式数据,查看执行计划
  SELECT *
  from dbo.view_Person p
  where p.PersonType in ('em','sc')
  Step6,优化
  分布式事务使用的资源远大于内部事务,通常使用OPENQUERY等相关行集函数,避免过度依赖分布式事务。
  1,使用OpenQuery,避免DTC的干预
create view dbo.view_Person
as
select [PersonID]
,[PersonType]
,[FirstName]
,[MiddleName]
,[LastName]
from [dbo].[Person]  with(nolock)
where [PersonType] in('IN','EM','SP')
union all
select [PersonID]
,[PersonType]
,[FirstName]
,[MiddleName]
,[LastName]
from OPENQUERY ( db1 ,
N'select [PersonID]
,[PersonType]
,[FirstName]
,[MiddleName]
,[LastName]
from db1.[DBTest2].[dbo].[Person] with(nolock)
where [PersonType] in(''SC'',''VC'',''GC'')' ) as p
with check OPTION;
  2,在Local Server上更新分片数据
update db1.DBTEST2.dbo.person
set FirstName=N'Harm'
where PersonId=102;
--修改成
exec db1.DBTEST2.sys.sp_executesql N'update dbo.person
set FirstName=N''Harm''
where PersonId=102;'
Appendix
--SQL Server 阻止了对组件 'Ad Hoc Distributed Queries'
exec sp_configure 'show advanced options',1
reconfigure
exec sp_configure 'Ad Hoc Distributed Queries',1
reconfigure
--使用完成后,关闭Ad Hoc Distributed Queries:
exec sp_configure 'Ad Hoc Distributed Queries',0
reconfigure
exec sp_configure 'show advanced options',0
reconfigure