9

我有两个数据库,一个被调用Natalie_playground,一个被调用LiveDB。由于我想练习插入、更新东西,我想将一些表LiveDBNatalie_playground.

我要复制的表称为: Customers, Computers, Cellphones, Prices

我试图做的是(使用SSMS)右键单击表格,但那里没有副本!

4

7 回答 7

11

Assuming that you have two databases, for example A and B:

  • If target table not exists, the following script will create (I do not recommend this way):

    SELECT table_A.FIELD_1, table_A.FIELD_2,......, table_A.FIELD_N 
    INTO COPY_TABLE_HERE 
    FROM  A.dbo.table_from_A table_A
    
  • If target table exists, then:

     INSERT INTO TABLE_TARGET 
     SELECT table_A.FIELD_1, table_A.FIELD_2,......, table_A.FIELD_N 
     FROM  A.dbo.table_from_A table_A
    

Note: if you want learn and practice this, you can use previous scripts, but if you want copy the complete structure and data from database to another, you should use, "Backup and restore Database" or, "Generate Script Database with data" and run this into another database.

于 2013-07-11T00:27:06.967 回答
8

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.

于 2013-07-11T00:28:37.830 回答
4

您还可以尝试 SQL Server 导入/导出向导。如果目标表不存在,它们将在您运行向导时创建。

查看 MSDN 了解更多详细信息http://msdn.microsoft.com/en-us/library/ms141209.aspx

于 2013-07-11T09:24:11.960 回答
1

我从其他博客中找到了一个简单的方法。希望这可能会有所帮助。

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

于 2015-04-02T20:33:03.387 回答
1

尝试这个:

如果目标表存在:

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

如果目标表不存在:

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

祝你好运!

于 2017-04-09T09:44:09.840 回答
0

尝试这个

USE TargetDatabase
GO

INSERT INTO dbo.TargetTable(field1, field2, field3)
SELECT field1, field2, field3
FROM SourceDatabase.dbo.SourceTable
WHERE (some condition)
于 2014-06-10T07:23:15.893 回答
0

如果您只想复制表的架构,您可以在提到的查询的末尾添加一个错误条件。

前任。

SELECT table_A.FIELD_1, ..., table_A.FIELD_N 
INTO LiveDB.custom_table
FROM  Natalie_playground.dbo.custom_table table_A
WHERE 0 > 1
于 2016-05-23T04:31:15.793 回答