SSMS加入不附加

时间:2017-11-06 19:38:51

标签: sql

我正在尝试进行简单的连接,但它无法正常工作。下面是我的测试数据代码。它们不是将它们连接在一起,而是将它们添加到底部。任何帮助,将不胜感激。我尝试在一个连接中进行所有操作并选择但无法使其工作,所以我试图将其分解。

    CREATE TABLE #inkoff
    ( 
            InvoiceDate varchar(50)
        ,   JobNumber varchar(50)
        ,   EstNum varchar(50)
        ,   Estimate varchar(50)
        ,   Actual varchar(50)
    )
    --This is an insert into select, but for testing purposes:
    INSERT INTO #inkoff (InvoiceDate, JobNumber, EstNum)
                        VALUES (20170101, '0001', 'E0001')
    INSERT INTO #inkoff (InvoiceDate, JobNumber, EstNum)
                        VALUES (20170201, '0002', 'E0002')
    INSERT INTO #inkoff (InvoiceDate, JobNumber, EstNum)
                        VALUES (20170301, '0003', 'E0003')
    INSERT INTO #inkoff (InvoiceDate, JobNumber, EstNum)
                        VALUES (20170401, '0004', 'E0004')
    --SELECT * FROM #inkoff
------------------------------------------------------------------------------------------------------------------------------------------------------------        
CREATE TABLE #inkest
    ( 
            EstNum varchar(50)
        ,   Estimate varchar(50)
    )
    --This is another insert into select but it is SELECT Distinct (EstimateNumber), Sum (Cost) as Estimate
    INSERT INTO #inkest (EstNum, Estimate)
        VALUES ('E0001', '100.25')
    INSERT INTO #inkest (EstNum, Estimate)
        VALUES ('E0002', '55.25')
    INSERT INTO #inkest (EstNum, Estimate)
        VALUES ('E0003', '75.26')
    INSERT INTO #inkest (EstNum, Estimate)
        VALUES ('E0004', '75.26')
    INSERT INTO #inkest (EstNum, Estimate)
        VALUES ('E0005', '100.01')                  
 --SELECT * FROM #inkest

 --Instead of appending them to the first four rows, it adds 5 rows to the bottom and doesn't join them up. 
 INSERT INTO #inkoff (Estimate) 
        SELECT A.Estimate FROM #inkest A 
        LEFT JOIN #inkoff  B    
        ON A.EstNum = B.EstNum 

SELECT * FROM #inkoff
SELECT * FROM #inkest

1 个答案:

答案 0 :(得分:1)

我怀疑你想要做的是update而不是插入,类似于:

    update b
    set b.estimate = a.estimate
    from #inkest a left join #inkoff b on a.EstNum=b.EstNum