sqlserver 2008 通过 实现发布订阅

新建发布订阅

1. 在源服务器新建发布

 1 CREATE PROCEDURE [dbo].[app_publish] 
 2     @name varchar(50),
 3     @db_name varchar(50),
 4     @login_name varchar(20),
 5     @password varchar(20)
 6 AS
 7 BEGIN
 8     -- SET NOCOUNT ON added to prevent extra result sets from
 9     -- interfering with SELECT statements.
10 -- 添加事务发布
11 exec sp_replicationdboption @dbname = @db_name, @optname = N'publish', @value = N'true'
12 
13 exec sp_addpublication @publication = @name, @description = '数据库的事务发布。', 
14         @sync_method = N'concurrent', @retention = 0, @allow_push = N'true', @allow_pull = N'true', @allow_anonymous = N'true', 
15         @enabled_for_internet = N'false', @snapshot_in_defaultfolder = N'true', @compress_snapshot = N'false', @ftp_port = 21, 
16         @ftp_login = N'anonymous', @allow_subscription_copy = N'false', @add_to_active_directory = N'false', 
17         @repl_freq = N'continuous', @status = N'active', @independent_agent = N'true', @immediate_sync = N'true', 
18         @allow_sync_tran = N'false', @autogen_sync_procs = N'false', @allow_queued_tran = N'false', @allow_dts = N'false', 
19         @replicate_ddl = 1, @allow_initialize_from_backup = N'false', @enabled_for_p2p = N'false', @enabled_for_het_sub = N'false'
20 
21 exec sp_addpublication_snapshot @publication = @name, @frequency_type = 4, @frequency_interval = 1, 
22         @frequency_relative_interval = 1, @frequency_recurrence_factor = 0, @frequency_subday = 2, 
23         @frequency_subday_interval = 10, @active_start_time_of_day = 0, @active_end_time_of_day = 235959, 
24         @active_start_date = 0, @active_end_date = 0, @job_login = null, @job_password = null, @publisher_security_mode = 0, 
25         @publisher_login = @login_name, @publisher_password = @password
26 
27 declare @table_name varchar(50);
28 declare @ins_cmd varchar(50),
29         @del_cmd varchar(50),
30         @upd_cmd varchar(50);
31 -- 创建一个游标
32 declare cursor_table cursor scroll for select table_name from app_table;
33 -- 打开游标
34 open cursor_table;
35 -- 获取第一条记录
36 fetch first from cursor_table into @table_name;
37 -- 遍历表名, 执行添加订阅协议
38 while(@@FETCH_STATUS = 0)
39 begin
40     set @ins_cmd = N'CALL sp_MSins_dbo'+@table_name;
41     set @del_cmd = N'CALL sp_MSdel_dbo'+@table_name;
42     set @upd_cmd = N'SCALL sp_MSupd_dbo'+@table_name;
43     exec sp_addarticle @publication = @name, @article = @table_name, @source_owner = N'dbo', @source_object = @table_name, 
44             @type = N'logbased', @description = null, @creation_script = null, @pre_creation_cmd = N'drop', 
45             @schema_option = 0x000000000803509F, @identityrangemanagementoption = N'manual', @destination_table = @table_name, 
46             @destination_owner = N'dbo', @vertical_partition = N'false', @ins_cmd = @ins_cmd, @del_cmd = @del_cmd, @upd_cmd = @upd_cmd;
47     fetch next from cursor_table into @table_name;
48 end
49 -- 关闭游标
50 close cursor_table;
51 -- 删除游标
52 deallocate cursor_table;
53 END
View Code

2. 新建推送订阅

 1 CREATE PROCEDURE [dbo].[app_subscription] 
 2     -- Add the parameters for the stored procedure here
 3     @subscriber varchar(30),
 4     @publication varchar(20),
 5     @destination_db varchar(20),
 6     @subscriber_login varchar(20),
 7     @subscriber_password varchar(20)
 8     
 9 AS
10 BEGIN
11     declare @start_date varchar(20);
12     set @start_date = convert(varchar(8),GETDATE(),112); 
13     exec sp_addsubscription @publication = @publication, @subscriber = @subscriber, @destination_db = @destination_db, 
14         @subscription_type = N'Push', @sync_type = N'automatic', @article = N'all', @update_mode = N'read only', 
15         @subscriber_type = 0
16     exec sp_addpushsubscription_agent @publication = @publication, @subscriber = @subscriber, 
17         @subscriber_db = @destination_db, @job_login = null, @job_password = null, @subscriber_security_mode = 0, 
18         @subscriber_login = @subscriber_login, @subscriber_password = @subscriber_password, @frequency_type = 64, @frequency_interval = 0, 
19         @frequency_relative_interval = 0, @frequency_recurrence_factor = 0, @frequency_subday = 0, 
20         @frequency_subday_interval = 0, @active_start_time_of_day = 0, @active_end_time_of_day = 235959, 
21         @active_start_date = @start_date, @active_end_date = 99991231, @enabled_for_syncmgr = N'False', 
22         @dts_package_location = N'Distributor'
23 END
View Code

删除发布订阅

 1 ---- 删除事务发布 ---
 2 EXEC sp_droppublication @publication = 'all';
 3 
 4 ---- 删除订阅 ----
 5 EXEC sp_dropsubscription 
 6   @publication = 'uuu', 
 7   @article = N'all',
 8   @subscriber = 'WHY';
 9   
10 exec  sp_subscription_cleanup @publisher='PC-201311211015\SHICH',@publisher_db = 'erp',@publication = 'uuu'

 

posted @ 2015-05-14 16:04  hewep  阅读(171)  评论(0)    收藏  举报