Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8268859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:02:03+00:00 2026-06-08T06:02:03+00:00

I am running an SSIS package which will replace data for a few tables

  • 0

I am running an SSIS package which will replace data for a few tables from FlatFiles to existing tables in a database.

My package will truncate the tables and then insert the new data. When I run my SSIS package, I get an exception because of the foreign keys.

Can I disable the constraints, run my import, then re-enable them?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T06:02:05+00:00Added an answer on June 8, 2026 at 6:02 am

    To disable foreign key constraints:

    DECLARE @sql nvarchar(max) = N'';
    
    ;WITH x AS 
    (
      SELECT DISTINCT obj = 
          QUOTENAME(OBJECT_SCHEMA_NAME(parent_object_id)) + '.' 
        + QUOTENAME(OBJECT_NAME(parent_object_id)) 
      FROM sys.foreign_keys
    )
    SELECT @sql += N'ALTER TABLE ' + obj + N' NOCHECK CONSTRAINT ALL;
    ' FROM x;
    
    EXEC sys.sp_executesql @sql;
    

    To re-enable:

    DECLARE @sql nvarchar(max) = N'';
    
    ;WITH x AS 
    (
      SELECT DISTINCT obj = 
          QUOTENAME(OBJECT_SCHEMA_NAME(parent_object_id)) + '.' 
        + QUOTENAME(OBJECT_NAME(parent_object_id)) 
      FROM sys.foreign_keys
    )
    SELECT @sql += N'ALTER TABLE ' + obj + N' WITH CHECK CHECK CONSTRAINT ALL;
    ' FROM x;
    
    EXEC sys.sp_executesql @sql;
    

    However, you will not be able to truncate the tables, you will have to delete from them in the right order. If you need to truncate them, you need to drop the constraints entirely, and re-create them. This is simple to do if your foreign key constraints are all simple, single-column constraints, but definitely more complex if there are multiple columns involved.

    Here is something you can try. In order to make this a part of your SSIS package you’ll need a place to store the FK definitions while the SSIS package runs (you won’t be able to do this all in one script). So in some utility database, create a table:

    CREATE TABLE dbo.PostCommand(cmd nvarchar(max));
    

    Then in your database, you can have a stored procedure that does this:

    DELETE other_database.dbo.PostCommand;
    
    DECLARE @sql nvarchar(max) = N'';
    
    SELECT @sql += N'ALTER TABLE ' 
       + QUOTENAME(OBJECT_SCHEMA_NAME(fk.parent_object_id))
       + '.' + QUOTENAME(OBJECT_NAME(fk.parent_object_id)) 
       + ' ADD CONSTRAINT ' + fk.name + ' FOREIGN KEY (' 
       + STUFF((SELECT ',' + c.name
        FROM sys.columns AS c 
            INNER JOIN sys.foreign_key_columns AS fkc 
            ON fkc.parent_column_id = c.column_id
            AND fkc.parent_object_id = c.[object_id]
        WHERE fkc.constraint_object_id = fk.[object_id]
        ORDER BY fkc.constraint_column_id 
        FOR XML PATH(''), 
        TYPE).value(N'./text()[1]', 'nvarchar(max)'), 1, 1, N'')
    + ') REFERENCES ' + 
    QUOTENAME(OBJECT_SCHEMA_NAME(fk.referenced_object_id))
    + '.' + QUOTENAME(OBJECT_NAME(fk.referenced_object_id))
    + '(' + 
    STUFF((SELECT ',' + c.name
        FROM sys.columns AS c 
            INNER JOIN sys.foreign_key_columns AS fkc 
            ON fkc.referenced_column_id = c.column_id
            AND fkc.referenced_object_id = c.[object_id]
        WHERE fkc.constraint_object_id = fk.[object_id]
        ORDER BY fkc.constraint_column_id 
        FOR XML PATH(''), 
        TYPE).value(N'./text()[1]', N'nvarchar(max)'), 1, 1, N'') + ');
    ' FROM sys.foreign_keys AS fk
    WHERE OBJECTPROPERTY(parent_object_id, 'IsMsShipped') = 0;
    
    INSERT other_database.dbo.PostCommand(cmd) SELECT @sql;
    
    IF @@ROWCOUNT = 1
    BEGIN
      SET @sql = N'';
    
      SELECT @sql += N'ALTER TABLE ' + QUOTENAME(OBJECT_SCHEMA_NAME(fk.parent_object_id))
        + '.' + QUOTENAME(OBJECT_NAME(fk.parent_object_id)) 
        + ' DROP CONSTRAINT ' + fk.name + ';
      ' FROM sys.foreign_keys AS fk;
    
      EXEC sys.sp_executesql @sql;
    END
    

    Now when your SSIS package is finished, it should call a different stored procedure, which does:

    DECLARE @sql nvarchar(max);
    
    SELECT @sql = cmd FROM other_database.dbo.PostCommand;
    
    EXEC sys.sp_executesql @sql;
    

    If you’re doing all of this just for the sake of being able to truncate instead of delete, I suggest just taking the hit and running a delete. Maybe use bulk-logged recovery model to minimize the impact of the log. In general I don’t see how this solution will be all that much faster than just using a delete in the right order.

    In 2014 I published a more elaborate post about this here:

    • Drop and Re-Create All Foreign Key Constraints in SQL Server
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an SSIS package that takes data from Tables in an SQL database
I have got a SSIS Package running every 2 hours. It collects data from
I'm running an SSIS package from an external scheduler (Control-M), using dtexec. I'd like
I am running a SSIS package to load say a million rows from a
I'm running an SSIS package that I made a few months ago, and ran
I have a very basic SSIS package with one data flow task (from an
I have written an SSIS package in C# to get the data from a
I have a SSIS package which reads an Excel File (Data Flow Source) and
Is it possible to stop SSIS package from running with a failure error if
I have a job on SQL Server 2005 running a ssis package. The package

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.