?
Solved

Export tables with foreign keys

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

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

Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

Question has a verified solution.

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

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.
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Suggested Courses

765 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