有两个表table1 table2
table1 结构如下    table2结构如下
f1  f2             F1    F2   F3
a   1              a     0    d
b   2              b     0    f
c   3              c     0    s
我是要这样处理的:
table1先处理完成后,然后根据table1的f2字段一次性更新table2的F2字段
使得table2的结构如下
 F1    F2   F3
 a     1    d
 b     2    f
 c     3    s
其中f1是主键
请问这样存储过程要怎么写

解决方案 »

  1.   

    update table2  set f2=(select f2 from table1 where table1.f1=table2.f1)
      

  2.   

    不过楼上的方法有个毛病,比如table2有table1中没有的记录,那么table2的字段f2也被改了且改成了NULL,有什么好的方法避免,我只想改table2中有table1的记录
      

  3.   

    CREATE PROCEDURE UpF2
    @f2 char(?)
    As
    update set table2.f2=table1.F2 
    from table1 left join table2 on table1.f1=table2.F1 order by f1
    where table1.f1=@f2 
    go
      

  4.   

    update table2  set f2=(select f2 from table1 where table1.f1=table2.f1)
    where f1 in (select f1 from talble1)
      

  5.   

    update table2 set table2.f2=table1.F2 
    from table2 left join table1 on table2.f1=table1.F1