Solved

Export tables with foreign keys

Posted on 2013-01-01
5
838 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:Scott Pletcher
Scott Pletcher 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sql server query 18 37
query linked sql table field from access 4 21
SQL Availablity Groups Shared Path 2 14
Getting invalid Syntax SQL. 3 19
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

861 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