A
_______________
id    value
1       1
2       2
3       3
4       10
5       11B
_______________
id    value     rAid
1       1         1
2       2         1
3       3         1
4       7         2
5       8         3
6       8         9
7       8         10把B表不存在A的值更新‘AAA’
使用sqlserver语句
查询B表结果:
id    value
1       1
2       1
3       1
4       2
5       3
6       AAA         
7       AAA       

解决方案 »

  1.   

    update B set value='AAA'
    from B
    where not exists(select * from A where A.id=B.id)
      

  2.   

    update B set value='AAA' where not exists(select 1 from A where id=B.id)
      

  3.   


    select b.id,(case when a.id is not null then ltrim(value) else 'AAA') val
    from b left join a on b.rAid = a.value
      

  4.   

    update B set value='AAA' where not exists(select 1 from A where id=B.id)
      

  5.   


    use tempdb;
    /*
    create table A
    (
    id int not null,
    value int not null
    );
    insert into A(id,value)
    values
    (1,1),
    (2,2),
    (3,3),
    (4,10),
    (5,11);create table B
    (
    id int not null,
    value int not null,
    rAid nvarchar(10) not null
    );
    insert into B(id,value,rAid)
    values
    (1,1,'1'),
    (2,2,'1'),
    (3,3,'1'),
    (4,7,'2'),
    (5,8,'3'),
    (6,8,'9'),
    (7,8,'10');
    */
    select B.id,
    case when A.value IS null then 'AAA' else B.rAid end as [value]
    from B
    left join A on A.id = B.id;