我有一个设置了复制的 SQL Server 2008 实例。当我们进行部署时,我想确保所有视图、过程和函数都复制到副本中。这意味着如果我添加一个新的视图、过程或函数,我需要将它添加到复制中,对吗?
我真的不想在我们部署时每 2 周手动执行一次,但似乎您应该能够在 T-SQL 中完成所有这些操作。是否有可以运行的脚本将所有视图、过程和函数添加到复制中?
我有一个设置了复制的 SQL Server 2008 实例。当我们进行部署时,我想确保所有视图、过程和函数都复制到副本中。这意味着如果我添加一个新的视图、过程或函数,我需要将它添加到复制中,对吗?
我真的不想在我们部署时每 2 周手动执行一次,但似乎您应该能够在 T-SQL 中完成所有这些操作。是否有可以运行的脚本将所有视图、过程和函数添加到复制中?
如果我要这样做,我会这样做:
据我所知,没有“基于规则”的复制。
您必须“编码”每个表。
当您浏览 GUI 时,它将使您能够“生成脚本”。
您可以生成脚本。并将其用作基线。但是您仍然需要对其进行调整,(也就是添加新表、对象等)到脚本中。
复制应该通过 tsql 脚本完成,恕我直言,因为忘记设置的选项太多了。
这是一个例子:
use [AdventureWorks]
exec sp_addarticle @publication = N'AdventureWorksPublication2', @article =
N'Address', @source_owner = N'Person', @source_object = N'Address', @type =
N'logbased', @description = null, @creation_script = null, @pre_creation_cmd
= N'drop', @schema_option = 0x000000000803589F,
@identityrangemanagementoption = N'manual', @destination_table = N'Address',
@destination_owner = N'Person', @vertical_partition = N'true', @ins_cmd =
N'CALL sp_MSins_PersonAddress', @del_cmd = N'CALL sp_MSdel_PersonAddress',
@upd_cmd = N'SCALL sp_MSupd_PersonAddress'
-- Adding the article's partition column(s)
exec sp_articlecolumn @publication = N'AdventureWorksPublication2', @article
= N'Address', @column = N'AddressID', @operation = N'add',
@force_invalidate_snapshot = 1, @force_reinit_subscription = 1
exec sp_articlecolumn @publication = N'AdventureWorksPublication2', @article
= N'Address', @column = N'AddressLine1', @operation = N'add',
@force_invalidate_snapshot = 1, @force_reinit_subscription = 1
exec sp_articlecolumn @publication = N'AdventureWorksPublication2', @article
= N'Address', @column = N'AddressLine2', @operation = N'add',
@force_invalidate_snapshot = 1, @force_reinit_subscription = 1
exec sp_articlecolumn @publication = N'AdventureWorksPublication2', @article
= N'Address', @column = N'City', @operation = N'add',
@force_invalidate_snapshot = 1, @force_reinit_subscription = 1
exec sp_articlecolumn @publication = N'AdventureWorksPublication2', @article
= N'Address', @column = N'StateProvinceID', @operation = N'add',
@force_invalidate_snapshot = 1, @force_reinit_subscription = 1
exec sp_articlecolumn @publication = N'AdventureWorksPublication2', @article
= N'Address', @column = N'PostalCode', @operation = N'add',
@force_invalidate_snapshot = 1, @force_reinit_subscription = 1
exec sp_articlecolumn @publication = N'AdventureWorksPublication2', @article
= N'Address', @column = N'rowguid', @operation = N'add',
@force_invalidate_snapshot = 1, @force_reinit_subscription = 1
-- Adding the article synchronization object
exec sp_articleview @publication = N'AdventureWorksPublication2', @article =
N'Address', @view_name = N'SYNC_Address_1__64', @filter_clause = null,
@force_invalidate_snapshot = 1, @force_reinit_subscription = 1
GO
http://msdn.microsoft.com/en-us/library/ms173857.aspx
现在,您可以编写编写代码的代码。
declare @publicationName varchar(64)
select @publicationName = 'AdventureWorksPublication2'
select 'exec sp_articlecolumn @publication = N' + char(39) + 'AdventureWorksPublication2' + char(39) + ', @article = N' + char(39) + TABLE_NAME + char(39) + ', @column = N' + char(39) + COLUMN_NAME + char(39) + ', @operation = N' + char(39) + 'add' + char(39) + ', @force_invalidate_snapshot = 1, @force_reinit_subscription = 1'
from INFORMATION_SCHEMA.COLUMNS IC
where TABLE_NAME = 'Address'
请注意,这是一个样本。它获取一个表的列。
如果/当您执行“脚本输出”时,您需要注意 SchemaOptionValue 这是我以前写的一个“检查器”。(你给它一个值,它会给你一个小报告)
这是编写复制脚本的第一个原因(恕我直言)。有很多选择,我认为你永远不会从记忆中做同样的事情两次。
--------------START TSQL
set nocount on
declare @CurrentOptionCompareValue int
declare @SchemaOptionValue int
select @SchemaOptionValue = 0x000000000803589F --<<Substitute your value
here
select @CurrentOptionCompareValue = 0x00
print 'Disables scripting by the Snapshot Agent and uses creation_script. '
if ( @CurrentOptionCompareValue & @SchemaOptionValue ) > 0 begin print
'Above Value is ON' end else begin print 'Above value is OFF' end
print ''
select @CurrentOptionCompareValue = 0x01
print 'Generates the object creation script (CREATE TABLE, CREATE PROCEDURE,
and so on). This value is the default for stored procedure articles. '
if ( @CurrentOptionCompareValue & @SchemaOptionValue ) > 0 begin print
'Above Value is ON' end else begin print 'Above value is OFF' end
print ''
select @CurrentOptionCompareValue = 0x02
print 'Generates the stored procedures that propagate changes for the
article, if defined. '
if ( @CurrentOptionCompareValue & @SchemaOptionValue ) > 0 begin print
'Above Value is ON' end else begin print 'Above value is OFF' end
print ''
select @CurrentOptionCompareValue = 0x04
print 'Identity columns are scripted using the IDENTITY property. '
if ( @CurrentOptionCompareValue & @SchemaOptionValue ) > 0 begin print
'Above Value is ON' end else begin print 'Above value is OFF' end
print ''
select @CurrentOptionCompareValue = 0x08
print 'Replicate timestamp columns. If not set, timestamp columns are
replicated as binary. '
if ( @CurrentOptionCompareValue & @SchemaOptionValue ) > 0 begin print
'Above Value is ON' end else begin print 'Above value is OFF' end
print ''
select @CurrentOptionCompareValue = 0x10
print 'Generates a corresponding clustered index. Even if this option is not
set, indexes related to primary keys and unique constraints are generated if
they are already defined on a published table. '
if ( @CurrentOptionCompareValue & @SchemaOptionValue ) > 0 begin print
'Above Value is ON' end else begin print 'Above value is OFF' end
print ''
select @CurrentOptionCompareValue = 0x20
print 'Converts user-defined data types (UDT) to base data types at the
Subscriber. This option cannot be used when there is a CHECK or DEFAULT
constraint on a UDT column, if a UDT column is part of the primary key, or
if a computed column references a UDT column. Not supported for Oracle
Publishers. '
if ( @CurrentOptionCompareValue & @SchemaOptionValue ) > 0 begin print
'Above Value is ON' end else begin print 'Above value is OFF' end
print ''
select @CurrentOptionCompareValue = 0x40
print 'Generates corresponding nonclustered indexes. Even if this option is
not set, indexes related to primary keys and unique constraints are
generated if they are already defined on a published table. '
if ( @CurrentOptionCompareValue & @SchemaOptionValue ) > 0 begin print
'Above Value is ON' end else begin print 'Above value is OFF' end
print ''
select @CurrentOptionCompareValue = 0x80
print 'Replicates primary key constraints. Any indexes related to the
constraint are also replicated, even if options select
@CurrentOptionCompareValue = 0x10 and select @CurrentOptionCompareValue =
0x40 are not enabled. '
if ( @CurrentOptionCompareValue & @SchemaOptionValue ) > 0 begin print
'Above Value is ON' end else begin print 'Above value is OFF' end
print ''
select @CurrentOptionCompareValue = 0x100
print 'Replicates user triggers on a table article, if defined. Not
supported for Oracle Publishers. '
if ( @CurrentOptionCompareValue & @SchemaOptionValue ) > 0 begin print
'Above Value is ON' end else begin print 'Above value is OFF' end
print ''
select @CurrentOptionCompareValue = 0x200
print 'Replicates foreign key constraints. If the referenced table is not
part of a publication, all foreign key constraints on a published table are
not replicated. Not supported for Oracle Publishers. '
if ( @CurrentOptionCompareValue & @SchemaOptionValue ) > 0 begin print
'Above Value is ON' end else begin print 'Above value is OFF' end
print ''
select @CurrentOptionCompareValue = 0x400
print 'Replicates check constraints. Not supported for Oracle Publishers. '
if ( @CurrentOptionCompareValue & @SchemaOptionValue ) > 0 begin print
'Above Value is ON' end else begin print 'Above value is OFF' end
print ''
select @CurrentOptionCompareValue = 0x800
print 'Replicates defaults. Not supported for Oracle Publishers. '
if ( @CurrentOptionCompareValue & @SchemaOptionValue ) > 0 begin print
'Above Value is ON' end else begin print 'Above value is OFF' end
print ''
select @CurrentOptionCompareValue = 0x1000
-- Replicates column-level collation.
print 'Note: This option should be set for Oracle Publishers to enable
case-sensitive comparisons. '
if ( @CurrentOptionCompareValue & @SchemaOptionValue ) > 0 begin print
'Above Value is ON' end else begin print 'Above value is OFF' end
print ''
select @CurrentOptionCompareValue = 0x2000
print 'Replicates extended properties associated with the published article
source object. Not supported for Oracle Publishers. '
if ( @CurrentOptionCompareValue & @SchemaOptionValue ) > 0 begin print
'Above Value is ON' end else begin print 'Above value is OFF' end
print ''
select @CurrentOptionCompareValue = 0x4000
print 'Replicates UNIQUE constraints. Any indexes related to the constraint
are also replicated, even if options select @CurrentOptionCompareValue =
0x10 and select @CurrentOptionCompareValue = 0x40 are not enabled. '
if ( @CurrentOptionCompareValue & @SchemaOptionValue ) > 0 begin print
'Above Value is ON' end else begin print 'Above value is OFF' end
print ''
select @CurrentOptionCompareValue = 0x8000
print 'This option is not valid for SQL Server 2005 Publishers. '
if ( @CurrentOptionCompareValue & @SchemaOptionValue ) > 0 begin print
'Above Value is ON' end else begin print 'Above value is OFF' end
print ''
select @CurrentOptionCompareValue = 0x10000
print 'Replicates CHECK constraints as NOT FOR REPLICATION so that the
constraints are not enforced during synchronization. '
if ( @CurrentOptionCompareValue & @SchemaOptionValue ) > 0 begin print
'Above Value is ON' end else begin print 'Above value is OFF' end
print ''
select @CurrentOptionCompareValue = 0x20000
print 'Replicates FOREIGN KEY constraints as NOT FOR REPLICATION so that the
constraints are not enforced during synchronization. '
if ( @CurrentOptionCompareValue & @SchemaOptionValue ) > 0 begin print
'Above Value is ON' end else begin print 'Above value is OFF' end
print ''
select @CurrentOptionCompareValue = 0x40000
print 'Replicates filegroups associated with a partitioned table or index. '
if ( @CurrentOptionCompareValue & @SchemaOptionValue ) > 0 begin print
'Above Value is ON' end else begin print 'Above value is OFF' end
print ''
select @CurrentOptionCompareValue = 0x80000
print 'Replicates the partition scheme for a partitioned table. '
if ( @CurrentOptionCompareValue & @SchemaOptionValue ) > 0 begin print
'Above Value is ON' end else begin print 'Above value is OFF' end
print ''
select @CurrentOptionCompareValue = 0x100000
print 'Replicates the partition scheme for a partitioned index. '
if ( @CurrentOptionCompareValue & @SchemaOptionValue ) > 0 begin print
'Above Value is ON' end else begin print 'Above value is OFF' end
print ''
select @CurrentOptionCompareValue = 0x200000
print 'Replicates table statistics. '
if ( @CurrentOptionCompareValue & @SchemaOptionValue ) > 0 begin print
'Above Value is ON' end else begin print 'Above value is OFF' end
print ''
select @CurrentOptionCompareValue = 0x400000
print 'Default Bindings '
if ( @CurrentOptionCompareValue & @SchemaOptionValue ) > 0 begin print
'Above Value is ON' end else begin print 'Above value is OFF' end
print ''
select @CurrentOptionCompareValue = 0x800000
print 'Rule Bindings '
if ( @CurrentOptionCompareValue & @SchemaOptionValue ) > 0 begin print
'Above Value is ON' end else begin print 'Above value is OFF' end
print ''
select @CurrentOptionCompareValue = 0x1000000
print 'Full-text index '
if ( @CurrentOptionCompareValue & @SchemaOptionValue ) > 0 begin print
'Above Value is ON' end else begin print 'Above value is OFF' end
print ''
select @CurrentOptionCompareValue = 0x2000000
print 'XML schema collections bound to xml columns are not replicated. '
if ( @CurrentOptionCompareValue & @SchemaOptionValue ) > 0 begin print
'Above Value is ON' end else begin print 'Above value is OFF' end
print ''
select @CurrentOptionCompareValue = 0x4000000
print 'Replicates indexes on xml columns. '
if ( @CurrentOptionCompareValue & @SchemaOptionValue ) > 0 begin print
'Above Value is ON' end else begin print 'Above value is OFF' end
print ''
select @CurrentOptionCompareValue = 0x8000000
print 'Create any schemas not already present on the subscriber. '
if ( @CurrentOptionCompareValue & @SchemaOptionValue ) > 0 begin print
'Above Value is ON' end else begin print 'Above value is OFF' end
print ''
select @CurrentOptionCompareValue = 0x10000000
print 'Converts xml columns to ntext on the Subscriber. '
if ( @CurrentOptionCompareValue & @SchemaOptionValue ) > 0 begin print
'Above Value is ON' end else begin print 'Above value is OFF' end
print ''
select @CurrentOptionCompareValue = 0x20000000
print 'Converts large object data types (nvarchar(max), varchar(max), and
varbinary(max)) introduced in SQL Server 2005 to data types that are
supported on SQL Server 2000. For information about how these types are
mapped, see the "Mapping New Data Types for Earlier Versions" section in
Using Multiple Versions of SQL Server in a Replication Topology. '
if ( @CurrentOptionCompareValue & @SchemaOptionValue ) > 0 begin print
'Above Value is ON' end else begin print 'Above value is OFF' end
print ''
select @CurrentOptionCompareValue = 0x40000000
print 'Replicate permissions. '
if ( @CurrentOptionCompareValue & @SchemaOptionValue ) > 0 begin print
'Above Value is ON' end else begin print 'Above value is OFF' end
print ''
select @CurrentOptionCompareValue = 0x80000000
print 'Attempt to drop dependencies to any objects that are not part of the
publication. '
if ( @CurrentOptionCompareValue & @SchemaOptionValue ) > 0 begin print
'Above Value is ON' end else begin print 'Above value is OFF' end
print ''
select @CurrentOptionCompareValue = 0x100000000
print 'Use this option to replicate the FILESTREAM attribute if it is
specified on varbinary(max) columns. Do not specify this option if you are
replicating tables to SQL Server 2005 Subscribers. Replicating tables that
have FILESTREAM columns to SQL Server 2000 Subscribers is not supported,
regardless of how this schema option is set. '
-- See related option select @CurrentOptionCompareValue = 0x800000000.
if ( @CurrentOptionCompareValue & @SchemaOptionValue ) > 0 begin print
'Above Value is ON' end else begin print 'Above value is OFF' end
print ''
select @CurrentOptionCompareValue = 0x200000000
print 'Converts date and time data types (date, time, datetimeoffset, and
datetime2) introduced in SQL Server 2008 to data types that are supported on
earlier versions of SQL Server. For information about how these types are
mapped, see the "Mapping New Data Types for Earlier Versions" section in
Using Multiple Versions of SQL Server in a Replication Topology. '
if ( @CurrentOptionCompareValue & @SchemaOptionValue ) > 0 begin print
'Above Value is ON' end else begin print 'Above value is OFF' end
print ''
select @CurrentOptionCompareValue = 0x400000000
print 'Replicates the compression option for data and indexes. For more
information, see Creating Compressed Tables and Indexes. '
if ( @CurrentOptionCompareValue & @SchemaOptionValue ) > 0 begin print
'Above Value is ON' end else begin print 'Above value is OFF' end
print ''
select @CurrentOptionCompareValue = 0x800000000
print 'Set this option to store FILESTREAM data on its own filegroup at the
Subscriber. If this option is not set, FILESTREAM data is stored on the
default filegroup. Replication does not create filegroups; therefore, if you
set this option, you must create the filegroup before you apply the snapshot
at the Subscriber. For more information about how to create objects before
you apply the snapshot, see Executing Scripts Before and After the Snapshot
Is Applied. '
-- See related option select @CurrentOptionCompareValue = 0x100000000.
if ( @CurrentOptionCompareValue & @SchemaOptionValue ) > 0 begin print
'Above Value is ON' end else begin print 'Above value is OFF' end
print ''
select @CurrentOptionCompareValue = 0x1000000000
print 'Converts common language runtime (CLR) user-defined types (UDTs) that
are larger than 8000 bytes to varbinary(max) so that columns of type UDT can
be replicated to Subscribers that are running SQL Server 2005. '
if ( @CurrentOptionCompareValue & @SchemaOptionValue ) > 0 begin print
'Above Value is ON' end else begin print 'Above value is OFF' end
print ''
select @CurrentOptionCompareValue = 0x2000000000
print 'Converts the hierarchyid data type to varbinary(max) so that columns
of type hierarchyid can be replicated to Subscribers that are running SQL
Server 2005. For more information about how to use hierarchyid columns in
replicated tables, see hierarchyid (Transact-SQL). '
if ( @CurrentOptionCompareValue & @SchemaOptionValue ) > 0 begin print
'Above Value is ON' end else begin print 'Above value is OFF' end
print ''
select @CurrentOptionCompareValue = 0x4000000000
print 'Replicates any filtered indexes on the table. For more information
about filtered indexes, see Filtered Index Design Guidelines. '
if ( @CurrentOptionCompareValue & @SchemaOptionValue ) > 0 begin print
'Above Value is ON' end else begin print 'Above value is OFF' end
print ''
select @CurrentOptionCompareValue = 0x8000000000
print 'Converts the geography and geometry data types to varbinary(max) so
that columns of these types can be replicated to Subscribers that are
running SQL Server 2005. '
if ( @CurrentOptionCompareValue & @SchemaOptionValue ) > 0 begin print
'Above Value is ON' end else begin print 'Above value is OFF' end
print ''
select @CurrentOptionCompareValue = 0x10000000000
print 'Replicates indexes on columns of type geography and geometry. '
if ( @CurrentOptionCompareValue & @SchemaOptionValue ) > 0 begin print
'Above Value is ON' end else begin print 'Above value is OFF' end
print ''
select @CurrentOptionCompareValue = 0x20000000000
print 'Replicates the SPARSE attribute for columns. For more information
about this attribute, see Using Sparse Columns. '
if ( @CurrentOptionCompareValue & @SchemaOptionValue ) > 0 begin print
'Above Value is ON' end else begin print 'Above value is OFF' end
print ''