Sql Server 2005 复制:如何使用 SQL 脚本设置订阅?

Sql Server 2005 复制:如何使用 SQL 脚本设置订阅?

在执行复制设置向导时,我可以在最后选择生成脚本。该脚本对于发布设置来说运行良好,但是,当我运行生成的订阅设置脚本时,我遇到了问题,复制不起作用。但是,如果我不生成脚本,而只是让向导本身启动复制,那么一切都会正常。我真的很想用脚本自动完成订阅设置。

生成的订阅脚本为:

-----------------BEGIN: Script to be run at Publisher 'SERVER\INSTANCE'-----------------
use [PublisherDatabase]
exec sp_addsubscription @publication = N'PublicationName', @subscriber = N'server\instance', @destination_db = N'SubscriberDatabase', @subscription_type = N'Push', @sync_type = N'automatic', @article = N'all', @update_mode = N'read only', @subscriber_type = 0
exec sp_addpushsubscription_agent @publication = N'PublicationName', @subscriber = N'server\instance', @subscriber_db = N'SubscriberDatabase', @job_login = null, @job_password = null, @subscriber_security_mode = 1, @frequency_type = 64, @frequency_interval = 0, @frequency_relative_interval = 0, @frequency_recurrence_factor = 0, @frequency_subday = 0, @frequency_subday_interval = 0, @active_start_time_of_day = 0, @active_end_time_of_day = 235959, @active_start_date = 20091028, @active_end_date = 99991231, @enabled_for_syncmgr = N'False', @dts_package_location = N'Distributor'
GO
-----------------END: Script to be run at Publisher 'FLANDERS\TESTING'-----------------

当我运行上述脚本时,它完成且没有错误。但是,订阅从未出现在 Sql Server Management Studio 复制->本地订阅树中,并且数据从未被复制。

然而奇怪的是,运行查询:

use distribution
select * from MSSubscriptions

给出结果集:

publisher_database_id publisher_id publisher_db                                                                                                                     publication_id article_id  subscriber_id subscriber_db                                                                                                                    subscription_type sync_type status subscription_seqno                 snapshot_seqno_flag independent_agent subscription_time       loopback_detection agent_id    update_mode publisher_seqno                    ss_cplt_seqno
--------------------- ------------ -------------------------------------------------------------------------------------------------------------------------------- -------------- ----------- ------------- -------------------------------------------------------------------------------------------------------------------------------- ----------------- --------- ------ ---------------------------------- ------------------- ----------------- ----------------------- ------------------ ----------- ----------- ---------------------------------- ----------------------------------
3                     0            PublisherDatabase                                                                                                                3              1           -1            virtual                                                                                                                          0                 1         1      0x00000027000001230003             0                   1                 2009-10-29 10:41:37.540 1                  10          0           0x00000027000001230003             0x00000027000001230003
3                     0            PublisherDatabase                                                                                                                3              1           -2            virtual                                                                                                                          0                 1         2      0x00000027000001230003             0                   1                 2009-10-29 10:41:37.603 1                  11          0           0x00000027000001230009             0x00000027000001230009
3                     0            PublisherDatabase                                                                                                                3              1           0             SubscriberDatabase                                                                                                               0                 1         1      0x000000270000013B0008             0                   1                 2009-10-29 10:54:58.140 1                  12          0           0x000000270000013B0008             0x000000270000013B0008

这似乎表明订阅已存在。

如果重要的话,这是一个仅推送的发布/订阅,并且发布者和订阅者数据库都在同一台服务器上。

我还需要做什么才能通过 SQL 脚本使订阅正常工作?

编辑:交叉发布至http://www.sqlservercentral.com/Forums/Topic811043-291-1.aspx

答案1

搞清楚了。直接从 Management Studio 生成订阅时,它会自动启动快照代理。但是,Management Studio 为订阅生成的脚本不是启动快照代理。将以下行添加到生成的脚本中即可使其正常工作:

EXEC sp_startpublication_snapshot @publication = N'InstitutionPublication'

因此,修改后订阅者的完整脚本如下:

-----------------BEGIN: Script to be run at Publisher 'SERVER\INSTANCE'-----------------
-----------------BEGIN: Script to be run at Publisher 'SERVER\INSTANCE'-----------------
use [Profile]
exec sp_addsubscription @publication = N'PublicationName', @subscriber = N'server\instance', @destination_db = N'destinationDatabaseName', @subscription_type = N'Push', @sync_type = N'automatic', @article = N'all', @update_mode = N'read only', @subscriber_type = 0
exec sp_addpushsubscription_agent @publication = N'PublicationName', @subscriber = N'server\instance', @subscriber_db = N'destinationDatabaseName', @job_login = null, @job_password = null, @subscriber_security_mode = 1, @frequency_type = 64, @frequency_interval = 0, @frequency_relative_interval = 0, @frequency_recurrence_factor = 0, @frequency_subday = 0, @frequency_subday_interval = 0, @active_start_time_of_day = 0, @active_end_time_of_day = 235959, @active_start_date = 20091104, @active_end_date = 99991231, @enabled_for_syncmgr = N'False', @dts_package_location = N'Distributor'
GO
-----------------END: Script to be run at Publisher 'SERVER\INSTANCE'-----------------

EXEC sp_startpublication_snapshot @publication = N'PublicationName';
GO

相关内容