比如我现在有两张表:
chnl_self_trade@dhcc 和 pos_points
我想在每天晚上6:00执行插入语句insert into pos_points select distinct(ac_no),'' from chnl_self_trade@dhcc where ac_no is not null表chnl_self_trade@dhcc中还有一个字段tx_date,记录的是交易日期,我想在插入的时候限制时间是从前天到昨天。这个存储过程怎么写啊,大家帮帮我啊

解决方案 »

  1.   

    create or replace procedure pr 
    is
    begin
       insert into pos_points 
       select distinct(ac_no),''
       from   chnl_self_trade@dhcc 
       where  ac_no is not null
       and    tx_date between trunc(sysdate-2) and trunc(sysdate-1);
    end pr;然后使用dbms_jobs定时执行。
    关于job的使用,参见:
    http://blog.ixpub.net/html/17/12285417-403019.html
      

  2.   

    楼上的这个语句是对的,还有一个问题是pos_points表中的第一个字段是主键,不能重复,而每天发生的交易中有很多交易账号是重复的,这样就会导致插入不进去,大家帮帮忙想想怎么解决这个问题啊?
      

  3.   

    可以使用merge的merge into pos_points a
    using (select distinct(ac_no) ac_no,''
       from   chnl_self_trade@dhcc 
       where  ac_no is not null
       and    tx_date between trunc(sysdate-2) and trunc(sysdate-1)) t
    on a.ac_no = t.ac_no
    when not matched then
    insert(a.c1) values(t.ac_no);
      

  4.   

     merge into pos_points a
    using (select distinct(ac_no),''
      from  chnl_self_trade@dhcc
      where  ac_no is not null
      and    to_date(tx_date,'yyyymmdd') between trunc(sysdate-10) and trunc(sysdate-1)) t
    on (a.ac_no = t.ac_no)
    when not matched then
    insert(a.ac_no)
    values(t.ac_no); 
    大哥,我按你的思路改后,报missing keyword 的错,您帮我看一下是哪错了啊?
      

  5.   

    你看看你的t视图里真的有ac_no字段吗
    注意加上列别名
      

  6.   

    merge into pos_points a
    using (select distinct(ac_no) ac_no,''
      from  chnl_self_trade@dhcc
      where  ac_no is not null
      and    to_date(tx_date,'yyyymmdd') between trunc(sysdate-10) and trunc(sysdate-1)) t
    on (a.ac_no = t.ac_no)
    when not matched then
    insert(a.ac_no)
    values(t.ac_no); 
    大哥,我加上了列的别名后,还是报错missing keyword,这条语句到底错在哪了呢,各位牛人大哥帮我写出一条可以执行的语句好吗,谢谢大家了
      

  7.   

    另外,这行语句是对的:
    select distinct(ac_no) ac_no,''
      from  chnl_self_trade@dhcc
      where  ac_no is not null
      and    to_date(tx_date,'yyyymmdd') between trunc(sysdate-10) and trunc(sysdate-1)
    可以出来结果,唯一可能错的地方就是merge语句了,大家帮我看看是哪错了啊。谢谢大家了
      

  8.   

    merge into pos_points a
    using (select distinct(ac_no) ac_no,''
      from  chnl_self_trade@dhcc
      where  ac_no is not null
      and    to_date(tx_date,'yyyymmdd') between trunc(sysdate-10) and trunc(sysdate-1)) t
    on (a.ac_no = t.ac_no)
    when matched then
    update set a.col2 = ' '
    when not matched then
    insert(a.ac_no)
    values(t.ac_no); 
      

  9.   

    9i的merge语句matched和mot matched要写全