none
Copy Table With PK / FK Between Databases

    Question

  • I need to copy a table from DatabaseA over to DatabaseB.  This is easy enough, but I want to keep the PK / FK (etc) from DatabaseA.  How can I do this?  I basically want to copy EVERYTHING about the table in DatabaseA over to DatabaseB.
    Friday, June 12, 2009 7:23 PM

Answers

  • Script out the constraints at source (Object Explorer right click on table).

    Create the table at destination with SELECT INTO TABLE CREATE.

    Then you can apply the constraints.

    Let us know if works.


    Kalman Toth, SQL Server 2008 Training, SSAS, SSIS, SSRS, BI: www.SQLUSA.com
    • Marked as answer by Jay Mazz Friday, June 12, 2009 7:54 PM
    Friday, June 12, 2009 7:49 PM

All replies

  • Script out the constraints at source (Object Explorer right click on table).

    Create the table at destination with SELECT INTO TABLE CREATE.

    Then you can apply the constraints.

    Let us know if works.


    Kalman Toth, SQL Server 2008 Training, SSAS, SSIS, SSRS, BI: www.SQLUSA.com
    • Marked as answer by Jay Mazz Friday, June 12, 2009 7:54 PM
    Friday, June 12, 2009 7:49 PM
  • It worked, thank you!
    Friday, June 12, 2009 7:54 PM
  • SQL 2005 is kind enough to not do this for you.  

    On your source DB you should right click the required tables, script them out and define them on your target server.  

    Then use SSMS/SSIS to import directly into them.  

    If required make sure you set the identity_insert option on.
    Friday, June 12, 2009 7:59 PM