Solved

Export tables with foreign keys

Posted on 2013-01-01
5
820 Views
Last Modified: 2013-01-02
I've got a database with 61 tables that have varying foreign keys. I'm trying to export it from SQL Server 2008 to SQL Server 2005 and keep running into problems because the export isn't smart enough to know what order the tables need to go in to not violate foreign keys. I've been trying to do a couple tables at a time, but don't know enough about things to know what order I should copy the tables in. What's the easiest way to figure out the order?
0
Comment
Question by:quizwedge
5 Comments
 
LVL 22

Accepted Solution

by:
Steve Wales earned 300 total points
ID: 38735103
One way would be to create your destination tables without the foreign key constraints, then load the data, then add the constraints after the data load is finished.

Done this myself in the past and had it work like a charm.
0
 
LVL 42

Assisted Solution

by:EugeneZ
EugeneZ earned 150 total points
ID: 38735386
-script all tables for migration without FK
-script all FK
Disable, enable, drop and recreate SQL Server Foreign Keys
http://www.mssqltips.com/sqlservertip/1376/disable-enable-drop-and-recreate-sql-server-foreign-keys/

-create the tables in the destination DB
- copy data into the tables
-- add FK from step#2
0
 
LVL 69

Assisted Solution

by:ScottPletcher
ScottPletcher earned 50 total points
ID: 38737255
The query below will list FK "depths", where a depth of 0 = no fk; 1 = 1st level fk; 2 = 2nd level fk; etc..

Load from the lowest to the highest: that is, all level 0 first, then level 1, then level 2, etc..



;WITH FKs AS (
    SELECT DISTINCT
        OnTable.name AS OnTable,
        AgainstTable.name AS AgainstTable
    FROM sys.foreign_keys fk
    INNER JOIN sys.objects onTable ON
        fk.parent_object_id = onTable.object_id
    INNER JOIN sys.objects againstTable ON
        fk.referenced_object_id = againstTable.object_id
    WHERE
        AgainstTable.TYPE = 'U' AND
        OnTable.TYPE = 'U' AND
        OnTable.Name <> AgainstTable.Name
),
UserTables AS (
    SELECT
        o.name AS OnTable,
        FKs.againstTable AS AgainstTable
    FROM sys.objects o
    LEFT OUTER JOIN FKs ON
        o.name = FKs.onTable
    WHERE
        o.type = 'U' AND
        o.name not like 'sys%' AND
        o.name not like 'ms%'
),
FKRecursion as (
    -- base case
    SELECT
         ut.OnTable AS TableName,
         0 AS Depth
    FROM UserTables ut
    WHERE
        AgainstTable IS NULL
    -- recursive case
    UNION ALL
    SELECT
        ut.OnTable AS TableName,
        r.Depth + 1 AS Depth
    FROM UserTables ut
    INNER JOIN FKRecursion r ON
        ut.AgainstTable = r.TableName
)
SELECT
    MAX(depth) AS FK_Depth,
    TableName
FROM
    FKRecursion
GROUP BY
    TableName
ORDER BY
    FK_Depth,
    TableName
0
 
LVL 14

Author Comment

by:quizwedge
ID: 38737884
@ScottPletcher, looks like a good function, but unfortunately, I'm getting the following error:

The statement terminated. The maximum recursion 100 has been exhausted before statement completion.

I ended up using the suggestions from @sjwales and @EugeneZ

Thanks
0
 
LVL 14

Author Closing Comment

by:quizwedge
ID: 38737901
I saw the suggestion from sjwales and ended up implementing one of EugeneZ's before I saw the post. I think ScottPletcher's solution would work if I gave him the opportunity to update the code, but wanted to close the question since I solved the problem. Thanks for all of the help.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now