Home Microsoft SQL Server DigiMailing iRN Contact
    Keyword



sys.sp_MSmakegenerationidentity

  No additional text.


Syntax
create procedure sys.sp_MSmakegenerationidentity as
begin
    declare @retcode int
    declare @old_genhistory_objid binary(16)
    declare @new_genhistory_objid binary(16)
    declare @database_principal sysname
    declare @permission sysname
	declare @cmd nvarchar(1000)

    if object_id('MSmerge_genhistory2') is not NULL
    begin
        drop table dbo.MSmerge_genhistory2
        if @@error <> 0
            return 1
    end

    -- Since an existing bigint column cannot be altered to be a bigint identity,
    -- we have to go through the hoops of creating another table with generation
    -- as identity, inserting all genhistory rows into this new table, then dropping
    -- the old table, and renaming the new table with the old name.
    create table dbo.MSmerge_genhistory2
    (
        guidsrc         uniqueidentifier     NOT NULL,
        pubid           uniqueidentifier     NULL,
        generation      bigint               identity(1,1) NOT NULL,
        art_nick        int                  NULL,
        -- when the size of nicknames is changed: adjust constants in CMergeDatasource::EnumerateGenerationRange.
        -- it is 1000 bytes in Shiloh, 1001 in Yukon: difference allows to determine whether column upgrade took place.
        nicknames       varbinary(1001)      NOT NULL,
        coldate         datetime             NOT NULL,
        genstatus       tinyint              NOT NULL default 0, -- 0 = open,
                                                                 -- 1 = closed and generated locally,
                                                                 -- 2 = closed and came from elsewhere,
                                                                 -- 3 = temporarily closed used only by makgeneration
                                                                 -- 4 = generation came from elsewhere and was inserted by merge. is open. possible interrupted generation
        changecount     int                  NOT NULL default 0,
        subscriber_number  int not NULL default 0
    )

    if @@error <> 0
        return 1

    set identity_insert dbo.MSmerge_genhistory2 on

    insert into dbo.MSmerge_genhistory2 (guidsrc, pubid, generation, art_nick, nicknames,
        coldate, genstatus, changecount)
        select guidsrc, pubid, generation, art_nick, nicknames, coldate, genstatus, changecount
            from dbo.MSmerge_genhistory

    if @@error <> 0
        return 1

    --set identity_insert dbo.MSmerge_genhistory off

    create clustered index c1MSmerge_genhistory on MSmerge_genhistory2(generation)
    if @@ERROR <> 0
        return 1

    -- Only create the index index if guidsrc values are unique. Before SP1 it was possible
    -- for this table to contain dup guidsrc. This happened when the genhistory table was empty to start
    -- and we insert an initial row (for no article) and then try to insert a row for the first
    -- article without generating a new guid.
    if exists ( select guidsrc, pubid
        from dbo.MSmerge_genhistory2
        group by guidsrc, pubid
        having count(*) > 1 )
        raiserror (21203, 10, 5, 'MSmerge_genhistory')
    else
    begin
        create unique index unc1MSmerge_genhistory on MSmerge_genhistory2(guidsrc, pubid)
        if @@ERROR <> 0
            return 1
    end

    create  index nc2MSmerge_genhistory on MSmerge_genhistory2(genstatus, art_nick)
    if @@ERROR <> 0
        return 1

    CREATE INDEX nc4MSmerge_genhistory ON MSmerge_genhistory2(coldate)
    if @@ERROR <> 0
        return 1

    begin tran
    save tran renaming_genhistory_table

    select @old_genhistory_objid = object_id('dbo.MSmerge_genhistory')

    -- before dropping this table get all the permissions for it and grant those on the new table
    declare genhistory_permissions cursor LOCAL FAST_FORWARD for
        select perm.permission_name, prin.name from sys.database_permissions perm, sys.database_principals prin
            where perm.major_id = @old_genhistory_objid and perm.grantee_principal_id = prin.principal_id
    open genhistory_permissions
    fetch genhistory_permissions into @permission, @database_principal
    while (@@fetch_status<>-1)
    begin
		select @cmd = 'grant ' + @permission + ' on dbo.MSmerge_genhistory2 to ' + quotename(@database_principal)
        exec(@cmd)
        if @@error<>0
            goto UNDO_cursor
        fetch next from genhistory_permissions into @permission, @database_principal
    end
    close genhistory_permissions
    deallocate genhistory_permissions

    drop table dbo.MSmerge_genhistory
    if @@error <> 0
        goto UNDO
    exec @retcode = sys.sp_rename 'MSmerge_genhistory2', 'MSmerge_genhistory'
    if @@error <> 0 or @retcode <> 0
        goto UNDO

    exec dbo.sp_MS_marksystemobject MSmerge_genhistory
    if @@ERROR <> 0
        goto UNDO

    -- we need to alter sysmergeschemachange to contain the artid (objid) corresponding to the
    -- new genhistory table
    select @new_genhistory_objid = object_id('dbo.MSmerge_genhistory')

    update dbo.sysmergeschemachange
        set artid = convert(uniqueidentifier, @new_genhistory_objid)
        where artid = convert(uniqueidentifier, @old_genhistory_objid)

    commit tran
    return 0


UNDO_cursor:
    close genhistory_permissions
    deallocate genhistory_permissions

UNDO:
    if @@trancount > 0
    begin
        rollback tran renaming_genhistory_table
        commit tran
    end

    return 1
end

 
Last revision 2008RTM
See also

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