最近有个存储过程的问题,自己写的sql效率太低,大哥们指点一下
要求:
更新表中上一条记录时间
表结构:
tablename  ->busiinfo 
userid  monney begin_dt end_dt
当前数据是这样的
somer  111     20071221 null
tod    222     20071221 null
crote  222     20071221 null
由于业务有批数据重新装入数据库
插入完数据是这样的
somer  333     20071226 null
tod    444     20071226 null
crote  555     20071226 null
dage   666     20071226 null (新添加的用户)现在库里数据是
somer  111     20071221 null
tod    222     20071221 null
crote  222     20071221 null
somer  333     20071226 null
tod    444     20071226 null
crote  555     20071226 null
dage   666     20071226 null (新添加的用户)要求做过业务的用户的上一条数据 end_dt = 当前beinf_dt-1 day
要求结果这样的
somer  111     20071221 20071225
tod    222     20071221 20071225 
crote  222     20071221 20071225
somer  333     20071226 null
tod    444     20071226 null
crote  555     20071226 null
dage   666     20071226 null (新添加的用户)
求个效率高的update

解决方案 »

  1.   

    数据入库后只会出现2种情况
    1。
    somer 111 20071119 20071120
    somer 111 20071121 NULL
    2
    somer 111 20071119 20071120
    somer 111 20071121 NULL
    somer 222 20071125 NULL每个用户的数据要么是有2条NULL数据,这种数据就需要把上个时间的数据end_dt 更新
    要么没有做业务,那就不需要更新他的end_dt
      

  2.   

    还是不明白。每次入库somer人的记录就会多一条
    入100次就有101条记录,那你怎么知道要更新哪条呢
    更新条件是什么,这个表不需要主键吗。
      

  3.   

    这个东西你用trigger写就可以了吧!每增加一条前,根据增加的内容在表中添加一个行就可以。
      

  4.   

    写个db2的trigger的例子吧。你参考下:
    create trigger schema.triggername after insert on schema.tablename 
    referencing new as n FOR EACH ROW MODE DB2SQL
    update schema.tablename as b set b.end_dt = n.start_dt where b.userid = n.userid and b.monney = n.money and b.end_dt is null
    其中 schema为表的模式,对应与oracle为用户 ; tablename为表名;triggername为触发器名称