select db,sch,tbl,col,
--编写删除语句
'
use ['+publisher_db+']
exec sp_dropsubscription @publication = N'''+publication+''', @article = N'''+article+'''
, @subscriber = N''all'', @destination_db = N''all''
exec sp_droparticle @publication = N'''+publication+''', @article = N'''+source_object+''', @force_invalidate_snapshot = 1
' as dropscripts,
--编写生成语句
'
use ['+publisher_db+']
exec sp_addarticle @publication = N'''+publication+''', @article = N'''+article+''',
@source_owner = N'''+source_owner+''', @source_object = N'''+source_object+''', @type = N''logbased'',
@description = N'''', @creation_script = N'''', @pre_creation_cmd = N''drop'', @schema_option = 0x000000000803509F,
@identityrangemanagementoption = N'''+
(case when exists(
select 1 from sys.all_columns t1
inner join sys.index_columns t2 on t1.object_id=t2.object_id and t1.column_id=t2.column_id
inner join sys.indexes t3 on t3.object_id=t2.object_id and t3.index_id=t2.index_id
and t1.is_identity=1 and t3.is_primary_key=1
where t1.object_id=object_id(quotename(aa.source_owner)+'.'+quotename(aa.source_object)) )
then 'manual' else 'none' end)
+''', @destination_table = N'''+destination_object+'''
, @destination_owner = N'''+isnull(destination_owner,source_owner)+''', @status = 24,
@vertical_partition = N''false'', @ins_cmd = N''CALL [dbo].[sp_MSins_dbo'+source_object+']''
, @del_cmd = N''CALL [dbo].[sp_MSdel_dbo'+source_object+']'',
@upd_cmd = N''SCALL [dbo].[sp_MSupd_dbo'+source_object+']''
' as addscripts
from
(
--查询复制订阅项目信息
SELECT a.article,a.publisher_db,b.publication,a.source_owner,a.source_object,a.destination_object,a.destination_owner,
s.db,s.sch,s.tbl,s.col
FROM [distribution].[dbo].[MSarticles] a inner join
[distribution].[dbo].[MSpublications] b
on a.publication_id=b.publication_id
inner join
(
--查询主键信息
select distinct db_name() as db, c.name sch,sp.name tbl,sc.name col from dbo.sysindexes si INNER JOIN
dbo.sysindexkeys sik ON si.id = sik.id AND si.indid = sik.indid INNER JOIN
dbo.syscolumns sc ON sc.id = sik.id AND sc.colid = sik.colid INNER JOIN
dbo.sysobjects so ON so.name = si.name AND so.xtype = 'PK'
inner join sys.sysobjects sp on so.parent_obj=sp.id
inner join sys.schemas c on c.schema_id=sp.uid
intersect
--查询定义好的信息做交集
select db,sch,tbl,col from master.dbo.UnionALL
) s
on a.publisher_db=s.db and a.source_owner=s.sch and a.source_object=s.tbl)
aa
--select * from sys.all_columns a
--inner join sys.index_columns b on a.object_id=b.object_id and a.column_id=b.column_id
--inner join sys.indexes c on b.object_id=c.object_id and b.index_id=c.index_id
--and a.is_identity=1 and c.is_primary_key=1
--where a.object_id=object_id('OrderFlow.BackChangeApply')
--(case when exists(
--select 1 from sys.all_columns a
--inner join sys.index_columns b on a.object_id=b.object_id and a.column_id=b.column_id
--inner join sys.indexes c on b.object_id=c.object_id and b.index_id=c.index_id
--and a.is_identity=1 and c.is_primary_key=1
--where a.object_id=object_id(quotename(a.source_owner)+'.'+quotename(a.source_object))
--then '' else '' end)