SQL Server Copying tables from one database to another

前端 未结 7 1168
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-20 16:56

I have two databases, one is called Natalie_playground and one is called LiveDB. Since I want to practice insert, update things, I want to copy som

相关标签:
7条回答
  • 2020-12-20 17:31

    If you want to copy only the schema of tables, you can add a false condition to the end of mentioned queries.

    ex.

    SELECT table_A.FIELD_1, ..., table_A.FIELD_N 
    INTO LiveDB.custom_table
    FROM  Natalie_playground.dbo.custom_table table_A
    WHERE 0 > 1
    
    0 讨论(0)
  • 2020-12-20 17:32

    I found an easy way from other blog. Hope this might be helpful.

    Select * into DestinationDB.dbo.tableName from SourceDB.dbo.SourceTable  
    

    http://www.codeproject.com/Tips/664327/Copy-Table-Schema-and-Data-From-One-Database-to-An

    0 讨论(0)
  • 2020-12-20 17:42

    Right click on your database -> under Tasks choose Generate scripts, follow the wizard, choose your tables and check the check box that says 'script table data' (or similar) generate it to an SQL script and execute it on your other DB.

    0 讨论(0)
  • 2020-12-20 17:42

    Try this:

    If target table is exists :

    SELECT SourceTableAlias.*
    INTO TargetDB.dbo.TargetTable
    FROM  SourceDB.dbo.SourceTable SourceTableAlias
    

    And if target table is not exists :

     INSERT INTO TargetDB.dbo.TargetTable 
     SELECT SourceTableAlias.*
     FROM SourceDB.dbo.SourceTable SourceTableAlias
    

    Good Luck!

    0 讨论(0)
  • 2020-12-20 17:45

    try this

    USE TargetDatabase
    GO
    
    INSERT INTO dbo.TargetTable(field1, field2, field3)
    SELECT field1, field2, field3
    FROM SourceDatabase.dbo.SourceTable
    WHERE (some condition)
    
    0 讨论(0)
  • 2020-12-20 17:51

    You can also try SQL Server Import/Export wizard. If target tables do not exist already they will be created when you run the wizard.

    Check out MSDN for more details http://msdn.microsoft.com/en-us/library/ms141209.aspx

    0 讨论(0)
提交回复
热议问题