从大选择查询更新表

时间:2013-12-12 21:48:10

标签: sql sql-server tsql

我有一个需要更新的表extradata。它需要针对863个不同的帐户进行更新。

这是一个查询块,它返回临时表并将需要更改的帐号放入临时表中。

是否有一种简单的方法可以从此查询中进行批量更新?

declare @tmp table (number int)

insert into @tmp
select accountid as number from letterrequest
where lettercode in ('97001','70003','19998','91009','20000','99100','19997','70002','99099','91008','97002','97210','97231')
and dateProcessed >= '2013-12-04'
union all
select number from gizmo_requests
where letterCode in (97001,70003,19998,91009,20000,99100,19997,70002,99099,91008,97002,97210,97231)
and dateProcessed >= '2013-12-04'
union all
select number from jm_efiling
where letter in (97001,70003,19998,91009,20000,99100,19997,70002,99099,91008,97002,97210,97231)
and datePrinted >= '2013-12-04'

select distinct * from @tmp

此查询返回863行帐户ID。

update extradata set line4 = 'TEST'
where extracode = 'L3'
and number in @tmp

仅当数字列在上面的临时表中时。

2 个答案:

答案 0 :(得分:3)

尝试:

update extradata set line4 = 'TEST'
from extradata inner join @tmp T
on extradata.number = T.number
where extracode = 'L3'

您可以在更新查询中使用联接

答案 1 :(得分:1)

in语句中使用where子查询:

update extradata
    set line4 = 'TEST'
    where extracode = 'L3' and
          number in (select number from @tmp);