将表从一个数据库复制到 SQL Server 中的另一个数据库

发布于 2024-07-07 17:49:22 字数 116 浏览 10 评论 0原文

我有一个名为 foo 的数据库和一个名为 bar 的数据库。 我在 foo 中有一个名为 tblFoobar 的表,我想将其(数据和全部)从数据库 foo 移动到数据库 bar。 执行此操作的 SQL 语句是什么?

I have a database called foo and a database called bar. I have a table in foo called tblFoobar that I want to move (data and all) to database bar from database foo. What is the SQL statement to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

所谓喜欢 2024-07-14 17:49:22

SQL Server Management Studio 的“导入数据”任务(右键单击数据库名称,然后单击任务)将为您完成大部分工作。 从要将数据复制到的数据库运行它。

如果表不存在,它会为您创建它们,但您可能必须重新创建任何索引等。 如果表确实存在,默认情况下它会附加新数据,但您可以调整它(编辑映射),以便它会删除所有现有数据。

我一直使用这个并且效果相当好。

SQL Server Management Studio's "Import Data" task (right-click on the DB name, then tasks) will do most of this for you. Run it from the database you want to copy the data into.

If the tables don't exist it will create them for you, but you'll probably have to recreate any indexes and such. If the tables do exist, it will append the new data by default but you can adjust that (edit mappings) so it will delete all existing data.

I use this all the time and it works fairly well.

中二柚 2024-07-14 17:49:22

在 SQL Server 上? 并在同一数据库服务器上? 使用三部分命名。

INSERT INTO bar..tblFoobar( *fieldlist* )
SELECT *fieldlist* FROM foo..tblFoobar

这只是移动数据。 如果您想移动表定义(以及其他属性,例如权限和索引),则必须执行其他操作。

On SQL Server? and on the same database server? Use three part naming.

INSERT INTO bar..tblFoobar( *fieldlist* )
SELECT *fieldlist* FROM foo..tblFoobar

This just moves the data. If you want to move the table definition (and other attributes such as permissions and indexes), you'll have to do something else.

撩心不撩汉 2024-07-14 17:49:22

这应该可行:

SELECT * 
INTO DestinationDB..MyDestinationTable 
FROM SourceDB..MySourceTable 

不会复制约束、默认值或索引。 创建的表将没有有聚集索引。

或者,您可以:

INSERT INTO DestinationDB..MyDestinationTable 
SELECT * FROM SourceDB..MySourceTable

如果您的目标表存在并且为空。

This should work:

SELECT * 
INTO DestinationDB..MyDestinationTable 
FROM SourceDB..MySourceTable 

It will not copy constraints, defaults or indexes. The table created will not have a clustered index.

Alternatively you could:

INSERT INTO DestinationDB..MyDestinationTable 
SELECT * FROM SourceDB..MySourceTable

If your destination table exists and is empty.

橘虞初梦 2024-07-14 17:49:22

如果只有一张表,那么您需要做的就是

  • 编写表定义脚本
  • 在另一个数据库中创建新表
  • 更新规则、索引、权限等
  • 导入数据(上面已经显示了几个插入示例)

您必须考虑的一件事是其他更新,例如将来迁移其他对象。 请注意,您的源表和目标表没有相同的名称。 这意味着如果您依赖视图、存储过程等对象,则还必须进行更改。

对于一个或多个对象,您可以手动进行操作,不会出现任何问题。 然而,当更新不止几个时,第三方比较工具就会非常方便。 现在我正在使用 ApexSQL Diff 进行架构迁移,但使用任何其他都不会出错那里有工具。

If it’s one table only then all you need to do is

  • Script table definition
  • Create new table in another database
  • Update rules, indexes, permissions and such
  • Import data (several insert into examples are already shown above)

One thing you’ll have to consider is other updates such as migrating other objects in the future. Note that your source and destination tables do not have the same name. This means that you’ll also have to make changes if you dependent objects such as views, stored procedures and other.

Whit one or several objects you can go manually w/o any issues. However, when there are more than just a few updates 3rd party comparison tools come in very handy. Right now I’m using ApexSQL Diff for schema migrations but you can’t go wrong with any other tool out there.

