簡體   English   中英

將表數據從一個數據庫復制到具有條件的另一個數據庫

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

我有兩個表和表結構數目相同的數據庫。 我想將數據從一個表復制到具有where條件的另一個表中。

我已經嘗試過以下查詢,以下查詢是否正確,

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

請幫忙

更新 :我正在尋找與上面的單個查詢類似

請嘗試這個查詢

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

您必須先在查詢中指定db2.table attributes然后再執行

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

即:

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

v_dbf_db數據庫位於同一服務器上

這對我來說是工作

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

嘗試這個

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

但是請確保主鍵列上沒有重復,並且表中也沒有自動遞增列。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM