1

レプリケーションを使用してセットアップされた SQL Server 2008 インスタンスがあります。展開を行うとき、すべてのビュー、プロシージャ、および関数がレプリカに複製されることを確認したいと考えています。つまり、新しいビュー、プロシージャ、または関数を追加する場合、それをレプリケーションに追加する必要がありますよね?

展開するときに 2 週間ごとにこれを手動で実行したくはありませんが、これらすべてを T-SQL で実行できるはずです。すべてのビュー、プロシージャ、および関数をレプリケーションに追加するために実行できるスクリプトはありますか?

4

2 に答える 2

1

私がこれを行うとしたら、次のようにします。

  1. スキーマをレプリケートするオブジェクトのタイプの DDL トリガーまたはイベント通知を作成します。どちらの方法でも、プロセスはメッセージを Service Broker キューに入れます。
  2. キューからメッセージをポップし、適切なパラメーターを指定して sp_addarticle および sp_addsubscription を呼び出すプロシージャを作成します (これらは単なるスキーマ ベースの記事であるため、それほど悪くはありません)。
  3. 手順をブローカ キューのアクティブ化手順にする
  4. スケジュールに従って、スナップショット エージェントを呼び出します。パブリケーションが即時同期に設定されていないことを確認してください。そうしないと、すべての記事のスナップショットが生成されます。新しいものだけが欲しい。
于 2013-02-26T02:45:04.617 に答える
0

私の知る限り、「ルール ベース」のレプリケーションはありません。

各テーブルを「コード化」する必要があります。

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' 

サンプルですのでご注意ください。1 つのテーブルの列を取得します。

「スクリプト アウト」を行う場合は、SchemaOptionValue に注意を払う必要があります。これは、私が昔書いた「チェッカー」です。(値を指定すると、小さなレポートが表示されます)

これは、レプリケーションをスクリプト化する最大の理由 (IMHO) です。非常に多くのオプションがあります。記憶から同じことを 2 回行うことはないと思います。

--------------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 ''
于 2013-02-25T21:54:52.170 に答える