![](/img/trans.png)
[英]How to insert data from source table (but different columns) into two different tables in SQL Server
[英]Insert two columns from two different tables into another table in SQL
我一直在尋找解決我的問題的方法,但找不到任何地方。 這是我的問題:我有兩個不同的表中的兩列,並希望將它們插入到另一個空的表中。 我試圖運行此查詢,但它不起作用,我甚至不知道為什么:
SELECT a.column, c.column
FROM FirstTable.column a, SecondTable.column c
left outer join ThirdTable.column b on a.column = b.column
left outer join ThirdTable.column b on c.column = b.column
運行此后,我收到以下消息:
Msg 4104, Level 16, State 1, Line 3
The multi-part identifier "a.column" could not be bound.
Msg 1011, Level 16, State 1, Line 1
The correlation name 'b' is specified multiple times in a FROM clause.
我還想補充一點,a.column有1143行,c.column有2057行。 兩者都是各自表格的PK。
任何幫助都不僅僅是值得贊賞的。
嘗試重新構建您的查詢
SELECT a.column, c.column
FROM Table a left outer join Table b on a.column = b.column
left outer join Table c on c.column = b.column
如果您提供了確切的查詢本來會更好,但問題很可能是連接。
我使用了一組臨時表變量來顯示概念,但它應該很容易轉換為實際的表。
DECLARE @tabl1 TABLE (col1 int DEFAULT 1)
INSERT INTO @tabl1 VALUES (1)
DECLARE @tabl2 TABLE (col2 int DEFAULT 2)
INSERT INTO @tabl2 VALUES (2)
DECLARE @tabl3 TABLE (col1 int, col2 int)
-- This should be what you need.
INSERT INTO @tabl3
SELECT a.col1
, b.col2
FROM @tabl1 a, @tabl2 b
SELECT * FROM @tabl3
除非您正在進行笛卡爾連接,否則您會混合使用顯式和隱式語法。 我會選擇一個或另一個(我自己偏向顯式語法):
SELECT a.column, c.column
FROM Table a
left outer join Table b on a.column = b.column
left outer join Table c on b.column = c.column
或任何適用於您的結構。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.