Solved

Export tables with foreign keys

Posted on 2013-01-01
5
839 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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:Eugene Z
Eugene Z 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

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.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to shrink a transaction log file down to a reasonable size.

756 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