创建项目并将其添加到发布中。此存储过程是在发布服务器的发布数据库中执行的。
Transact-SQL 语法约定

sp_addarticle [ @publication = ] 'publication' , [ @article = ] 'article' [ , [ @source_table = ] 'source_table' ] [ , [ @destination_table = ] 'destination_table' ] [ , [ @vertical_partition = ] 'vertical_partition' ] [ , [ @type = ] 'type' ] [ , [ @filter = ] 'filter' ] [ , [ @sync_object= ] 'sync_object' ] [ , [ @ins_cmd = ] 'ins_cmd' ] [ , [ @del_cmd = ] 'del_cmd' ] [ , [ @upd_cmd = ] 'upd_cmd' ] [ , [ @creation_script = ] 'creation_script' ] [ , [ @description = ] 'description' ] [ , [ @pre_creation_cmd = ] 'pre_creation_cmd' ] [ , [ @filter_clause = ] 'filter_clause' ] [ , [ @schema_option = ] schema_option ] [ , [ @destination_owner = ] 'destination_owner' ] [ , [ @status = ] status ] [ , [ @source_owner = ] 'source_owner' ] [ , [ @sync_object_owner = ] 'sync_object_owner' ] [ , [ @filter_owner = ] 'filter_owner' ] [ , [ @source_object = ] 'source_object' ] [ , [ @artid = ] article_ID OUTPUT ] [ , [ @auto_identity_range = ] 'auto_identity_range' ] [ , [ @pub_identity_range = ] pub_identity_range ] [ , [ @identity_range = ] identity_range ] [ , [ @threshold = ] threshold ] [ , [ @force_invalidate_snapshot = ] force_invalidate_snapshot ] [ , [ @use_default_datatypes = ] use_default_datatypes [ , [ @identityrangemanagementoption = ] identityrangemanagementoption ] [ , [ @publisher = ] 'publisher' ] [ , [ @fire_triggers_on_snapshot = ] 'fire_triggers_on_snapshot' ]


0(成功)或 1(失败)

sp_addarticle 用于快照复制或事务复制。
默认情况下,如果复制不支持列数据类型,则复制不发布源表中的任何列。如果需要发布这样的列,则必须执行 sp_articlecolumn 才能添加列。
将项目添加到支持对等事务复制的发布中时,将应用下列限制:
-
必须为所有基于日志的项目指定参数化语句。必须在 status 值中包括 16。
-
目标表的名称和所有者必须与源表匹配。
-
不能水平或垂直筛选项目。
-
不支持自动标识范围管理。必须将 identityrangemanagementoption 的值指定为手动。
-
如果表中存在 timestamp 列,则必须在 schema_option 中包括 0x08,才能将列作为 timestamp 进行复制。
-
不能为 ins_cmd、upd_cmd 和 del_cmd 指定值 SQL。
有关详细信息,请参阅对等事务复制。
发布对象时,对象的定义会复制到订阅服务器。如果要发布的数据库对象依赖于一个或多个其他对象,则必须发布所有被引用对象。例如,如果要发布的视图依赖于一个表,则也必须发布该表。
如果 vertical_partition 设置为 true,则 sp_addarticle 将视图的创建推迟到调用 sp_articleview 时(添加最后一个 sp_articlecolumn 之后)。
如果发布允许更新订阅,并且已发布的表没有 uniqueidentifier 列,则 sp_addarticle 会自动将 uniqueidentifier 列添加到表中。
复制到不是 SQL Server 实例的订阅服务器(异类复制)时,仅对于 INSERT、UPDATE 和 DELETE 命令支持 Transact-SQL 语句。
运行日志读取器代理时,将项目添加到对等发布可能导致日志读取器代理和添加该项目的进程之间的死锁。为了避免此问题,在将项目添加到对等发布前,请使用复制监视器停止正在添加项目的节点上的日志读取器代理。在添加项目后,重新启动日志读取器代理。
默认架构选项
该表说明了当用户未指定 schema_options 时复制所设置的默认值,这种情况下,该值取决于复制类型(沿顶部显示)和项目类型(显示在第一列中)。
项目类型 |
复制类型 |
|
---|---|---|
事务性 |
快照 |
|
aggregate schema only |
0x01 |
0x01 |
func schema only |
0x01 |
0x01 |
indexed view schema only |
0x01 |
0x01 |
indexed view logbased |
0x30F3 |
0x3071 |
indexed view logbase manualboth |
0x30F3 |
0x3071 |
indexed view logbased manualfilter |
0x30F3 |
0x3071 |
indexed view logbased manualview |
0x30F3 |
0x3071 |
logbased |
0x30F3 |
0x3071 |
logbased manualfilter |
0x30F3 |
0x3071 |
logbased manualview |
0x30F3 |
0x3071 |
proc exec |
0x01 |
0x01 |
proc schema only |
0x01 |
0x01 |
serializable proc exec |
0x01 |
0x01 |
view schema only |
0x01 |
0x01 |
![]() |
---|
如果对排队更新启用发布,则将 schema_option 的值 0x80 添加到表中所显示的默认值中。对于非 SQL Server 发布,默认 schema_option 为 0x050D3。 |
有效架构选项
此表按复制类型(在顶部横向显示)和项目类型(在第一列纵向显示)说明了 schema_option 允许的值。
项目类型 |
复制类型 |
|
---|---|---|
事务 |
快照 |
|
logbased |
所有选项 |
除 0x02 之外的所有选项 |
logbased manualfilter |
所有选项 |
除 0x02 之外的所有选项 |
logbased manualview |
所有选项 |
除 0x02 之外的所有选项 |
indexed view logbased |
所有选项 |
除 0x02 之外的所有选项 |
indexed view logbased manualfilter |
所有选项 |
除 0x02 之外的所有选项 |
indexed view logbased manualview |
所有选项 |
除 0x02 之外的所有选项 |
indexed view logbase manualboth |
所有选项 |
除 0x02 之外的所有选项 |
proc exec |
0x01、0x20、0x2000、0x400000、0x800000、0x2000000、0x8000000、0x10000000、0x20000000、0x40000000 和 0x80000000 |
0x01、0x20、0x2000、0x400000、0x800000、0x2000000、0x8000000、0x10000000、0x20000000、0x40000000 和 0x80000000 |
serializable proc exec |
0x01、0x20、0x2000、0x400000、0x800000、0x2000000、0x8000000、0x10000000、0x20000000、0x40000000 和 0x80000000 |
0x01、0x20、0x2000、0x400000、0x800000、0x2000000、0x8000000、0x10000000、0x20000000、0x40000000 和 0x80000000 |
proc schema only |
0x01、0x20、0x2000、0x400000、0x800000、0x2000000、0x8000000、0x10000000、0x20000000、0x40000000 和 0x80000000 |
0x01、0x20、0x2000、0x400000、0x800000、0x2000000、0x8000000、0x10000000、0x20000000、0x40000000 和 0x80000000 |
view schema only |
0x01、0x010、0x020、0x040、0x0100、0x2000、0x40000、0x100000、0x200000、0x400000、0x800000、0x2000000、0x8000000、0x40000000 和 0x80000000 |
0x01、0x010、0x020、0x040、0x0100、0x2000、0x40000、0x100000、0x200000、0x400000、0x800000、0x2000000、0x8000000、0x40000000 和 0x80000000 |
func schema only |
0x01、0x20、0x2000、0x400000、0x800000、0x2000000、0x8000000、0x10000000、0x20000000、0x40000000 和 0x80000000 |
0x01、0x20、0x2000、0x400000、0x800000、0x2000000、0x8000000、0x10000000、0x20000000、0x40000000 和 0x80000000 |
indexed view schema only |
0x01、0x010、0x020、0x040、0x0100、0x2000、0x40000、0x100000、0x200000、0x400000、0x800000、0x2000000、0x8000000、0x40000000 和 0x80000000 |
0x01、0x010、0x020、0x040、0x0100、0x2000、0x40000、0x100000、0x200000、0x400000、0x800000、0x2000000、0x8000000、0x40000000 和 0x80000000 |
![]() |
---|
对于排队更新发布,必须启用 schema_option 的值 0x8000 和 0x80。非 SQL Server 发布支持的 schema_option 值包括:0x01、0x02、0x10、0x40、0x80、0x1000、0x4000 和 0X8000。 |

DECLARE @publication AS sysname; DECLARE @table AS sysname; DECLARE @filterclause AS nvarchar(500); DECLARE @filtername AS nvarchar(386); DECLARE @schemaowner AS sysname; SET @publication = N'AdvWorksProductTran'; SET @table = N'Product'; SET @filterclause = N'[DiscontinuedDate] IS NULL'; SET @filtername = N'filter_out_discontinued'; SET @schemaowner = N'Production'; -- Add a horizontally and vertically filtered article for the Product table. -- Manually set @schema_option to ensure that the Production schema -- is generated at the Subscriber (0x8000000). EXEC sp_addarticle @publication = @publication, @article = @table, @source_object = @table, @source_owner = @schemaowner, @schema_option = 0x80030F3, @vertical_partition = N'true', @type = N'logbased', @filter_clause = @filterclause; -- (Optional) Manually call the stored procedure to create the -- horizontal filtering stored procedure. Since the type is -- 'logbased', this stored procedures is executed automatically. EXEC sp_articlefilter @publication = @publication, @article = @table, @filter_clause = @filterclause, @filter_name = @filtername; -- Add all columns to the article. EXEC sp_articlecolumn @publication = @publication, @article = @table; -- Remove the DaysToManufacture column from the article EXEC sp_articlecolumn @publication = @publication, @article = @table, @column = N'DaysToManufacture', @operation = N'drop'; -- (Optional) Manually call the stored procedure to create the -- vertical filtering view. Since the type is 'logbased', -- this stored procedures is executed automatically. EXEC sp_articleview @publication = @publication, @article = @table, @filter_clause = @filterclause; GO

只有 sysadmin 固定服务器角色或 db_owner 固定数据库角色的成员才能执行 sp_addarticle。