SQL SERVER如何更新其他表中存在的表中的所有行

时间:2017-06-20 01:29:02

标签: sql sql-server division exists

我有这个UPDATE SENTENCE更新行,如果存在为每一行进行除法,每行都有不同的Rate / HR

UPDATE CP_ValueStream_Data
        SET
        CP_ValueStream_Data.W1 = (SELECT ROUND(CAST((SELECT [CP_Demand_G&PS].W1 FROM [CP_Demand_G&PS]) AS FLOAT)/CAST(Rate AS FLOAT),2)),
        CP_ValueStream_Data.W2 = (SELECT ROUND(CAST((SELECT [CP_Demand_G&PS].W2 FROM [CP_Demand_G&PS]) AS FLOAT)/CAST(Rate_Hr AS FLOAT),2)),
        CP_ValueStream_Data.W3 = (SELECT ROUND(CAST((SELECT [CP_Demand_G&PS].W3 FROM [CP_Demand_G&PS]) AS FLOAT)/CAST(Rate_Hr AS FLOAT),2)),
        CP_ValueStream_Data.W4 = (SELECT ROUND(CAST((SELECT [CP_Demand_G&PS].W4 FROM [CP_Demand_G&PS]) AS FLOAT)/CAST(Rate_Hr AS FLOAT),2)),
        CP_ValueStream_Data.W5 = (SELECT ROUND(CAST((SELECT [CP_Demand_G&PS].W5 FROM [CP_Demand_G&PS]) AS FLOAT)/CAST(Rate_Hr AS FLOAT),2)),
        CP_ValueStream_Data.W6 = (SELECT ROUND(CAST((SELECT [CP_Demand_G&PS].W6 FROM [CP_Demand_G&PS]) AS FLOAT)/CAST(Rate_Hr AS FLOAT),2)),
        CP_ValueStream_Data.W7 = (SELECT ROUND(CAST((SELECT [CP_Demand_G&PS].W7 FROM [CP_Demand_G&PS]) AS FLOAT)/CAST(Rate_Hr AS FLOAT),2)),
        CP_ValueStream_Data.W8 = (SELECT ROUND(CAST((SELECT [CP_Demand_G&PS].W8 FROM [CP_Demand_G&PS]) AS FLOAT)/CAST(Rate_Hr AS FLOAT),2)),
        CP_ValueStream_Data.W9 = (SELECT ROUND(CAST((SELECT [CP_Demand_G&PS].W9 FROM [CP_Demand_G&PS]) AS FLOAT)/CAST(Rate_Hr AS FLOAT),2)),
        CP_ValueStream_Data.W10 = (SELECT ROUND(CAST((SELECT [CP_Demand_G&PS].W10 FROM [CP_Demand_G&PS]) AS FLOAT)/CAST(Rate_Hr AS FLOAT),2)),
        CP_ValueStream_Data.W11 = (SELECT ROUND(CAST((SELECT [CP_Demand_G&PS].W11 FROM [CP_Demand_G&PS]) AS FLOAT)/CAST(Rate_Hr AS FLOAT),2)),
        CP_ValueStream_Data.W12 = (SELECT ROUND(CAST((SELECT [CP_Demand_G&PS].W12 FROM [CP_Demand_G&PS]) AS FLOAT)/CAST(Rate_Hr AS FLOAT),2)),
        CP_ValueStream_Data.W13 = (SELECT ROUND(CAST((SELECT [CP_Demand_G&PS].W13 FROM [CP_Demand_G&PS]) AS FLOAT)/CAST(Rate_Hr AS FLOAT),2)),
        Active = 1
        FROM
            [CP_Demand_G&PS]
            INNER JOIN CP_ValueStream_Data 
        ON [CP_Demand_G&PS].RowLabels = CP_ValueStream_Data.Material

但是当我执行查询时,这会返回错误

子查询返回的值超过1。当子查询遵循=,!=,<,< =,>,> =或子查询用作表达式时,不允许这样做。

1 个答案:

答案 0 :(得分:0)

您不需要子查询:

UPDATE CP_ValueStream_Data
    SET CP_ValueStream_Data.W1 = ROUND(CAST(d.W1 AS FLOAT)/CAST(Rate AS FLOAT), 2),
        . . .
        Active = 1
        FROM [CP_Demand_G&PS] d INNER JOIN 
             CP_ValueStream_Data v
             ON d.RowLabels = v.Material;