DTS and FK Constraints - multiple tables

  • Hi,

    Is there an easy/quick way to disable Foreign Key Constraints when copying & populating data from approx. 100 tables from a MSSQL 2000 database to another MSSQL 2000 server/database?

    THanks. Jeff

    Many thanks. Jeff

  • Sorry for the bad news but AFAIK

    script them out, drop ,COPY DATA, recreate!

     

     


    * Noel

  • I have put a Run SQL task that first drops an index, then a second Run SQL task that creates that index.  The first task runs before the transformation and the second after.

    You could do the same with the foreign key constraints.

    Russ

    Russel Loski, MCSE Business Intelligence, Data Platform

  • In my DTS I'm using the following, which seems to work well:

    ALTER TABLE <tablename> NOCHECK CONSTRAINT <fk name>

    Run delete/import script

    ALTER TABLE <tablename> CHECK CONSTRAINT <fk name>

  • We used the following to disable all constraints and triggers when importing data into existing SQL tables. Hope this helps. Richard

    -Disable CONSTRAINST & TRIGGERS

    DECLARE @TempSQL NVARCHAR(200)

      SET @TempSQL = 'sp_msforeachtable "Alter Table ? NOCHECK CONSTRAINT ALL"'

      EXEC sp_executesql @TempSQL

      SET @TempSQL = 'sp_msforeachtable "Alter Table ? DISABLE TRIGGER ALL"'

      EXEC sp_executesql @TempSQL

    --Enable CONSTRAINST & TRIGGERS

    DECLARE @TempSQL2 NVARCHAR(200)

      SET @TempSQL2 = 'sp_msforeachtable @Command1 = "Print ''?''", @command2 = "Alter Table ? CHECK CONSTRAINT ALL"'

      EXEC sp_executesql @TempSQL2

      SET @TempSQL2 = 'sp_msforeachtable @Command1 = "Print ''?''", @command2 = "Alter Table ? ENABLE TRIGGER ALL"'

      EXEC sp_executesql @TempSQL2

  • HI,

    Thanks to everyone's responses. One question .. the "?" .. is this a placeholder that will insert all the tables in the follwing:

    SET @TempSQL = 'sp_msforeachtable "Alter Table ? NOCHECK CONSTRAINT ALL"'

    Also, will this identify all tables or just user defined table objects?

    THanks. Jeff

    Many thanks. Jeff

  • Yes, this will identify all tables in db and will disable/enable all constraints and any triggers. Richard

  • newbie ... many thanks!!! Works like a charm... Jeff

    Many thanks. Jeff

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply