日期:2014-05-17 浏览次数:20420 次
if OBJECT_ID('a') is not null
drop table a
if OBJECT_ID('b') is not null
drop table b
go
create table a(updatetime date)
create table b(holiday date)
insert into a values('2013-05-03'),
('2013-05-26'),
('2013-07-08')
insert into b values('2013-05-05'),
('2013-05-28'),
('2013-05-26'),
('2013-05-29'),
('2013-06-15'),
('2013-08-10')
select * from a
declare @adddays int=5
update a set updatetime=
DATEADD(dd,(select COUNT(1) from b where holiday>a.updatetime and holiday<dateadd(dd,@adddays,a.updatetime)),
updatetime) from a
select * from a
/*
updatetime
----------
2013-05-03
2013-05-26
2013-07-08
updatetime
----------
2013-05-04
2013-05-28
2013-07-08
*/