Demétrio's profileDemétrio Silva - Sql Ser...BlogListsNetworkMore Tools Help

Blog


    July 13

    Alterar identity várias tabelas

    Galera,

    As vezes precisamos gerar scripts para alterar o identity de várias tabelas, por exemplo, ambientes replicados onde identitys do publicante e assinante precisam ser diferentes.

    Abaixo, mostro um script de como fazer isso:

    SET NOCOUNT ON    
    GO
            
    --declaro uma table que será usada para guardar o nome das tabelas que possuem campos identitys
    declare @tabelas table (idx int identity(1,1), tabela varchar(100))             
    insert into @tabelas (tabela)                                    
    select distinct t.name
    from
        sys.tables t
    inner join sys.columns c on c.object_id = t.object_id
    where
        t.is_ms_shipped = 0   
        and c.is_identity = 1
    order by
        name

    declare @inicio int, @fim int             
    declare @command varchar(1000)

    --variável que deve ser setada para o valor do novo identity            
    declare @ident_aux int = 15000
                 
    select @inicio = 1, @fim = max(idx) from @tabelas             

    --faço o while ao invés de um cursor e monto o comando
    while @inicio <= @fim             
    begin       
                                                                     
        select
            @command = 'dbcc checkident ( ' + tabela + ', ' + 'reseed' + ', ' + cast( @ident_aux as varchar(10) ) + ' ) '
        from
            @tabelas
        where
            idx = @inicio             
       
        --imprimo o comando para que o mesmo possa ser executado posteriormente
        print( @command )             
        print('GO')
       
        set @inicio = @inicio + 1 
                   
    end           

    Simples não? Mas é muito útil para alteração de identitys em várias tabelas.

    Abraços

             

    Comments

    Please wait...
    Sorry, the comment you entered is too long. Please shorten it.
    You didn't enter anything. Please try again.
    Sorry, we can't add your comment right now. Please try again later.
    To add a comment, you need permission from your parent. Ask for permission
    Your parent has turned off comments.
    Sorry, we can't delete your comment right now. Please try again later.
    You've exceeded the maximum number of comments that can be left in one day. Please try again in 24 hours.
    Your account has had the ability to leave comments disabled because our systems indicate that you may be spamming other users. If you believe that your account has been disabled in error please contact Windows Live support.
    Complete the security check below to finish leaving your comment.
    The characters you type in the security check must match the characters in the picture or audio.

    To add a comment, sign in with your Windows Live ID (if you use Hotmail, Messenger, or Xbox LIVE, you have a Windows Live ID). Sign in


    Don't have a Windows Live ID? Sign up

    Trackbacks

    The trackback URL for this entry is:
    http://demetriosqlserver.spaces.live.com/blog/cns!1D50CF1AC3A02CDE!264.trak
    Weblogs that reference this entry
    • None