SQL Server:select有太多列用于插入

时间:2012-06-06 21:01:30

标签: sql-server-2000

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

  

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

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

整体查询试图在不同的表上获得一些列值(客户ID,联系人和销售员ID),但问题来自于需要根除重复的客户ID并确保只有1个推销员和每个客户出现1个联系人。如果它不是salemen 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'。

对于temptable2插入,它会出现一次,对于temptable2插入,它会出现两次

2 个答案:

答案 0 :(得分:3)

您尝试在2个字段(CustomerID,ContactName)中存储3个值(客户。[客户ID],销售员。[4个字母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

答案 1 :(得分: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...