A表数据:
编号    日期         其他字段
1      2008-01-05
1      2008-01-07
2      2008-01-05
2      2008-06-05
B表数据:
编号   其他字段
1
2
3两表其他字段是一样的,要得到的效果是:将B表的其他字段中的值更新为A表中相应编号中日期最小的其他字段的值,
现在用的是游标,先取出A表中不同的编号,再根据编号取日期最小的其他字段,再更新B表.
效率很低,请问用SQL语句直UPDATE怎么写呢.不知道说清楚没有,下面是我的游标写法set @cur=cursor for select DISTINCT ID from tb
open @cur
fetch next from @cur into @ID
while @@fetch_status=0
begin
        select top 1 @字段1=其他字段1 from tb where ID=@ID order by 日期
        select top 1 @字段2=其他字段2 from tb where ID=@ID order by 日期
        update tbBarcodeHis
set 其他字段1=@其他字段1,其他字段2=@其他字段2
where ID=@ID 
fetch next from @cur into @ID
end
close @cur
deallocate @cur

解决方案 »

  1.   

    update b
     set b.其它字段=a.其它字段
    from tbb b,
     (select * from tba a where 日期=(select min(日期) from tba where 编号=a.编号)) a
    where b.编号=a.编号
      

  2.   

    -->生成测试数据
     
    declare @A表数据 table([编号] int,[日期] Datetime,[其他字段] nvarchar(1))
    Insert @A表数据
    select 1,'2008-01-05',N'f' union all
    select 1,'2008-01-07',N'f' union all
    select 2,'2008-01-05',N'f' union all
    select 2,'2008-06-05',N'f'
    Select * from @A表数据
    /*
    编号          日期                  其他字段
    ----------- ----------------------- ----
    1           2008-01-05 00:00:00.000 f
    1           2008-01-07 00:00:00.000 f
    2           2008-01-05 00:00:00.000 f
    2           2008-06-05 00:00:00.000 f
    */
    -->生成测试数据
     
    declare @B表数据 table([编号] int,[其他字段] nvarchar(1))
    Insert @B表数据
    select 1,N'A' union all
    select 2,N'B' union all
    select 3,N'C'
    --Select * from @B表数据UPDATE A
    SET A.[其他字段] = (SELECT [其他字段] FROM @B表数据 WHERE [编号] = A.[编号])
    FROM @A表数据 A
    WHERE NOT EXISTS(SELECT 1 FROM @A表数据 WHERE [编号] =A.[编号] AND [日期] <A.[日期])SELECT * FROM @A表数据
    /*
    编号          日期                    其他字段
    ----------- ----------------------- ----
    1           2008-01-05 00:00:00.000 A
    1           2008-01-07 00:00:00.000 f
    2           2008-01-05 00:00:00.000 B
    2           2008-06-05 00:00:00.000 f
    */
      

  3.   

    update b set 其他字段=t.其他字段
    from b,a t
    where t.编号=b.编号
    and not exists(select 1 from a where 编号=t.编号 and 日期<t.日期)
      

  4.   

    ->生成测试数据
     
    declare @A表数据 table([编号] int,[日期] Datetime,[其他字段] nvarchar(1))
    Insert @A表数据
    select 1,'2008-01-05',N'f' union all
    select 1,'2008-01-07',N'f' union all
    select 2,'2008-01-05',N'f' union all
    select 2,'2008-06-05',N'f'
     
    declare @B表数据 table([编号] int,[其他字段] nvarchar(1))
    Insert @B表数据
    select 1,N'A' union all
    select 2,N'B' union all
    select 3,N'C'
    Select * from @B表数据
    /*
    编号          其他字段
    ----------- ----
    1           A
    2           B
    3           C
    */
    UPDATE B
    SET B.[其他字段] = ISNULL(A.[其他字段],B.[其他字段])
    FROM @B表数据 B
    LEFT JOIN @A表数据 A ON A.[编号] = B.[编号] AND NOT EXISTS(SELECT 1 FROM @A表数据 WHERE [编号] =A.[编号] AND [日期] <A.[日期])
    SELECT * FROM @B表数据
    /*
    编号          其他字段
    ----------- ----
    1           f
    2           f
    3           C
    */
      

  5.   


    update B set 其他字段1=D.其他字段1,其他字段2=D.其他字段2 left join 
    (select distinct(A.编号),A.日期,A.其他字段 from A
    left join (select 编号,min(日期) as 日期 from B group by 编号) C
    on A.编号=C.编号 where A.finish_plane=B.finish_plane) D
    on B.编号=D.编号
      

  6.   

    300W的数据,只要用left join速度肯定不会很快的。update B set 其他字段1=D.其他字段1,其他字段2=D.其他字段2 left join 
    (select distinct(A.编号),A.日期,A.其他字段1,A.其他字段2,A.其他字段3 from A
    left join (select 编号,min(日期) as 日期 from B group by 编号) C
    on A.编号=C.编号 where A.finish_plane=B.finish_plane) D
    on B.编号=D.编号
      

  7.   

    楼是行的Finish_plane是什么?
    我用
    背着灵魂漫步
    的做发现比较快,只用了22秒.但是数据不对,不知道是不是我日期实际上是有两个日期字段的,也就是 Order By 日期1,日期2.
      

  8.   

    不好意思,写错,那段where 子句去掉即可update B set 其他字段1=D.其他字段1,其他字段2=D.其他字段2 left join 
    (select distinct(A.编号),A.日期,A.其他字段1,A.其他字段2,A.其他字段3 from A
    left join (select 编号,min(日期) as 日期 from B group by 编号) C
    on A.编号=C.编号) D
    on B.编号=D.编号
      

  9.   


    update B set 其他字段1=D.其他字段1,其他字段2=D.其他字段2 left join 
    (select distinct(A.编号),A.日期,A.其他字段1,A.其他字段2,A.其他字段3 from A
    left join (select 编号,min(日期) as 日期 from B group by 编号) C
    on A.编号=C.编号) D
    on B.编号=D.编号test ....test
      

  10.   


    update B set 其他字段1=D.其他字段1,其他字段2=D.其他字段2 from B left join 
    (select distinct(A.编号),A.日期,A.其他字段1,A.其他字段2,A.其他字段3 from A
    left join (select 编号,min(日期) as 日期 from B group by 编号) C
    on A.编号=C.编号) D
    on B.编号=D.编号
      

  11.   


    declare @A table([编号] int,[日期] Datetime,[其它字段] varchar(30))
    Insert @A
    select 1,'2008-01-05',N'A表编号,-01-05的数据' union all
    select 1,'2008-01-06',N'A表编号,-01-06的数据' union all
    select 2,'2008-02-04',N'A表编号,-02-04的数据' union all
    select 2,'2008-02-05',N'A表编号,-02-05的数据' union all
    select 2,'2008-02-06',N'A表编号,-02-06的数据'
     
    declare @B table([编号] int,[其它字段] varchar(30))
    Insert @B
    select 1,'B表编号的数据' union all
    select 2,'B表编号的数据' union all
    select 3,'B表编号的数据'
    Select * from @B/*
    编号 其它字段
    1 B表编号1的数据
    2 B表编号2的数据
    3 B表编号3的数据
    */;with A as
    (select [编号],[其它字段] from @A where [日期] in (select min([日期]) from @A group by [编号]))
    update @B
    set B.[其它字段] = A.[其它字段]
    FROM @B B, A
    where(B.[编号]=A.[编号])select * from @B/*
    编号 其它字段
    1 A表编号1,2008-01-05的数据
    2 A表编号2,2008-02-04的数据
    3 B表编号3的数据
    */
      

  12.   


    -->生成测试数据
     
    declare @A table([编号] int,[日期] Datetime,[其它字段] varchar(30))
    Insert @A
    select 1,'2008-01-05','A表编号1,2008-01-05的数据' union all
    select 1,'2008-01-06','A表编号1,2008-01-06的数据' union all
    select 2,'2008-02-04','A表编号2,2008-02-04的数据' union all
    select 2,'2008-02-05','A表编号2,2008-02-05的数据' union all
    select 2,'2008-02-06','A表编号3,2008-02-06的数据'
     
    declare @B table([编号] int,[其它字段] varchar(30))
    Insert @B
    select 1,'B表编号1的数据' union all
    select 2,'B表编号2的数据' union all
    select 3,'B表编号3的数据'
    Select * from @B;with A as
    (select [编号],[其它字段] from @A where [日期] in (select min([日期]) from @A group by [编号]))
    update @B
    set B.[其它字段] = A.[其它字段]
    FROM @B B, A
    where(B.[编号]=A.[编号])select * from @B
      

  13.   


    declare @A table([编号] int,[日期] Datetime,[其它字段] varchar(30))
    Insert @A
    select 1,'2008-01-05','A表编号1,2008-01-05的数据' union all
    select 1,'2008-01-06','A表编号1,2008-01-06的数据' union all
    select 2,'2008-02-04','A表编号2,2008-02-04的数据' union all
    select 2,'2008-02-05','A表编号2,2008-02-05的数据' union all
    select 2,'2008-02-06','A表编号2,2008-02-06的数据'
     
    declare @B table([编号] int,[其它字段] varchar(30))
    Insert @B
    select 1,'B表编号1的数据' union all
    select 2,'B表编号2的数据' union all
    select 3,'B表编号3的数据'
    Select * from @B/*
    编号 其它字段
    1 B表编号1的数据
    2 B表编号2的数据
    3 B表编号3的数据
    */;with A as
    (select [编号],[其它字段] from @A where [日期] in (select min([日期]) from @A group by [编号]))
    update @B
    set B.[其它字段] = A.[其它字段]
    FROM @B B, A
    where(B.[编号]=A.[编号])select * from @B/*
    编号 其它字段
    1 A表编号1,2008-01-05的数据
    2 A表编号2,2008-02-04的数据
    3 B表编号3的数据
    */