我有两个数据库,一个被调用Natalie_playground
,一个被调用LiveDB
。由于我想练习插入、更新东西,我想将一些表LiveDB
从Natalie_playground
.
我要复制的表称为:
Customers, Computers, Cellphones, Prices
我试图做的是(使用SSMS)右键单击表格,但那里没有副本!
我有两个数据库,一个被调用Natalie_playground
,一个被调用LiveDB
。由于我想练习插入、更新东西,我想将一些表LiveDB
从Natalie_playground
.
我要复制的表称为:
Customers, Computers, Cellphones, Prices
我试图做的是(使用SSMS)右键单击表格,但那里没有副本!
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.
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.
您还可以尝试 SQL Server 导入/导出向导。如果目标表不存在,它们将在您运行向导时创建。
查看 MSDN 了解更多详细信息http://msdn.microsoft.com/en-us/library/ms141209.aspx
我从其他博客中找到了一个简单的方法。希望这可能会有所帮助。
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
尝试这个:
如果目标表存在:
SELECT SourceTableAlias.*
INTO TargetDB.dbo.TargetTable
FROM SourceDB.dbo.SourceTable SourceTableAlias
如果目标表不存在:
INSERT INTO TargetDB.dbo.TargetTable
SELECT SourceTableAlias.*
FROM SourceDB.dbo.SourceTable SourceTableAlias
祝你好运!
尝试这个
USE TargetDatabase
GO
INSERT INTO dbo.TargetTable(field1, field2, field3)
SELECT field1, field2, field3
FROM SourceDatabase.dbo.SourceTable
WHERE (some condition)
如果您只想复制表的架构,您可以在提到的查询的末尾添加一个错误条件。
前任。
SELECT table_A.FIELD_1, ..., table_A.FIELD_N
INTO LiveDB.custom_table
FROM Natalie_playground.dbo.custom_table table_A
WHERE 0 > 1