子查询返回了多个值 - SQL

时间:2013-05-28 15:59:04

标签: sql sql-server sql-server-2008

运行下面的脚本时收到以下错误。任何帮助将不胜感激。

  

子查询返回的值超过1。这是不允许的   子查询跟随=,!=,<,< =,>,> =或当子查询用作   表达。

脚本

DROP TABLE ProductBase_TEMP

GO

SELECT     *
INTO            ProductBase_TEMP
FROM         PILOT.dbo.PART

GO

UPDATE    ProductBase
SET              CurrentCost =
                          (SELECT     ProductBase_temp.UNIT_LABOR_COST + ProductBase_temp.UNIT_BURDEN_COST + ProductBase_temp.UNIT_SERVICE_COST AS VISUAL_Cost
                            FROM          ProductBase INNER JOIN
                                                   ProductBase_temp ON ProductBase.ProductNumber = ProductBase_TEMP.ID COLLATE Latin1_General_CI_AI)
WHERE     (ProductNumber COLLATE Latin1_General_CI_AI =
                          (SELECT     ProductBase_temp.ID
                            FROM      ProductBase_temp INNER JOIN
                                          ProductBase ON ProductBase_temp.ID COLLATE Latin1_General_CI_AI = ProductBase.ProductNumber))

由于

1 个答案:

答案 0 :(得分:1)

问题是相关子查询中的连接:

UPDATE ProductBase
    SET CurrentCost = (SELECT ProductBase_temp.UNIT_LABOR_COST + ProductBase_temp.UNIT_BURDEN_COST + ProductBase_temp.UNIT_SERVICE_COST AS VISUAL_Cost
                       FROM ProductBase_temp
                       WHERE ProductBase.ProductNumber = ProductBase_TEMP.ID COLLATE Latin1_General_CI_AI
                      )
    WHERE (ProductNumber COLLATE Latin1_General_CI_AI =
              (SELECT ProductBase_temp.ID
               FROM ProductBase_temp 
               WHERE ProductBase_temp.ID COLLATE Latin1_General_CI_AI = ProductBase.ProductNumber
              )
          )

连接是ProductBase表的另一个副本,因此您获得了大量的行(完整的结果集)。您希望将查询与外部ProductBase相关联。