在SQL Server中更新具有多个条件的多行

时间:2018-02-08 04:44:16

标签: sql-server sql-update

我有这样的代码来更新多行

--ZZSEQUENCENBR
UPDATE AUSP 
SET CharacteristicValue = SUBSTRING(@RolNum, 5, 1) 
WHERE Object = @Batch AND Internalcharno = 'ZZSEQUENCENBR'

--ZZPOSITIONMS
UPDATE AUSP  
SET CharacteristicValue = SUBSTRING(@RolNum, 6, 1) 
WHERE Object = @Batch AND Internalcharno = 'ZZPOSITIONMS'

--ZZDERIVATIVEMS
UPDATE AUSP SET CharacteristicValue = SUBSTRING(@RolNum,7,1) WHERE Object = @Batch AND Internalcharno = 'ZZDERIVATIVEMS'
--ZZPOSITIONSS
UPDATE AUSP SET CharacteristicValue = SUBSTRING(@RolNum,8,1) WHERE Object = @Batch AND Internalcharno = 'ZZPOSITIONSS'
--ZZDERIVATIVESS
UPDATE AUSP SET CharacteristicValue = SUBSTRING(@RolNum,9,1) WHERE Object = @Batch AND Internalcharno = 'ZZDERIVATIVESS'
--ZZNOMORROLL
UPDATE AUSP SET CharacteristicValue = @RolNum WHERE Object = @Batch AND Internalcharno = 'ZZNOMORROLL'

--UPDATE Speed
UPDATE AUSP SET Valuefrom = @SPEED WHERE Object = @Batch AND InternalCharNo = 'ZZSPEED'
--Update Panjang, Lebar, berat & Xtra Length
UPDATE AUSP SET Valuefrom = @LebarActual WHERE Object = @Batch AND InternalCharNo = 'ZZWIDTH'
UPDATE AUSP SET Valuefrom = @PanjangActual WHERE Object = @Batch AND InternalCharNo = 'ZZLENGTH'    
SELECT @Thick = ZZTYPEFILM.[ThicknessFilm(Micron)], @Density = ZZTYPEFILM.[Density(g/Cm3)] FROM (SELECT CharacteristicValue FROM AUSP WHERE (InternalCharNo = N'ZZCODE') AND (Object = @Batch)) AS View1 INNER JOIN ZZTYPEFILM ON View1.CharacteristicValue = ZZTYPEFILM.KodeFilm
SET @QtyRol = ROUND((@LebarActual * @PanjangActual * convert(float,@Thick) * convert(float,@density))/1000000,1) 
UPDATE AUSP SET Valuefrom = @QtyRol WHERE Object = @Batch AND InternalCharNo = 'ZZCONVERSIONROLLKG'
UPDATE AUSP SET Valuefrom = @XtraPanjang WHERE Object = @Batch AND InternalCharNo = 'ZZEXLENGTH'

我尝试使用这样的代码来更新Internalcharno,而不是在Valuefrom:

UPDATE AUSP
SET CharacteristicValue = (case when Internalcharno = 'ZZPRODLINE' then 'EDIT'
                        when Internalcharno = 'ZZMONTHYEAR' then 'EDIT1'
                        when Internalcharno = 'ZZSEQUENCENBR' then 'EDIT2'
                        when Internalcharno = 'ZZPOSITIONMS' then 'EDIT3'
                        when Internalcharno = 'ZZDERIVATIVEMS' then 'EDIT4'
                        when Internalcharno = 'ZZPOSITIONSS' then 'EDIT5'
                        when Internalcharno = 'ZZDERIVATIVESS' then 'EDIT6'
                        when Internalcharno = 'ZZNOMORROLL' then 'EDIT7'
                end)
WHERE Internalcharno in ('ZZPRODLINE', 'ZZMONTHYEAR', 'ZZSEQUENCENBR', 'ZZPOSITIONMS', 
'ZZDERIVATIVEMS', 'ZZPOSITIONSS', 'ZZDERIVATIVESS', 'ZZNOMORROLL' ) AND
        [Object] = '414095';

但是这段代码会抛出一个错误:

  

Msg 512,Level 16,State 1,Procedure HistoryAUSPBeforeUpdate,Line 27
  子查询返回的值超过1。当子查询遵循=,!=,<,< =,>,> =或子查询用作表达式时,不允许这样做。
  声明已经终止。

有人可以帮忙吗?谢谢,

1 个答案:

答案 0 :(得分:1)

当您更改了问题时,我认为问题不在您的上次更新查询中,而是在此处的某处:

SELECT @Density = ZZTYPEFILM.[Density(g/Cm3)]
FROM (SELECT CharacteristicValue FROM AUSP WHERE (InternalCharNo = N'ZZCODE') 
AND (Object = @Batch)) AS View1 INNER JOIN ZZTYPEFILM ON View1.CharacteristicValue = ZZTYPEFILM.KodeFilm

确保您的每个子查询都为您提供单一值。

相关问题