如何将表记录插入另一个数据库表 [英] How to insert table records into another database table

查看:70
本文介绍了如何将表记录插入另一个数据库表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个表从一个数据库复制并粘贴到另一个数据库。

一个数据库(db1)包含有关数据的所有详细信息,但db2没有数据.Db2包含db1的所有字段和表但不是数据。所以我想将一个表数据从db1复制并粘贴到db2。所以我把表作为一个脚本(右键单击表脚本 - 创建到 - 新的查询编辑窗口)。复制那个脚本和db2中的过去但错误来了



我尝试了什么:



请帮忙。

I want to copy and paste a table from one database to another.
one database(db1) has all details about data but db2 has no data .Db2 has all fields and table as of db1 but not datas .so i want to copy and paste that one table data from db1 to db2. so i make table as a script ( right click the table- script- create to - new query edit window).copy that script and past in db2 but error came

What I have tried:

please help.

推荐答案

INSERT INTO db1.[dbo].[insertfam]
select 
--Select ALL Columns except identity column--
from  db2.[dbo].[insertfam]


USE
  TargetDatabase
GO

INSERT INTO dbo.TargetTable(field1, field2, field3)
   SELECT field1, field2, field3
     FROM SourceDatabase.dbo.SourceTable  WHERE (some condition)



这会将详细信息从一个转移到另一个


this will transfer the details from one to another


如果数据库中不存在表,那么使用此解决方案< br $>


If table is not exist in database then use this solution

SELECT * INTO TargetDB.dbo.TableName   
    FROM [SourceDb].[dbo].[TableName]





使用此查询即可。

此处您必须根据数据库名称替换TargetDb和SourceDb以及TableName。



其他表存在于数据库中然后使用此表



By using this query you can do.
Here you have to replace TargetDb and SourceDb and TableName according to your database name.

Else table is exists in database then use this one

INSERT INTO [TargetDB].[dbo].[Department]
([Title],[Credits])
SELECT 
[Title],[Credits]
FROM [SourceDb].[dbo].[Department]



如果您仍然遇到任何问题,请告诉我。


Let me know if you still face any issue.


这篇关于如何将表记录插入另一个数据库表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