Original: SQL Server transactional replication uses scripts to add publications for an object
--use [Publish Library]-Add Table: Create the project and add it to the publication exec sp_addarticle @publication = n ' replicationname ', @article = N ' MyObject ', @source_ Owner = n ' dbo ', @source_object = N ' MyObject ', @destination_owner = n ' dbo ', @destination_table = N ' MyObject ', @schema_optio n = 0x0000004008037fdf,---project properties (self reference) @status = 0, @description = n ",--default@type = N ' logbased ',--default@creation_scrip t = null,--default@pre_creation_cmd = n ' Drop ',--default@vertical_partition = N ' false ',-- default@identityrangemanagementoption = n ' manual ',--default@force_invalidate_snapshot = 1, @ins_cmd = N ' Call sp_MSins_ Dbomyobject ', @del_cmd = n ' Call sp_msdel_dbomyobject ', @upd_cmd = n ' scall sp_msupd_dbomyobject ' go--add view exec sp_ Addarticle @publication = n ' replicationname ', @article = N ' v_myview ', @source_owner = n ' dbo ', @source_object = n ' v_myview ' , @destination_owner = n ' dbo ', @destination_table = N ' v_myview ', @type = N ' view schema only ', @description = null, @creatio N_script = null, @pre_creation_cmd = N ' drop ', @schema_option = 0x0000000008000001, @force_invalidate_snapshot = 1go--Add stored procedure exec sp_addarticle @publication = n ' replicationname ', @article = N ' usp_ MyProc ', @source_owner = n ' dbo ', @source_object = N ' usp_myproc ', @destination_owner = n ' dbo ', @destination_table = N ' usp_m Yproc ', @type = N ' proc schema only ', @description = null, @creation_script = null, @pre_creation_cmd = n ' Drop ', @schema_op tion = 0x0000000008000001, @force_invalidate_snapshot = 1go--add function exec sp_addarticle @publication = N ' Replicationname ', @ Article = n ' f_myfunc ', @source_owner = n ' dbo ', @source_object = N ' f_myfunc ', @destination_owner = n ' dbo ', @destination_tab Le = N ' f_myfunc ', @type = N ' func schema only ', @description = null, @creation_script = null, @pre_creation_cmd = n ' Drop ', @schema_option = 0x0000000008000001, @force_invalidate_snapshot = 1 go/* Adding views/stored procedures/functions is mostly @type different: @type = N ' View schema O Nly ',--View @type = N ' proc schema only ',--stored procedure @type = N ' func schema only ',--function */--the subscription to the new item in its pull subscription is added to the publication exec SP_REFRESHSUBSC Riptions @publication = N ' ReplIcationname ' go--start the Snapshot Agent exec sys.sp_startpublication_snapshot @publication = N ' replicationname ' go/*--remove the Publish project ( i.e. cancel synchronization of an object) exec sp_dropsubscription @publication = n ' replicationname ', @article = N ' MyObject ', @subscriber = n ' All ' goexec sp_droparticle @publication = n ' replicationname ', @article = N ' MyObject ', @force_invalidate_snapshot = 0go*/
Using scripts to add a publication for an object in SQL Server transactional replication