select tCount from t_temp where tclass=1 and tName=col4 ,單獨執行這句會報錯, 但我需要對應更新多行.該怎麼寫呢?
update @t1 set col1=t_temp.tCount from t_temp where t_temp.tclass=1 and [email protected]是这个意思吗?
現在居然提示 Must declare @t1 代碼: declare @t1 table( col4 nvarchar(40), col1 smallint null, col2 smallint null , col3 smallint null , total smallint null )insert into @t1 (col4,total)select tName,sum(tCount) from t_temp group by tName update @t1 set col1=t_temp.tCount from t_temp where t_temp.tclass=1 and t_temp.tName= @t1.col4 //出錯行.@t1,沒有書寫錯誤.
insert into @t1 (col4,total)select tName,sum(tCount) from t_temp group by tName update @t1 set col1=t_temp.tCount from t_temp where t_temp.tclass=1 and t_temp.tName= @t1.col4 //出錯行. //改为 update @t1 set col1=t_temp.tCount from t_temp ,@t1 where t_temp.tclass=1 and t_temp.tName= @t1.col4
update @t1 set col1=tcount from (select count(tCount) tcount,tname from t_temp where tclass=1 group by tname ) b and tName=col4
update @t1 set col1=(select tCount from t_temp where tclass=1 and tName=col4 ) //此行出錯.因为子查询中返回的是记录集,而不是一个值
但我需要對應更新多行.該怎麼寫呢?
from t_temp where t_temp.tclass=1
and [email protected]是这个意思吗?
代碼:
declare @t1 table(
col4 nvarchar(40),
col1 smallint null,
col2 smallint null ,
col3 smallint null ,
total smallint null )insert into @t1 (col4,total)select tName,sum(tCount) from t_temp group by tName
update @t1 set col1=t_temp.tCount from t_temp where t_temp.tclass=1 and t_temp.tName= @t1.col4 //出錯行.@t1,沒有書寫錯誤.
update @t1 set col1=t_temp.tCount from t_temp where t_temp.tclass=1 and t_temp.tName= @t1.col4 //出錯行.
//改为
update @t1 set col1=t_temp.tCount from t_temp ,@t1 where t_temp.tclass=1 and t_temp.tName= @t1.col4
from (select count(tCount) tcount,tname from t_temp where tclass=1 group by tname ) b
and tName=col4