Home Microsoft SQL Server DigiMailing iRN Contact
    Keyword



sys.sp_MSscriptinsertconflictfinder

  No additional text.


Syntax
create procedure sys.sp_MSscriptinsertconflictfinder
(
	@publication sysname		-- publication name
	,@article sysname			-- article name
	,@objid int				-- object id
)
as
begin
	declare
			@rc int
			,@cmd nvarchar(4000)
			,@artid int
			,@pubid int
			,@qualname nvarchar(517)
			,@fhasnonpkuniquekeys int

	
	-- initialize the vars we will use
	
	select @pubid = pubid
	from syspublications
	where name = @publication
	select @artid = artid
	from sysarticles
	where name = @article
		and pubid = @pubid
	exec sp_MSget_qualified_name @objid, @qualname OUTPUT
	
	--  check if this article has non PK unique keys
	
	exec @fhasnonpkuniquekeys = sp_repltablehasnonpkuniquekey @tabid = @objid
	
	-- start scripting
	
	select @cmd = N'
	
	'+N'
	'+N'-- 
	'+N'-- This is the crux of the proc for conflict resolution
	'+N'-- This code block is essentially a state machine
	'+N'-- where we ascertain the state of resolution
	'+N'-- The actions of this resolution varies for the policy
	'+N'-- The comments for each state outline the policy
	'+N'-- specific actions
	'+N'-- 
	'+N'--'
	insert into #proctext(procedure_text) values( @cmd )
	
	-- continue scripting
	
	select @cmd = N'
	if (@execution_mode in (@QPubWins, @QSubWins))
	begin
		'+N'
		'+N'-- initialize the conflict case
		'+N'
		select @cftcase = 0

		if (@rowcount = 0)
		begin
			'+N'
			'+N'-- we had conflict for this command
			'+N'
			if (@error in (547, 2601, 2627))
			begin'
	insert into #proctext(procedure_text) values( @cmd )
	select @cmd = N'
				'+N'
				'+N'-- Conflict due to unique key/constraint
				'+N'--'
	insert into #proctext(procedure_text) values( @cmd )
	
	-- script row check with PK
	
	select @cmd = N'
				if exists (select * from ' + @qualname
	insert into #proctext(procedure_text) values( @cmd )
	exec sp_MSscript_where_clause @objid, @artid, 'new_pk_q', NULL, 0
	insert into #proctext(procedure_text) values( N' )')
	
	-- continue scripting
	
	select @cmd = N'
				begin
					'+N'
					'+N'-- case 21: A row with same PK already exists
					'+N'-- PubWins 
					'+N'-- generate delete compensating action for row with PK
					'+N'-- generate delete + insert compensating action with values for all unique keys
					'+N'-- SubWins 
					'+N'-- delete rows with values of all keys
					'+N'-- insert row with values
					'+N'
					select @cftcase = 21
				end'
	insert into #proctext(procedure_text) values( @cmd )
	
	-- script this block if the article has non PK unique keys
	
	if (@fhasnonpkuniquekeys = 1)
	begin
		select @cmd = N'
				else
				begin
					'+N'
					'+N'-- case 22: A row with same nonPK key(s) already exists
					'+N'-- PubWins 
					'+N'-- generate delete compensating action for row with PK
					'+N'-- generate delete + insert compensating action with values for non PK keys
					'+N'-- SubWins 
					'+N'-- delete rows with values of non PK keys
					'+N'-- insert row with values
					'+N'
					select @cftcase = 22
				end'
		insert into #proctext(procedure_text) values( @cmd )
	end
	
	-- continue scripting
	
	select @cmd = N'
			end
		end
		else if (@execution_mode = @QPubWins)
		begin
			'+N'
			'+N'-- we had no conflict for this command
			'+N'-- We need to process this block only in the Publisher Wins cases
			'+N'

			'+N'
			'+N'-- case 23: No conflict
			'+N'-- PubWins 
			'+N'-- generate delete compensating action with PK
			'+N'
			select @cftcase = 23
		end	
	end'
	insert into #proctext(procedure_text) values( @cmd )
	
	-- continue scripting
	
	select @cmd = N'

	'+N'
	'+N'-- 
	'+N'-- Now the generation phase
	'+N'-- Use the conflict case value to decide what to do
	'+N'-- 
	'+N'--'
	insert into #proctext(procedure_text) values( @cmd )
	
	-- all done
	
	return 0
end

 
Last revision 2008RTM
See also

  sp_MSdrop_repltran (Procedure)
sp_MSscript_sync_ins_proc (Procedure)
       



News

  Query a named instance
Sybase+ASE+silent+install
SQL 2008 R2
Deprecated procedures in SQL2008
Reporting Services item-level role definitions
Create all your missing indexes
Converting datetime field
Start MSSQL Server Profiler at time
Replicating MSSQL Server views
Exploring Microsoft Sharepoint
The OLE DB provider "SQLNCLI10" for linked server indicates.
Mobile solar charger
Oracle to SQL Server replication
Cannot insert the value NULL into column
Undocumented Microsoft SQL Server 2008
VMware
Zoekmachine optimalisatie
SQL Servers hidden objects
FckEditor
Flash in Ajax
De hype die AJAX heet
Barcode scannen
MySQL; Gratis, makkelijk en snel!
PHP
sIFR; de combinatie tussen HTML en Flash