0

我正在尝试运行此查询,但我不断收到此错误:

INSERT 语句的选择列表包含比插入列表更多的项目。SELECT 值的数量必须与 INSERT 列的数量相匹配。

这个错误是在我尝试执行 INSERT INTO #tempTable SELECT.... 当我每个临时表只有 2 列时它工作得很好,但是现在我已经添加了第三个,它一直给我这个错误,即使它似乎正在选择 3 列插入到具有 3 列的表中。

整个查询试图在不同的表上获取一些列值(客户 ID、联系人和销售人员 ID),但问题来自需要根除重复的客户 ID 并确保只有 1 个销售人员和 1 个联系人出现在每个客户面前。如果不是销售员 ID,查询将完美运行。这是我到目前为止所拥有的:

if object_id('tempdb..#tempTable') IS NOT NULL DROP TABLE #tempTable
if object_id('tempdb..#tempTable2') IS NOT NULL DROP TABLE #tempTable2

CREATE TABLE #tempTable(
CustomerID int,
ContactName nvarchar(50),
SalesmenID nvarchar(4)
)

CREATE TABLE #tempTable2(
CustomerID int,
ContactName nvarchar(50),
SalesmenID nvarchar(4)
)


INSERT INTO #tempTable 
    (CustomerID, ContactName,SalesmenID)
    SELECT Customers.[Customer ID],Salesmen.[4 Letter ID],
    CASE([Customer Contact].defaultprintonorder) 
        WHEN 0 
        THEN 'zzzzzzz_NOCONTACT'
        ELSE
        [Customer Contact].[Contact Name]
        END as ContactName
    From Customers
    LEFT JOIN [Customer Contact] on Customers.[Customer ID]=[Customer Contact].[Customer ID]
    Left Join [Customer Salesmen] On Customers.[Customer ID]=[Customer Salesmen].[Customer ID]
    INNER JOIN Salesmen on [Customer Salesmen].[Salesman Name]=Salesmen.[Salesman Name]
WHERE Customers.[Customer ID] NOT IN(SELECT CustomerID FROM #tempTable)
GROUP BY Customers.[Customer ID], [Contact Name], DefaultPrintOnOrder


INSERT INTO #tempTable2  (CustomerID, ContactName,SalesmenID)
SELECT distinct CustomerID, '', SalesmenID FROM #tempTable 

UPDATE  #TempTable2 SET 
    #tempTable2.CustomerID=#tempTable.CustomerID, 
    #tempTable2.ContactName=#tempTable.ContactName
FROM 
    #TempTable2
    INNER JOIN #TempTable ON #TempTable2.CustomerID=#TempTable.CustomerID

SELECT Salesmen.[4 Letter ID],
       [Customers].[Customer ID], 
       [Customer Contact].[Contact Name]
FROM Customers
Right JOIN #TempTable2 ON
    Customers.[Customer ID]=#TempTable2.CustomerID
Right JOIN [Customer Salesmen] ON
    #TempTable2.CustomerID=[Customer Salesmen].[Customer ID]
INNER JOIN  
    [Salesmen] ON
    [Customer Salesmen].[Salesman Name]=Salesmen.[Salesman Name]
LEFT JOIN 
    [Customer Contact] ON
    #TempTable2.[CustomerID]=[Customer Contact].[Customer ID]

编辑: 我将 SalesmenID 添加到插入中,但现在我收到此错误消息 3 次:

列名“SalesmenID”无效。

它为 temptable 插入出现一次,为 temptable2 插入出现两次

4

2 回答 2

3

您正在尝试在 2 个字段(CustomerID、ContactName)中存储 3 个值(Customers.[Customer ID]、Salesmen.[4 Letter ID]、ContactName)

    INSERT INTO #tempTable 
    (CustomerID, ContactName)
    SELECT Customers.[Customer ID],Salesmen.[4 Letter ID],
    CASE([Customer Contact].defaultprintonorder) 
    WHEN 0 
    THEN 'zzzzzzz_NOCONTACT'
    ELSE
    [Customer Contact].[Contact Name]
    END as ContactName
于 2012-06-06T21:07:41.847 回答
1

你确定问题出在你指出的地方吗?

此插入有 2 个目标列和 3 个选择列:

INSERT INTO #tempTable 
    (CustomerID, ContactName)    -- 2 columns

    SELECT Customers.[Customer ID], --column 1
            Salesmen.[4 Letter ID], --column 2
    CASE([Customer Contact].defaultprintonorder) -- column 3!
        WHEN 0 
        THEN 'zzzzzzz_NOCONTACT'
        ELSE
        [Customer Contact].[Contact Name]
        END as ContactName
    From Customers...
于 2012-06-06T21:05:09.430 回答