Home Microsoft SQL Server DigiMailing iRN Contact
    Keyword



sys.sp_MSUpgradeConflictTable

  No additional text.


Syntax
create procedure sys.sp_MSUpgradeConflictTable
AS

declare @conflict_table sysname
declare @conflict_id    int
declare @objid            int
declare @qual_conflict    nvarchar(517)
declare @rgcol nvarchar(270)
declare @indname nvarchar(258)
declare @owner sysname
declare @constraint_name nvarchar(258)
declare @MSrepl_create_time_didnotexist bit
declare @cmd nvarchar(4000)

if object_id('MSmerge_delete_conflicts') is not NULL and
    not exists (select * from sys.columns where object_id = object_id('MSmerge_delete_conflicts') and name = 'create_time')
begin
    alter table MSmerge_delete_conflicts add create_time datetime not null default getdate()
    if @@error<>0
        return 1
end


-- Get entries from MSmerge_delete_conflicts for all the articles.
if object_id('MSmerge_delete_conflicts') is not NULL and
   object_id('MSmerge_conflicts_info') is not NULL
begin
    exec ('insert into MSmerge_conflicts_info(tablenick,
                                    rowguid,
                                    origin_datasource,
                                    conflict_type,
                                    reason_code,
                                    reason_text,
                                    pubid,
                                    MSrepl_create_time
                                    )
        select tablenick, rowguid, origin_datasource, conflict_type, reason_code, reason_text, pubid, create_time
            from MSmerge_delete_conflicts')
    if @@ERROR<>0 goto ErrorReturn
end

declare AC CURSOR LOCAL FAST_FORWARD for select conflict_table, objid from dbo.sysmergearticles
open AC
fetch AC into @conflict_table, @objid
while (@@fetch_status<>-1)
begin
    if @conflict_table is NOT null
    begin
        set @constraint_name=NULL
        select @owner=schema_name(schema_id) from sys.objects where object_id = @objid
        select @rgcol = QUOTENAME(name) from sys.columns where object_id = @objid and
                    is_rowguidcol = 1

        select @qual_conflict = QUOTENAME(schema_name(schema_id)) + '.' + QUOTENAME(@conflict_table) from sys.objects where name = @conflict_table
        select @conflict_id = object_id(@qual_conflict)

        /*
        ** Upgrade conflict table so that it can get cleaned up later on
        */
        if @conflict_id is not NULL
        and exists (select name from sys.columns where name='reason_code' and object_id=@conflict_id)
        and exists (select name from sys.columns where name='pubid' and object_id=@conflict_id)
        and exists (select name from sys.columns where name='reason_text' and object_id=@conflict_id)
        and exists (select name from sys.columns where name='conflict_type' and object_id=@conflict_id)
        and exists (select name from sys.columns where name='origin_datasource' and object_id=@conflict_id)
        begin
            if not exists (select name from sys.columns where name='MSrepl_create_time' and object_id=@conflict_id)
            begin
                set @MSrepl_create_time_didnotexist = 1
                exec ('alter table ' + @qual_conflict + ' add MSrepl_create_time datetime NULL ')
                if @@ERROR<>0 goto ErrorReturn
                exec (' update ' +     @qual_conflict + ' set MSrepl_create_time = getdate()')
                if @@ERROR<>0 goto ErrorReturn
            end
            else
                set @MSrepl_create_time_didnotexist = 0

            if not exists (select name from sys.columns where name='origin_datasource_id' and object_id=@conflict_id)
            begin
                exec ('alter table ' + @qual_conflict + ' add origin_datasource_id uniqueidentifier not NULL default newid() ')
                if @@ERROR<>0 goto ErrorReturn
            end
            if object_id('MSmerge_conflicts_info') is not NULL
            begin
                exec ('insert into MSmerge_conflicts_info(tablenick,
                                                    rowguid,
                                                    origin_datasource,
                                                    conflict_type,
                                                    reason_code,
                                                    reason_text,
                                                    pubid,
                                                    MSrepl_create_time,
                                                    origin_datasource_id)
                    select 0, ' + @rgcol + ', origin_datasource, conflict_type, reason_code, reason_text, pubid, MSrepl_create_time, origin_datasource_id
                        from ' + @qual_conflict)
                if @@ERROR<>0 goto ErrorReturn
            end


            -- drop index built on conflict table based on rowguid and origin_datasource,
            -- now that origin_datasource is no longer part of the conflict table schema

            select @indname = 'uc_' + @conflict_table
            if exists (select * from sysindexes where id = @conflict_id and name=@indname)
            begin
                set @indname = QUOTENAME(@indname)
                exec('drop index ' + @qual_conflict + '.' + @indname)
                if @@ERROR<>0 goto ErrorReturn
            end

            -- drop default constraints built on column MSrepl_create_time, so that we can drop the
            -- column, now that it is no longer part of new conflict table schema

            select @constraint_name=object_name(constid) from sysconstraints where id=@conflict_id
            while (@constraint_name is not NULL)
            begin
                select @constraint_name = QUOTENAME(@constraint_name)

                exec ('alter table ' + @qual_conflict + ' drop constraint ' + @constraint_name)
                if @@ERROR<>0 goto ErrorReturn

                select @constraint_name = NULL
                select @constraint_name=object_name(constid) from sysconstraints where id=@conflict_id
            end

            select @cmd  = 'alter table ' + @qual_conflict + ' drop column reason_code, reason_text, pubid, conflict_type, origin_datasource'

            /* Shiloh RTM merge databases used to have the name of the conflict create_time column as create_time and not MSrepl_create_time */
            /* If create_time column exists and MSrepl_create_time does not exist, this clearly is the case of Shiloh RTM database - drop that column too */
            if @MSrepl_create_time_didnotexist = 1 and
                exists (select name from sys.columns where name='create_time' and object_id=@conflict_id)
            begin
                select @cmd = @cmd + ', create_time'
            end

            if exists (select name from sys.columns where name='MSrepl_create_time' and object_id=@conflict_id)
            begin
                select @cmd = @cmd + ', MSrepl_create_time'
            end

            exec(@cmd)
            if @@ERROR<>0 goto ErrorReturn

            -- create index on new conflict table on rowguid column and origin_datasource_id column
            -- identified by new conflict table name
            select @indname = 'uc_' + @conflict_table + '_new'
            set @indname = QUOTENAME(@indname)

            if not exists (select * from sys.indexes where object_id = @conflict_id and index_id = 1)
                and exists (select * from sys.columns where object_id = @conflict_id and quotename(name) = @rgcol)
                and exists (select * from sys.columns where object_id = @conflict_id and name = 'origin_datasource_id')
            begin
                exec ('Create unique clustered index ' + @indname + ' on ' + @qual_conflict +
                    ' (' + @rgcol + ', origin_datasource_id)' )
                if @@ERROR <> 0 goto ErrorReturn
            end

ErrorReturn:
            if @@ERROR<>0
            begin
                close AC
                deallocate AC
                return (1)
            end
        end
    end
    fetch next from AC into @conflict_table, @objid
end
close AC
deallocate AC
return (0)

 
Last revision 2008RTM
See also

  sp_vupgrade_mergetables (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