<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>replications &#8211; SQLpowered.com</title>
	<atom:link href="https://sqlpowered.com/tag/replications/feed/" rel="self" type="application/rss+xml" />
	<link>https://sqlpowered.com</link>
	<description>SQL Server + BI</description>
	<lastBuildDate>Thu, 11 Mar 2021 07:34:32 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://sqlpowered.com/wp-content/uploads/2020/07/FavIcon-e1594067873682-99x100.png</url>
	<title>replications &#8211; SQLpowered.com</title>
	<link>https://sqlpowered.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Error in transactional replication: &#8216;Incorrect syntax near WHERE&#8217;</title>
		<link>https://sqlpowered.com/replications-incorrect-syntax-near-where/</link>
					<comments>https://sqlpowered.com/replications-incorrect-syntax-near-where/#respond</comments>
		
		<dc:creator><![CDATA[Jan Dvořák]]></dc:creator>
		<pubDate>Tue, 05 Dec 2017 21:05:25 +0000</pubDate>
				<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[errors]]></category>
		<category><![CDATA[replications]]></category>
		<guid isPermaLink="false">https://sqlpowered.com/?p=2294</guid>

					<description><![CDATA[Last week we have seen this error when we have initialized new Subscriber from snapshot that was generated for transactional replication after published database schema was updated: &#8216;An error has occurred: Incorrect syntax near where&#8217; This error occurred after about one hour of applying snapshot to subscribers database and caused...]]></description>
										<content:encoded><![CDATA[<p>Last week we have seen this error when we have initialized new Subscriber from snapshot that was generated for transactional replication after published database schema was updated:<br />
<span style="color: #ff0000;">&#8216;An error has occurred: Incorrect syntax near where&#8217; </span></p>
<p>This error occurred after about one hour of applying snapshot to subscribers database and caused restart of the whole process.</p>
<p>Investigation of error details in distribution database tables was lost time. First after monitoring of the full traffic against the subscriber database we have discovered that our error was caused by this statement:</p>
<pre class="lang:tsql mark:3 decode:true">EXEC(N'
CREATE UNIQUE NONCLUSTERED INDEX [UX_RecordCategory_Record_ID_Category_ID] ON [ts].[RecordCategory]([Record_ID],[Category_ID]) 
WITH (DATA_COMPRESSION = PAGE ) 
WHERE ([Record_ID] IS NOT NULL AND [Category_ID] IS NOT NULL)')</pre>
<p>What&#8217;s wrong there? It&#8217;s maybe not visible at the first look but yes, there is serious syntax error: WITH (DATA_COMPRESSION = PAGE ) should be after the WHERE first:</p>
<pre class="lang:tsql mark:4 decode:true ">EXEC(
N'CREATE UNIQUE NONCLUSTERED INDEX [UX_RecordCategory_Record_ID_Category_ID] ON [ts].[RecordCategory]([Record_ID],[Category_ID]) 
WHERE ([Record_ID] IS NOT NULL AND [Category_ID] IS NOT NULL) 
WITH (DATA_COMPRESSION = PAGE ) 
')</pre>
<p>This invalid statement was hidden in one of *.idx files in snapshot folder. *.idx files are executed as last during snapshot application so this is the reason why we were waiting for this error more than one hour (and 60 GB of data inserted to subscriber DB).</p>
<p>The general message for the whole audience is that you simply cannot generate valid snapshot in case that your database contains filtered indexes with data compression at the same time. MS guys should do better job when testing new features:)</p>
<p>You can check your database for this kind of indexes with this query:</p>
<pre class="lang:tsql decode:true">SELECT *
FROM sys.[indexes] [i]
INNER JOIN sys.[partitions] [p] ON [p].[object_id] = [i].[object_id] AND [p].[index_id] = [i].[index_id]
WHERE [i].[has_filter] = 1 AND [p].[data_compression_desc] &lt;&gt; 'NONE'</pre>
<p>I have created <a href="https://feedback.azure.com/forums/908035-sql-server/suggestions/32898463-replication-snapshot-is-invalid-when-published-dat">Microsoft Collaborate</a> item for this.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sqlpowered.com/replications-incorrect-syntax-near-where/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>How to Protect Transactional Replica Against Breaking Data Modifications?</title>
		<link>https://sqlpowered.com/how-to-protect-transactional-replica-against-breaking-data-modifications/</link>
					<comments>https://sqlpowered.com/how-to-protect-transactional-replica-against-breaking-data-modifications/#respond</comments>
		
		<dc:creator><![CDATA[Jan Dvořák]]></dc:creator>
		<pubDate>Fri, 28 Oct 2016 11:18:58 +0000</pubDate>
				<category><![CDATA[DBA]]></category>
		<category><![CDATA[replications]]></category>
		<guid isPermaLink="false">https://sqlpowered.com/?p=2129</guid>

					<description><![CDATA[In our scenario we have created transactional replica of our the production database to the staging machine where developers can prepare queries to be later executed in production. But in few days one of developers executed prepared DELETE statement directly against replica and the replication stopped to work with the...]]></description>
										<content:encoded><![CDATA[<p>In our scenario we have created transactional replica of our the production database to the staging machine where developers can prepare queries to be later executed in production. But in few days one of developers executed prepared DELETE statement directly against replica and the replication stopped to work with the error: <a href="https://sqlpowered.com/transactional-replication-error-the-row-was-not-found-at-the-subscriber-when-applying-the-replicated-command/">The row was not found at the Subscriber when applying the replicated command</a>. To prevent this situation we have decided to disable all DML operations against this replica for all accounts except of the system one used by replication agent. After searching for various solutions the easiest way (not saying that the best one) for us was to generate triggers where all transactions from other then replication accounts will be rolled back.<span id="more-2129"></span></p>
<p>Below is simple script to generate required triggers for every table in database. You only need to set @ReplAccount variable to your replication system account. If you will later remove all triggers you can switch @DeleteOnly variable from 0 to 1.</p>
<pre class="lang:tsql decode:true">DECLARE @Stmt NVARCHAR(MAX)
DECLARE @DeleteOnly BIT
DECLARE @ReplAccount NVARCHAR(128)

SET @ReplAccount = 'domain\account'
SET @DeleteOnly = 0

SET @Stmt = ''

SELECT 
	@Stmt = @Stmt + 
	CASE @DeleteOnly 
		WHEN 0 THEN 
			N'EXECUTE(''CREATE TRIGGER ['+ [sch].[name] + '].[trg_prevent_modify_' + [tab].[name] + '] 
							ON [' + [sch].[name] + '].[' + [tab].[name] + ']
			FOR INSERT, UPDATE, DELETE
			AS
			BEGIN
				IF SUSER_SNAME() &lt;&gt; ''''' + @ReplAccount + '''''
				BEGIN
					RAISERROR(''''Database is replicated and data cannot be modified!'''',16,1);
					ROLLBACK;
				END
			END'');

			'
		ELSE 'DROP TRIGGER ['+[sch].[name]+'].[trg_prevent_modify_'+[tab].[name]+']' + CHAR(13)
	END
FROM sys.tables AS tab
	JOIN sys.schemas AS sch ON tab.[schema_id] = sch.[schema_id]
WHERE tab.[type] = 'U'

PRINT @Stmt
EXECUTE(@Stmt)
GO</pre>
<p>If you will test this solution the try to update the data in any table and the following exception will be generated:</p>
<p><img fetchpriority="high" decoding="async" class="alignnone size-full wp-image-2219" src="https://sqlpowered.com/wp-content/uploads/2016/10/Trigger_To_Prevent_Replica_To_Be_Updated_Exception.png" alt="trigger_to_prevent_replica_to_be_updated_exception" width="892" height="80" /></p>
]]></content:encoded>
					
					<wfw:commentRss>https://sqlpowered.com/how-to-protect-transactional-replica-against-breaking-data-modifications/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Transactional Replication Error: The row was not found at the Subscriber when applying the replicated command.</title>
		<link>https://sqlpowered.com/transactional-replication-error-the-row-was-not-found-at-the-subscriber-when-applying-the-replicated-command/</link>
					<comments>https://sqlpowered.com/transactional-replication-error-the-row-was-not-found-at-the-subscriber-when-applying-the-replicated-command/#comments</comments>
		
		<dc:creator><![CDATA[Jan Dvořák]]></dc:creator>
		<pubDate>Mon, 18 Jul 2016 17:19:44 +0000</pubDate>
				<category><![CDATA[DBA]]></category>
		<category><![CDATA[replications]]></category>
		<guid isPermaLink="false">https://sqlpowered.com/?p=2122</guid>

					<description><![CDATA[We are using transactional replication to publish production data for the developers that they can test bugs and other stuff on the subscriber database with near-live data. From time to time they do a mistake and modify data on subscribers and the result is that replication is broken. In this...]]></description>
										<content:encoded><![CDATA[<p>We are using transactional replication to publish production data for the developers that they can test bugs and other stuff on the subscriber database with near-live data. From time to time they do a mistake and modify data on subscribers and the result is that replication is broken. In this post, I will show you few simple steps on how to find more details in case your replication is reporting an error that the row was not found at the Subscriber when applying the replicated command.<span id="more-2122"></span></p>
<p>Usually, you will capture this error in Replication Monitor as visible in this picture:</p>
<p><img decoding="async" class="alignnone size-full wp-image-2125" src="https://sqlpowered.com/wp-content/uploads/2016/07/Transactional-Replication-Error.png" alt="Transactional-Replication-Error" width="708" height="577" /></p>
<p>As a first step, we need to browse replication commands that are waiting in the <em>distribution</em> database to be replayed on subscribers. We will select these data from the <a href="https://msdn.microsoft.com/en-us/library/ms188765.aspx">Msrepl_errors</a> table:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="sql">USE [distribution]
GO

SELECT 
	[id], [time], [error_code], [error_text], [xact_seqno]
FROM [dbo].[MSrepl_errors] 
ORDER BY [time] desc
GO</pre>
<p>Once we have selected our error rising command the most important information is hidden in <em>the xact_seqno</em> column: it&#8217;s the starting transaction log sequence number of the first failed transaction in the execution batch.</p>
<p><img decoding="async" class="alignnone size-full wp-image-2124" src="https://sqlpowered.com/wp-content/uploads/2016/07/Transactional-Replication-MSrepl_errors.png" alt="Transactional-Replication-MSrepl_errors" width="865" height="77" /></p>
<p>We will use this sequence number as an input parameter for <a href="https://msdn.microsoft.com/en-us/library/ms176109.aspx">sys.sp_browsereplcmds</a> stored procedure which returns a readable version of the replicated commands stored in the distribution database.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="sql">USE [distribution]
GO

EXEC [sys].[sp_browsereplcmds]
	 @xact_seqno_start = N'0x0000A31F0007BC9E01A200000000',
     @xact_seqno_end = N'0x0000A31F0007BC9E01A200000000'
GO</pre>
<p><img loading="lazy" decoding="async" class="alignnone size-full wp-image-2123" src="https://sqlpowered.com/wp-content/uploads/2016/07/Transactional-Replication-sp_browsereplcmds.png" alt="Transactional-Replication-sp_browsereplcmds" width="656" height="96" /></p>
<p>In the <em>command</em> column is the user-friendly text representation of the command to be replicated. The most important part of it is the number in parentheses which is in our particular case the primary key value of the row that should be deleted by this command. And because this row was deleted on the subscriber manually, a replication error is raised when this command is executed on the subscriber.</p>
<p>If we have the option to insert deleted rows on subscriber manually, replication will continue without an error and there is no need for subscription reinitialization.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://sqlpowered.com/transactional-replication-error-the-row-was-not-found-at-the-subscriber-when-applying-the-replicated-command/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
