表结构如下:
表一
A   B   C   D
123表二
A1  B1 C1 
123 84 100
123 85 200
123 86 300表一的A字段与表二的A1关联,如果表二的B1中84对应的记录存在,则在表一中的B字段填入100,表二的B1中85对应的记录存在,C中填入200,依次类推如上述描述的数据
则表一的数据为
A   B   C   D
123 100 200 300用一条SQL该如何实现?(SQLServer)
不用存储过程之类的。

解决方案 »

  1.   


    update A
    SET B=B1,C=C1
    FROM 表一 as A inner join 表二 as B on A.A=B.A1
      

  2.   


    create table tab1
    (A int, B int, C int, D int)insert into tab1(A) values(123)create table tab2
    (A1 int, B1 int, C1 int)  insert into tab2
    select 123, 84, 100 union all
    select 123, 85, 200 union all
    select 123, 86, 300
    update t1
    set t1.B=(select top 1 C1 from tab2 t2 where t2.A1=t1.A and t2.B1=84),
        t1.C=(select top 1 C1 from tab2 t2 where t2.A1=t1.A and t2.B1=85),
        t1.D=(select top 1 C1 from tab2 t2 where t2.A1=t1.A and t2.B1=86)
    from tab1 t1select * from tab1/*
    A           B           C           D
    ----------- ----------- ----------- -----------
    123         100         200         300(1 row(s) affected)
    */