将一张表复制到另一张表失败-“每个表中的列名必须唯一”

AFD*_*AFD 2 sql-server

当我尝试这个时:

SELECT * 
-- INTO DB2.dbo.CustomerOrderLines
FROM DB1.dbo.CustomerOrderLines 
INNER JOIN DB1.dbo.CustomerOrders ON DB1.dbo.CustomerOrders.Order_Display_Ref = DB1.dbo.CustomerOrderLines.Order_Display_Ref
WHERE DB1.dbo.CustomerOrders.Delivered_Date BETWEEN '2009-09-23' and '2009-09-24'
Run Code Online (Sandbox Code Playgroud)

it show the rows correctly.

When I try to copy the contents from one table in DB1 into the same table in DB2 (and create it if it does not exist):

SELECT * 
INTO DB2.dbo.CustomerOrderLines
FROM DB1.dbo.CustomerOrderLines 
INNER JOIN DB1.dbo.CustomerOrders ON DB1.dbo.CustomerOrders.Order_Display_Ref = DB1.dbo.CustomerOrderLines.Order_Display_Ref
WHERE DB1.dbo.CustomerOrders.Delivered_Date BETWEEN '2009-09-23' and '2009-09-24'
Run Code Online (Sandbox Code Playgroud)

it fails with

Msg 2705, Level 16, State 3, Line 1 Column names in each table must be unique. Column name 'Order_Display_Ref' in table 'CustomerOrderLines' is specified more than once.

SELECT * INTO and INSERT INTO SELECT * work fine when copying other tables from one database into another, but they do not use JOINS.

What is my mistake?

Max*_*erl 5

You want to add the content of the DB1.dbo.CustomerOrderLines to DB2.dbo.CustomerOrderLines?

Then, tell SQL Server in your SELECT that you want only the content of this table.

SELECT DB1.dbo.CustomerOrderLines.* 
INTO DB2.dbo.CustomerOrderLines
FROM DB1.dbo.CustomerOrderLines 
INNER JOIN DB1.dbo.CustomerOrders ON DB1.dbo.CustomerOrders.Order_Display_Ref = DB1.dbo.CustomerOrderLines.Order_Display_Ref
WHERE DB1.dbo.CustomerOrders.Delivered_Date BETWEEN '2009-09-23' and '2009-09-24'
Run Code Online (Sandbox Code Playgroud)