?
Solved

Export tables with foreign keys

Posted on 2013-01-01
5
Medium Priority
?
859 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 23

Accepted Solution

by:
Steve Wales earned 1200 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 43

Assisted Solution

by:Eugene Z
Eugene Z earned 600 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 70

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 200 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Suggested Courses

850 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