删除某个具体同步链

来源:互联网 发布:卖家怎么申请淘宝介入 编辑:程序博客网 时间:2024/05/21 17:45
USE masterGOSET NOCOUNT ONDECLARE @pubinfo_for_drop TABLE(id INT IDENTITY(1,1) NOT NULL  PRIMARY KEY,publication_id int,publication SYSNAME,pub_server SYSNAME, pub_db SYSNAME, pub_schema SYSNAME, pub_object SYSNAME, sub_server SYSNAME,sub_db SYSNAME, sub_schema SYSNAME,sub_object SYSNAME)DECLARE @execsql TABLE(id INT IDENTITY(1,1) PRIMARY KEY,runsql VARCHAR(2000))--解析publisher;WITH  PUBLISHERS AS( SELECT publisher=N'Server.DB.dbo.TB'    ,subscriber_server = NULL  ,subscriber_database =NULL,subscriber_schema = N'dbo',subscriber_object =NULLUNION ALL SELECT publisher=N'Server.DB.dbo.mytb,sam'      ,subscriber_server = NULL ,subscriber_database =NULL,subscriber_schema = N'dbo',subscriber_object =NULL),PUB_INFOAS(SELECT TA.pub_server,pub_db,pub_schema,T.C.value('.[1]','sysname') pub_object,TA.sub_server,TA.sub_db,TA.sub_schema,TA.sub_objectFROM(SELECT PARSENAME(PUBLISHERS.publisher,4) AS pub_server,PARSENAME(PUBLISHERS.publisher,3)  AS pub_db,PARSENAME(PUBLISHERS.publisher,2)  AS pub_schema, CAST(N'<value>'+REPLACE(PARSENAME(PUBLISHERS.publisher,1),N',',N'</value><value>')+N'</value>' AS XML )  TBXML,subscriber_server AS sub_server,subscriber_database AS sub_db,subscriber_schema AS sub_schema,subscriber_object AS sub_objectFROM PUBLISHERS ) TACROSS APPLYTA.TBXML.nodes('value')  AS T(C)) ,PUB_INFO_2AS(SELECTpub.pub_server, pub.pub_db, pub.pub_schema, pub.pub_object,pub.sub_server,pub.sub_db AS sub_db,ISNULL(CAST(pub.sub_schema AS SYSNAME),CAST(pub.pub_schema AS SYSNAME)) AS sub_schema,ISNULL(CAST(pub.sub_object AS SYSNAME),CAST(pub.pub_object AS SYSNAME)) AS sub_objectFROM PUB_INFO AS pub),PUN_INFO_ALLAS(SELECT PUB.publication_id,PUB.publication, @@SERVERNAME  pub_server,ART.publisher_db pub_db,ART.source_owner pub_schema,ART.source_object pub_object,SER.name sub_server,SUB.subscriber_db sub_db,ISNULL(CAST(ART.destination_owner AS SYSNAME),CAST(ART.source_owner AS SYSNAME)) sub_schema,ART.destination_object sub_objectFROM distribution.dbo.MSarticles ARTINNER JOIN distribution.dbo.MSpublications PUBON ART.publication_id = PUB.publication_idINNER JOIN distribution.dbo.MSsubscriptions SUBON SUB.publication_id = PUB.publication_idAND SUB.article_id = ART.article_idINNER JOIN sys.servers  SERON SER.server_id = SUB.subscriber_id)INSERT INTO @pubinfo_for_dropSELECT PALL.publication_id,PALL.publication,P2.pub_server, P2.pub_db, P2.pub_schema, P2.pub_object,sub_server=CASE ISNULL(CAST(P2.sub_server AS SYSNAME),'{_NOTSUBSERVER}')WHEN '{_NOTSUBSERVER}' THEN CAST(PALL.sub_server AS SYSNAME)ELSE CAST(P2.sub_server AS SYSNAME)END,sub_db=CASE ISNULL(CAST(P2.sub_server AS SYSNAME),'{_NOTSUBSERVER}')WHEN '{_NOTSUBSERVER}' THEN CAST(PALL.sub_db AS SYSNAME)ELSE ISNULL(CAST(P2.sub_db AS SYSNAME),CAST(PALL.sub_db AS SYSNAME)) END,P2.sub_schema, P2.sub_objectFROM PUB_INFO_2 P2INNER JOIN PUN_INFO_ALL PALLON P2.pub_server=PALL.pub_serverAND P2.pub_db=PALL.pub_dbAND P2.pub_schema=PALL.pub_schemaAND P2.pub_object=PALL.pub_object--AND P2.sub_db=PALL.sub_dbAND (  P2.sub_server IS NULL OR (P2.sub_server IS NOT NULL AND P2.sub_server=PALL.sub_server))ORDER BY PALL.publication_id,P2.pub_dbSELECT * FROM @pubinfo_for_dropDECLARE @pubinfo TABLE(id INT IDENTITY(1,1) NOT NULL  PRIMARY KEY,publication_id int,publication SYSNAME,pub_server SYSNAME, pub_db SYSNAME, pub_schema SYSNAME, pub_object SYSNAME, sub_server SYSNAME,sub_db SYSNAME, sub_schema SYSNAME,sub_article SYSNAME)INSERT INTO @pubinfoSELECT PUB.publication_id,PUB.publication, @@SERVERNAME  pub_server,ART.publisher_db pub_db,ART.source_owner pub_schema,ART.source_object pub_object,SER.name sub_server,SUB.subscriber_db sub_db,ISNULL(CAST(ART.destination_owner AS SYSNAME),CAST(ART.source_owner AS SYSNAME)) sub_schema,ART.articleFROM distribution.dbo.MSarticles ARTINNER JOIN distribution.dbo.MSpublications PUBON ART.publication_id = PUB.publication_idINNER JOIN distribution.dbo.MSsubscriptions SUBON SUB.publication_id = PUB.publication_idAND SUB.article_id = ART.article_idINNER JOIN sys.servers  SERON SER.server_id = SUB.subscriber_id--SELECT * FROM @pubinfoDECLARE @dropsubscription VARCHAR(2000),@droparticle VARCHAR(2000),@droppublication VARCHAR(2000),@dropsubscription_in VARCHAR(2000),@droparticle_in VARCHAR(2000),@droppublication_in VARCHAR(2000)SELECT @dropsubscription = 'use {db}exec sp_dropsubscription @publication = N''{publication_name}'', @subscriber = N''{sub_server}'', @destination_db = N''{sub_db}'', @article = N''{sub_object}''',@droparticle='use {db}exec sp_droparticle @publication = N''{publication_name}'', @article = N''{sub_object}'', @force_invalidate_snapshot = 1',@droppublication='use {db}exec sp_droppublication @publication = N''{publication_name}'''DECLARE @db AS SYSNAME,@publication_id INT,@publication AS SYSNAME,@pub_db AS SYSNAME,@sub_server AS SYSNAME,@sub_db AS SYSNAME,@sub_object AS SYSNAMEDECLARE @id AS INT=-1WHILE(1=1)BEGINIF NOT EXISTS( SELECT TOP(1) 1 FROM @pubinfo_for_drop)BREAKSELECT TOP(1) @id=id, @publication_id=publication_id, @publication=publication, @pub_db=pub_db,@sub_server=sub_server, @sub_db=sub_db, @sub_object=sub_objectFROM @pubinfo_for_drop--1--同一个表对象被多个server订阅,删除部分server的订阅IF EXISTS(SELECT TOP(1) 1 FROM @pubinfo AS INFOWHERE INFO.publication_id=@publication_idAND INFO.sub_article=@sub_objectAND INFO.sub_server <> @sub_server)BEGINSELECT @dropsubscription_in=REPLACE(REPLACE(REPLACE(REPLACE( REPLACE(@dropsubscription,'{db}',@pub_db),'{publication_name}',@publication),'{sub_server}',@sub_server),'{sub_db}',@sub_db),'{sub_object}',@sub_object)DELETE TOP(1) FROM @pubinfo WHERE publication_id=@publication_idAND sub_server=@sub_serverAND sub_db=@sub_dbAND sub_article=@sub_objectPRINT '--************'PRINT @dropsubscription_in     --TESTGOTO NEXTSTEPINWHILEEND--在同一个publication里还存在其他的订阅对象    IF EXISTS(SELECT TOP(1) 1 FROM @pubinfo AS INFOWHERE INFO.publication_id=@publication_idAND INFO.sub_server=@sub_serverAND INFO.sub_db=@sub_dbAND INFO.sub_article<>@sub_object)BEGINSELECT @dropsubscription_in=REPLACE(REPLACE(REPLACE(REPLACE( REPLACE(@dropsubscription,'{db}',@pub_db),'{publication_name}',@publication),'{sub_server}',@sub_server),'{sub_db}',@sub_db),'{sub_object}',@sub_object)SELECT @droparticle_in=REPLACE(REPLACE(REPLACE(@droparticle,'{db}',@pub_db),'{publication_name}',@publication),'{sub_object}',@sub_object)DELETE TOP(1) FROM @pubinfo WHERE publication_id=@publication_idAND sub_server=@sub_serverAND sub_db=@sub_dbAND sub_article=@sub_objectPRINT '--************'PRINT @dropsubscription_in     --TESTPRINT @droparticle_inGOTO NEXTSTEPINWHILEEND--在该发布中没有其他订阅对象,    IF NOT EXISTS(SELECT TOP(1) 1 FROM @pubinfo AS INFOWHERE INFO.publication_id=@publication_idAND INFO.sub_server=@sub_serverAND INFO.sub_db=@sub_dbAND INFO.sub_article<>@sub_object)BEGINSELECT @dropsubscription_in=REPLACE(REPLACE(REPLACE(REPLACE( REPLACE(@dropsubscription,'{db}',@pub_db),'{publication_name}',@publication),'{sub_server}',@sub_server),'{sub_db}',@sub_db),'{sub_object}',@sub_object)SELECT @droparticle_in=REPLACE(REPLACE(REPLACE(@droparticle,'{db}',@pub_db),'{publication_name}',@publication),'{sub_object}',@sub_object)SELECT @droppublication_in=REPLACE(REPLACE(@droppublication,'{db}',@pub_db),'{publication_name}',@publication)DELETE TOP(1) FROM @pubinfo WHERE publication_id=@publication_idAND sub_server=@sub_serverAND sub_db=@sub_dbAND sub_article=@sub_objectPRINT '--************'PRINT @dropsubscription_in     --TESTPRINT @droparticle_inPRINT @droppublication_inGOTO NEXTSTEPINWHILEENDNEXTSTEPINWHILE:--SELECT @id,@publication_id,@publication,@subscriber,@destination_db,@article    --TESTDELETE TOP(1) FROM @pubinfo_for_dropWHERE id=@idEND


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 饮水机热水口不出水怎么办 饮水机热水口出水小怎么办 新饮水机热水口出水小怎么办 白钢水桶中间支撑怎么办 17岁想长高应该怎么办 身子瘦但脸胖怎么办 减肥只瘦了胸是怎么办 减肥把胸瘦了怎么办 脸又大又圆怎么办 想长高又想减肥怎么办 孩子做作业精力不集中怎么办 写作业老要睡觉怎么办 孩子写作业精神不集中怎么办 小孩精神不集中要怎么办 注意力不集中上课走神怎么办 注意力不集中总是走神怎么办 孩子注意力不集中走神怎么办 上课老走神注意力不集中怎么办 注意力不集中老走神怎么办 7岁儿童视力0.6怎么办 三岁宝宝注意力不集中怎么办 四岁宝宝注意力不集中怎么办 一直在想一件事怎么办 买的家具味道大怎么办 买的木床有味道怎么办 宝宝拍胸片衣服有扣子怎么办 前扣内衣扣子坏了怎么办 运动内衣穿上平胸怎么办 文胸的海绵变形了怎么办 文胸背部松紧变形了怎么办 黑色裤子被漂白水漂红了怎么办 黑色裤子晒红了怎么办 黑色裤子泛红了怎么办 黑色裤子洗红了怎么办 新内衣穿了痒怎么办 灰色针织运动裤洗完发黄怎么办 皮衣买大了一号怎么办 紧身衣破了一个洞怎么办 紧身衣破了一个小洞怎么办 内衣肩带不带调整的怎么办 不正确佩戴胸罩导致下垂怎么办