简体   繁体   English

将表数据从一个数据库复制到具有条件的另一个数据库

[英]copy table data from one db to another with where condition

i have two database with same number of tables & table structure. 我有两个表和表结构数目相同的数据库。 I want to copy data from one table into another with where condition. 我想将数据从一个表复制到具有where条件的另一个表中。

I have tried this below query, Is the below query is correct, 我已经尝试过以下查询,以下查询是否正确,

 INSERT INTO db2.table (SELECT * FROM db1.table t  where t.restaurant_id=12);

Please help 请帮忙

update : i am looking for a single query similer with above 更新 :我正在寻找与上面的单个查询类似

Please try this query 请尝试这个查询

select *into destination_database.dbo.destination table from _
source_database.dbo.source table where 1 = 2
USE AdventureWorks2012
GO
----Create TestTable
CREATE TABLE TestTable (FirstName VARCHAR(100), LastName VARCHAR(100))
----INSERT INTO TestTable using SELECT
INSERT INTO TestTable (FirstName, LastName)
SELECT FirstName, LastName
FROM Person.Person
WHERE EmailPromotion = 2
----Verify that Data in TestTable
SELECT FirstName, LastName
FROM TestTable
----Clean Up Database
DROP TABLE TestTable
GO

You must specify in your query db2.table attributes before doing 您必须先在查询中指定db2.table attributes然后再执行

SELECT * FROM db1.table t  where t.restaurant_id=12

ie: 即:

INSERT INTO db2.table (attribut1, attribut2,...)
SELECT * FROM db1.table t  where t.restaurant_id=12;

v_db and f_db databases are located in same server v_dbf_db数据库位于同一服务器上

and it is work for me 这对我来说是工作

INSERT INTO v_db.app_user (SELECT * FROM f_db.app_user AS t WHERE t.user_id = 100003083401232)

TRY THIS 尝试这个

INSERT INTO [DB].[schema].[table] 
(SELECT * FROM [DB].[schema].[table] t  where t.restaurant_id=12);

But make sure there is not duplication on primary key column and also there is not auto increment column in table. 但是请确保主键列上没有重复,并且表中也没有自动递增列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM