仅传输具有匹配 Order Header 的行 Order Lines

Only transfer rows Order Lines with matching Order Header

我需要将行从我的 OrderLines table 迁移到新数据库,但我只希望已经迁移了匹配 headers 的行。

我正在使用 SQL Server Mgmt Studio 并尝试了以下代码但没有成功:

INSERT INTO dbNew.dbo.OrderLines
(columns)

SELECT columns 
FROM dbOld.dbo.OrderLines

WHERE EXISTS
 (SELECT * FROM dbNew.dbo.OrderHeader
  WHERE dbOld.dbo.OrderLines.OrderID = dbNew.dbo.OrderHeader.OrderID)

试试这个:

insert into dbNew.dbo.OrderLines (columns)
select columns
from dbOld.dbo.OrderLines old_lines
where exists (
    select null
    from dbNew.dbo.OrderHeader h
    where old_lines.OrderID = h.OrderID
)

编辑:糟糕,修正了错别字。

你可以试试这个:

INSERT INTO dbNew.dbo.OrderLines (columns)
SELECT columns 
FROM dbOld.dbo.OrderLines
where 
OrderID in
 (SELECT OrderID FROM dbNew.dbo.OrderHeader)