Where子句不在sql server中工作

时间:2016-12-30 13:31:04

标签: sql-server

以下是我的SQL代码的一部分 -

INTO #tmpRecDistData2
    FROM #TempReceivingUnmatchedUPCs tr 
        FULL JOIN #tmpDistributorRecords td 
            ON  ( tr.RecUPC=td.DistUPC )
            where
            tr.RecSupplierInvoiceNumber=td.DistSupplierInvoiceNumber

这里where子句不起作用并且给出所有结果,无论两张发票是相同还是不同。 可能是什么原因?

修改

RecSupplierInvoiceNumber       DistSupplierInvoiceNumber
00000000111807                                    6745
00000000111807                                    5635
00000000111807                                    5676

这是我在使用where子句

后得到的输出
SELECT td.DistSupplierInvoiceNumber,td.DistProductDescription,td.DistUPC,
       td.DistProductID, td.DistTranslationExists,td.DistInCatalog,      tr.RecSupplierInvoiceNumber,tr.RecProductDescription,tr.RecUPC,tr.RecProductID

    INTO #tmpRecDistData2
    FROM #TempReceivingUnmatchedUPCs tr 
        FULL JOIN #tmpDistributorRecords td 
            ON  ( tr.RecUPC=td.DistUPC )
            WHERE RecSupplierInvoiceNumber=DistSupplierInvoiceNumber

1 个答案:

答案 0 :(得分:0)

让我们尝试创建一个自包含的示例 - 以下代码适用于我。

您可以将应用程序中的一些示例数据插入此玩具代码中,看看它是否按预期运行?如果确实如此,则问题出在代码的其他地方。

CREATE TABLE #TempReceivingUnmatchedUPCs (
RecUPC VARCHAR(20),
RecSupplierInvoiceNumber VARCHAR(20),
Foobar VARCHAR(20)
)

CREATE TABLE #tmpDistributorRecords  (
DistUPC VARCHAR(20),
DistSupplierInvoiceNumber VARCHAR(20),
Foobar VARCHAR(20)
)

INSERT INTO #TempReceivingUnmatchedUPCs
(RecUPC,RecSupplierInvoiceNumber,Foobar)
VALUES
('ABC',22,'FooA'),
('ABD',23,'BarB'),
('BBB',30,'RecA'),
('BBB',31,'RecB')

INSERT INTO #tmpDistributorRecords
(DistUPC,DistSupplierInvoiceNumber,Foobar)
VALUES
('ABA',22,'FooC'),
('BBD',43,'BarD'),
('BBB',30,'DistA'),
('BBB',32,'DistB')


SELECT *
FROM #TempReceivingUnmatchedUPCs tr 
        FULL JOIN #tmpDistributorRecords td 
            ON  ( tr.RecUPC=td.DistUPC )
            where
            tr.RecSupplierInvoiceNumber=td.DistSupplierInvoiceNumber

DROP TABLE #TempReceivingUnmatchedUPCs
DROP TABLE #tmpDistributorRecords

对我来说,这会产生

RecUPC               RecSupplierInvoiceNumber Foobar               DistUPC              DistSupplierInvoiceNumber Foobar
-------------------- ------------------------ -------------------- -------------------- ------------------------- --------------------
BBB                  30                       RecA                 BBB                  30                        DistA

和RecSupplierInvoiceNumber = DistSupplierInvoiceNumber按预期