日期:2014-05-19  浏览次数:20495 次

存储过程中字段更新的问题
我有2个表:
表A:
productcode,value100,value105,value110.....value300,content100,content105....content300((名字中数值以5递增)
  aaa                     200             150               200....             300
  bbb                     100             100               100....             100
  ccc                     200             200               200....             200

表B:
productcode,name1from,name1value,name2from,name2value,name3from,name3value
        aaa               100                 20                   null             null               null             null
        bbb               100                 10                   110                 9                   null             null
        ccc               100                 12                   200                 11                 280                 10
       

name1from~name3from表示表A中字段value后面的数字

根据productcode更新a表字段:

aaa:content100~content300字段   对应   表B中的name1value,为20
bbb:   content300~content110字段   对应   表B中的name1value,为10
          content115~content300字段   对应   表B中的name2value,为9
ccc:   content100~content200字段   对应   表B中的name1value,为12
          content205~content280字段   对应   表B中的name2value,为11
          content285~content300字段   对应   表B中的name3value,为10

我现在在存储过程中是根据a中的productcode,取出b中所有对应的记录,然后遍历b中结果,更新a表中的字段,可是这样感觉效率比较低,有没有更好的方法呢?谢谢




------解决方案--------------------
就得用case when这个了,一定写得很长,要不就动态写SQL了
------解决方案--------------------
用动态SQL。
------解决方案--------------------
up。
------解决方案--------------------
动态sql好维护,但是效率比 when case更低吧!