22,210
社区成员
发帖
与我相关
我的任务
分享
create table #t1
(
intid int not null,
datep smalldatetime,
price decimal(19,2) null
)
insert into #t1 select 1,'2008-8-1', 1.1
insert into #t1 select 1,'2008-9-1', 1.2
insert into #t1 select 2,'2008-9-1', 2.1
insert into #t1 select 2,'2008-10-1', 2.2
insert into #t1 select 2,'2008-10-10', 2.3
create table #t2
(
intid int not null,
indate smalldatetime,
price decimal(19,2) null
)
insert into #t2 (intid,indate) select 1, '2008-9-10'
insert into #t2 (intid,indate) select 2, '2008-10-8'
update #t2 set price = (select top 1 price from #t1 n where n.intid = m.intid and n.datep < m.indate order by n.datep desc) from #t2 m
select * from #t2
drop table #t1, #t2
/*
intid indate price
----------- ------------------------------------------------------ ---------------------
1 2008-09-10 00:00:00 1.20
2 2008-10-08 00:00:00 2.20
(所影响的行数为 2 行)
*/
create table #t1
(
intid int not null,
datep smalldatetime,
price decimal(19,2) null
)
insert into #t1 select 1,'2008-8-1', 1.1
insert into #t1 select 1,'2008-9-1', 1.2
insert into #t1 select 2,'2008-9-1', 2.1
insert into #t1 select 2,'2008-10-1', 2.2
insert into #t1 select 2,'2008-10-10', 2.2
create table #t2
(
intid int not null,
indate smalldatetime,
price decimal(19,2) null
)
insert into #t2 (intid,indate) select 1, '2008-9-10'
insert into #t2 (intid,indate) select 2, '2008-10-8'
update #t2 set price = (select top 1 price from #t1 n where n.intid = m.intid and n.datep < m.indate order by n.datep desc) from #t2 m
select * from #t2
drop table #t1, #t2
/*
intid indate price
----------- ------------------------------------------------------ ---------------------
1 2008-09-10 00:00:00 1.20
2 2008-10-08 00:00:00 2.20
(所影响的行数为 2 行)
*/