Skip to content

Commit

Permalink
0003901: MSSQL: Updating multiple rows in source table results in
Browse files Browse the repository at this point in the history
reversed rows in destination table
  • Loading branch information
philipmarzullo64 committed Sep 13, 2019
1 parent 538ba15 commit 671e53f
Show file tree
Hide file tree
Showing 3 changed files with 230 additions and 137 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,37 @@ public AseTriggerTemplate(ISymmetricDialect symmetricDialect) {
" $(declareNewKeyVariables) \n" +
" $(custom_before_update_text) \n" +
" if ($(syncOnIncomingBatchCondition)) begin \n" +
" if ($(hasPrimaryKeysDefined) $(primaryKeysUpdated) ) \n" +
" begin \n" +
" declare DeleteDataCursor cursor for \n" +
" select $(oldKeys), $(oldColumns) $(oldKeyNames), $(specialSqlServerSybaseChannelExpression) from deleted where $(syncOnDeleteCondition) \n" +
" open DeleteDataCursor \n" +
" fetch DeleteDataCursor into @OldPk, @OldDataRow $(oldKeyVariables), @ChannelId \n" +
" while @@sqlstatus = 0 begin \n" +
" insert into $(defaultCatalog)$(defaultSchema)$(prefixName)_data (table_name, event_type, trigger_hist_id, pk_data, old_data, channel_id, transaction_id, source_node_id, external_data, create_time) \n" +
" values('$(targetTableName)','D', $(triggerHistoryId), @OldPk, @OldDataRow, @ChannelId, @txid, @clientname, $(externalSelect), getdate())\n" +
" fetch DeleteDataCursor into @OldPk,@OldDataRow $(oldKeyVariables), @ChannelId \n" +
" end \n" +
" close DeleteDataCursor \n" +
" deallocate cursor DeleteDataCursor \n" +
" declare InsertDataCursor cursor for \n" +
" $(if:containsBlobClobColumns) \n" +
" select $(columns) $(newKeyNames), $(channelExpression) from inserted inner join $(schemaName)$(tableName) $(origTableAlias) on $(tableNewPrimaryKeyJoin) where $(syncOnInsertCondition)\n" +
" $(else:containsBlobClobColumns) \n" +
" select $(columns) $(newKeyNames), $(channelExpression) from inserted where $(syncOnInsertCondition) \n" +
" $(end:containsBlobClobColumns) \n" +
" open InsertDataCursor \n" +
" fetch InsertDataCursor into @DataRow $(newKeyVariables), @ChannelId \n" +
" while @@sqlstatus = 0 begin \n" +
" insert into $(defaultCatalog)$(defaultSchema)$(prefixName)_data (table_name, event_type, trigger_hist_id, row_data, channel_id, transaction_id, source_node_id, external_data, create_time) \n" +
" values('$(targetTableName)','I', $(triggerHistoryId), @DataRow, @ChannelId, @txid, @clientname, $(externalSelect), getdate()) \n" +
" fetch InsertDataCursor into @DataRow $(newKeyVariables), @ChannelId \n" +
" end \n" +
" close InsertDataCursor \n" +
" deallocate cursor InsertDataCursor \n" +
" end \n" +
" else \n" +
" begin \n" +
" declare DataCursor cursor for \n" +
" $(if:containsBlobClobColumns) \n" +
" select $(columns), $(oldKeys), $(oldColumns) $(oldKeyNames) $(newKeyNames), $(channelExpression) from inserted inner join $(schemaName)$(tableName) $(origTableAlias) on $(tableNewPrimaryKeyJoin) inner join deleted on $(oldNewPrimaryKeyJoin) where $(syncOnUpdateCondition)\n" +
Expand All @@ -135,59 +166,10 @@ public AseTriggerTemplate(ISymmetricDialect symmetricDialect) {
" close DataCursor \n" +
" deallocate cursor DataCursor \n" +
" end \n" +
" $(custom_on_update_text) \n" +
" set nocount off \n" +
" end " );

sqlTemplates.put("updateHandleKeyUpdatesTriggerTemplate" ,
"create trigger $(triggerName) on $(schemaName)$(tableName) for update as \n" +
" begin \n" +
" set nocount on \n" +
" declare @TransactionId varchar(1000) \n" +
" declare @DataRow varchar(16384) \n" +
" declare @OldPk varchar(2000) \n" +
" declare @OldDataRow varchar(16384) \n" +
" declare @clientapplname varchar(50) \n" +
" select @clientapplname = clientapplname from master.dbo.sysprocesses where spid = @@spid \n" +
" declare @clientname varchar(50) \n" +
" select @clientname = clientname from master.dbo.sysprocesses where spid = @@spid and clientapplname = 'SymmetricDS' \n" +
" declare @ChannelId varchar(128) \n" +
" $(declareOldKeyVariables) \n" +
" $(declareNewKeyVariables) \n" +
" declare @txid varchar(50) \n" +
" if (@@TRANCOUNT > 0) begin \n" +
" select @txid = $(txIdExpression) \n" +
" end \n" +
" $(custom_before_update_text) \n" +
" if ($(syncOnIncomingBatchCondition)) begin \n" +
" declare DeleteCursor cursor for \n" +
" select $(oldKeys), $(oldColumns) $(oldKeyNames) from deleted where $(syncOnDeleteCondition) \n" +
" declare InsertCursor cursor for \n" +
" $(if:containsBlobClobColumns) \n" +
" select $(columns) $(newKeyNames), $(channelExpression) from inserted inner join $(schemaName)$(tableName) $(origTableAlias) on $(tableNewPrimaryKeyJoin) where $(syncOnInsertCondition)\n" +
" $(else:containsBlobClobColumns) \n" +
" select $(columns) $(newKeyNames), $(channelExpression) from inserted where $(syncOnInsertCondition) \n" +
" $(end:containsBlobClobColumns) \n" +
" open DeleteCursor \n" +
" open InsertCursor \n" +
" fetch DeleteCursor into @OldPk, @OldDataRow $(oldKeyVariables) \n" +
" fetch InsertCursor into @DataRow $(newKeyVariables), @ChannelId \n" +
" while @@sqlstatus = 0 begin \n" +
" if ($(dataHasChangedCondition)) begin \n" +
" insert into $(defaultCatalog)$(defaultSchema)$(prefixName)_data (table_name, event_type, trigger_hist_id, row_data, pk_data, old_data, channel_id, transaction_id, source_node_id, external_data, create_time) \n" +
" values('$(targetTableName)','U', $(triggerHistoryId), @DataRow, @OldPk, @OldDataRow, @ChannelId, @txid, @clientname, $(externalSelect), getdate())\n" +
" end \n" +
" fetch DeleteCursor into @OldPk, @OldDataRow $(oldKeyVariables) \n" +
" fetch InsertCursor into @DataRow $(newKeyVariables), @ChannelId \n" +
" end \n" +
" close DeleteCursor \n" +
" close InsertCursor \n" +
" deallocate cursor DeleteCursor \n" +
" deallocate cursor InsertCursor \n" +
" end \n" +
" $(custom_on_update_text) \n" +
" set nocount off \n" +
" end \n"
" end \n" +
" $(custom_on_update_text) \n" +
" set nocount off \n" +
" end \n"
);


Expand Down
Loading

0 comments on commit 671e53f

Please sign in to comment.