try this,update a
 set a.flh=b.rn
 from 表B a
 inner join
 (select *,row_number() over(partition by id,cpid order by wlid) 'rn'
  from 表B) b on a.id=b.id and a.cpid=b.cpid and a.wlid=b.wlid

解决方案 »

  1.   

    if object_id('[表A]') is not null drop table [表A]
    create table [表A]([id] int,[cpid] int)
    insert[表A]
    select 208,2223 union all
    select 209,2874 union all
    select 214,3340if object_id('[表B]') is not null drop table [表B]
    create table [表B]([id] int,[cpid] int,[wlid] numeric(4,3),[sl] int,[flh] sql_variant)
    insert[表B]
    select 208,2223,1.001,10,null union all
    select 208,2223,1.005,8,null union all
    select 208,2223,1.007,16,null union all
    select 209,2874,1.007,11,null union all
    select 209,2874,1.010,17,null union all
    select 214,3340,1.023,80,null union all
    select 214,3340,1.025,42,null union all
    select 214,3340,1.027,21,null union all
    select 214,3340,1.028,13,nullselect * from [表A]
    select * from [表B]SELECT *,ROW_NUMBER() OVER(PARTITION BY id,cpid ORDER BY id) AS num
    FROM [表B]
    WHERE EXISTS(SELECT 1 FROM [表A] WHERE [表A].id = [表B].id AND [表A].cpid=[表B].cpid)/*
    id cpid wlid sl flh num
    208 2223 1.001 10 NULL 1
    208 2223 1.005 8 NULL 2
    208 2223 1.007 16 NULL 3
    209 2874 1.007 11 NULL 1
    209 2874 1.010 17 NULL 2
    214 3340 1.023 80 NULL 1
    214 3340 1.025 42 NULL 2
    214 3340 1.027 21 NULL 3
    214 3340 1.028 13 NULL 4*/