天气好吗我好吗 2024-07-14 17:49:22
  1. 在 Management Studio 中编写创建表脚本,在 bar 中运行该脚本来创建表。 (右键单击对象资源管理器中的表,将表脚本编写为,创建到...)

  2. INSERT bar.[schema].table SELECT * FROM foo.[schema].table

  1. Script the create table in management studio, run that script in bar to create the table. (Right click table in object explorer, script table as, create to...)

  2. INSERT bar.[schema].table SELECT * FROM foo.[schema].table

风为裳 2024-07-14 17:49:22

您还可以使用生成 SQL Server 脚本向导帮助指导 SQL 脚本的创建,该脚本可以执行以下操作:

  • 复制表架构
  • 任何约束(身份、默认值等)
  • 表中的
  • 数据以及许多其他选项(如果需要)

SQL Server 2008 的良好示例工作流程,其中显示屏幕截图 此处

You can also use the Generate SQL Server Scripts Wizard to help guide the creation of SQL script's that can do the following:

  • copy the table schema
  • any constraints (identity, default values, etc)
  • data within the table
  • and many other options if needed

Good example workflow for SQL Server 2008 with screen shots shown here.

饮湿 2024-07-14 17:49:22

您可以采用这种方式:(一般示例)

insert into QualityAssuranceDB.dbo.Customers (columnA, ColumnB)
Select columnA, columnB from DeveloperDB.dbo.Customers

此外,如果您还需要生成列名以放入插入子句中,请使用:

    select (name + ',') as TableColumns from sys.columns 
where object_id = object_id('YourTableName')

复制结果并粘贴到查询窗口中以表示您的表列名,甚至这将排除标识列:

    select (name + ',') as TableColumns from sys.columns 
where object_id = object_id('YourTableName') and is_identity = 0

请记住,如果数据库属于同一位置,则复制行的脚本将起作用。


你可以试试这个。

select * into <Destination_table> from <Servername>.<DatabaseName>.dbo.<sourceTable>

如果两个数据库位于同一服务器中,则服务器名称是可选的。

You may go with this way: ( a general example )

insert into QualityAssuranceDB.dbo.Customers (columnA, ColumnB)
Select columnA, columnB from DeveloperDB.dbo.Customers

Also if you need to generate the column names as well to put in insert clause, use:

    select (name + ',') as TableColumns from sys.columns 
where object_id = object_id('YourTableName')

Copy the result and paste into query window to represent your table column names and even this will exclude the identity column as well:

    select (name + ',') as TableColumns from sys.columns 
where object_id = object_id('YourTableName') and is_identity = 0

Remember the script to copy rows will work if the databases belongs to the same location.


You can Try This.

select * into <Destination_table> from <Servername>.<DatabaseName>.dbo.<sourceTable>

Server name is optional if both DB is in same server.

橘香 2024-07-14 17:49:22

我给你三个选项:

如果它们是同一实例上的两个数据库,则执行以下

SELECT  * INTO My_New_Table FROM [HumanResources].[Department];

操作: 如果它们是不同服务器上的两个数据库,并且您有链接服务器,则执行以下操作:

SELECT  * INTO My_New_Table FROM [ServerName].[AdventureWorks2012].[HumanResources].[Department];

如果它们是不同服务器上的两个数据库,并且您没有链接服务器,则执行以下操作:

SELECT * INTO My_New_Table
FROM OPENROWSET('SQLNCLI', 'Server=My_Remote_Server;Trusted_Connection=yes;',
     'SELECT * FROM AdventureWorks2012.HumanResources.Department');

I give you three options:

If they are two databases on the same instance do:

SELECT  * INTO My_New_Table FROM [HumanResources].[Department];

If they are two databases on different servers and you have linked servers do:

SELECT  * INTO My_New_Table FROM [ServerName].[AdventureWorks2012].[HumanResources].[Department];

If they are two databases on different servers and you don't have linked servers do:

SELECT * INTO My_New_Table
FROM OPENROWSET('SQLNCLI', 'Server=My_Remote_Server;Trusted_Connection=yes;',
     'SELECT * FROM AdventureWorks2012.HumanResources.Department');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文