Visualizzazione dei risultati da 1 a 5 su 5
  1. #1

    [SQL Server] generazione automatica degli script

    Ho un piccolo problema con Sql server, in pratica devo effettuare una grossa operazione di massa e sostituire tutti i constraint che ho su un db su altri 3 DB.

    Con l'enterprise manager ho creato automaticamente tutti gli script per aggiungere i constraint ma non riesco a creare gli script che mi droppano i constraint dei 3 db.
    O detta meglio gli unici script che mi vengono generati per eseguire il drop dei constraint sono quelli per le foreign key.

    Avete una soluzione da propormi per generare automaticamente tutti gli script di drop sulla primary key che non siano foreing key

    Grazie a tutti

    Ciao
    TonyTalk
    Gioca a The Empire Multiplayer online game...ispirato a Risiko ma con scenari ed opzioni rivoluzionarie
    http://www.multiplayergames.it
    http://utenti.lycos.it/the_empire

  2. #2
    potresti essere un po piu chiaro...

    i db sono tutti uguali?
    byz goldfix

  3. #3
    Si i db sono identici ma hanno nomi ai constraint differenti ed io li devo mettere tutti uguali
    TonyTalk
    Gioca a The Empire Multiplayer online game...ispirato a Risiko ma con scenari ed opzioni rivoluzionarie
    http://www.multiplayergames.it
    http://utenti.lycos.it/the_empire

  4. #4
    Originariamente inviato da tonytalk
    Si i db sono identici ma hanno nomi ai constraint differenti ed io li devo mettere tutti uguali
    allora...
    la soluzione c'e' ma devi scriverti un programmino che faccia questo lavoro.

    mi sa che devi per forza di cose interrogare lo schema del DB. Per farlo leggi qui:

    http://msdn.microsoft.com/library/de...ia-iz_4pbn.asp

    le viste che trovi nel suddetto articolo ti permettono di sapere quali sono le constraint e le chiavi del DB che vuoi modificare.
    ciclaci sopra le cancelli e le ricrei.

    Diciamo che questa e la strada piu difficle ma che ti permette di ravanare parecchio dentro SQL.

    In alternativa qui: http://www.red-gate.com/SQL_Compare.htm
    trovi un tool che fa al caso tuo...
    byz goldfix

  5. #5
    Per quetsioni di tempo ho creato io una mia soluzione, la posto qui per il futuro.

    Non ho potuto visitare i link che mi hai proposto ma ci farò un giro al più presto.

    Grazie

    ciao


    codice:
    declare @constname      sysname,
            @cmd            varchar(1024)
    
    --------------------------
    declare @result           nvarchar(4000)
    set @result=''
    
    declare @allTable sysname
    declare curs_tableName cursor for
    select  name 
            from    sysobjects
            where   xtype in ('U')
    	order by name
    
    open curs_tableName
    fetch next from curs_tableName into @allTable
    while (@@fetch_status = 0)
    begin
       --set @result=@result+'-'+@allTable
    --------------------------
    
    
    
    declare curs_constraints cursor for
            select  name
            from    sysobjects      --F=foreign pk=primary
            where   xtype in ('C', 'F', 'UQ', 'D')
            and     (status & 64) = 0
            and     parent_obj = object_id('gat_i.gat_i.'+@allTable)--@tablename)
                                            
    open curs_constraints
    
    
    fetch next from curs_constraints into @constname
    while (@@fetch_status = 0)
    begin
            exec('alter table gat_i.gat_i.'+@allTable+' nocheck constraint all')
            --select @cmd = 'ALTER TABLE ' + @tablename + ' DROP CONSTRAINT ' + @constname
            select @cmd = 'ALTER TABLE gat_i.gat_i.'+ @allTable + ' DROP CONSTRAINT ' + @constname
            set @result=@result+ '^P'+@cmd
            exec(@cmd)
    	exec('alter table gat_i.gat_i.'+@allTable+' check constraint all')
    
            fetch next from curs_constraints into @constname
    end
    
    close curs_constraints
    deallocate curs_constraints
    
    
    ---------
            fetch next from curs_tableName into @allTable 
    end
    close curs_tableName
    deallocate curs_tableName
    ----
    
    select 'Tutti i constraint eliminati tranne le primary key'
    
    
    
    
    
    
    
    
    -----------------eliminiamo le primary key
    declare curs_tableName cursor for
    select  name
            from    sysobjects
            where   xtype in ('U')
    	order by name
    
    open curs_tableName
    fetch next from curs_tableName into @allTable
    while (@@fetch_status = 0)
    begin
       --set @result=@result+'-'+@allTable
    --------------------------
    
    
    
    declare curs_constraints cursor for
            select  name
            from    sysobjects      --F=foreign pk=primary
            where   xtype in ('C', 'F', 'PK', 'UQ', 'D')
            and     (status & 64) = 0
            and     parent_obj = object_id('gat_i.gat_i.'+@allTable)--@tablename)
                                            
    open curs_constraints
    
    
    fetch next from curs_constraints into @constname
    while (@@fetch_status = 0)
    begin
            exec('alter table gat_i.gat_i.'+@allTable+' nocheck constraint all')
            --select @cmd = 'ALTER TABLE ' + @tablename + ' DROP CONSTRAINT ' + @constname
            select @cmd = 'ALTER TABLE gat_i.gat_i.'+@allTable + ' DROP CONSTRAINT ' + @constname
            set @result=@result+ '^P'+@cmd
            exec(@cmd)
    	exec('alter table gat_i.gat_i.'+@allTable+' check constraint all')
    
            fetch next from curs_constraints into @constname
    end
    
    close curs_constraints
    deallocate curs_constraints
    
    
    ----
            fetch next from curs_tableName into @allTable 
    end
    close curs_tableName
    deallocate curs_tableName
    ----
    
    
    select 'primary key eliminate'
    TonyTalk
    Gioca a The Empire Multiplayer online game...ispirato a Risiko ma con scenari ed opzioni rivoluzionarie
    http://www.multiplayergames.it
    http://utenti.lycos.it/the_empire

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.