0

我必须将一个表的数据插入另一个表。我已经创建了一个程序。在过程中,一个 varchar2 变量代码如下 -

create or replace procedure proc_OneTimeLoad

 as
  query varchar2(5000);

begin

  OneTimeLoad_query := 'INSERT INTO trgt_table
              (Originterminal,
               Destinationterminal,
               Scheduledepartdate,
               Scheduledeparttime,
               Runtypenbr,
               DOW,
               Sched_Slip_Arr_Tm,
               DAY,
               Sched_Arr_Tm,
               Drivercode,
               Puprunflag,
               Sched_Cd,
               Status_Cd)

             SELECT Orgn_Term_Cd,
   Dest_Term_Cd,
   sysdate,
   Sched_Dep_Tm,
   Run_Nbr,
   DOW,
   to_date(
   LPAD(nvl(substr(tn.Sched_Slip_Arr_Tm,1,length(Sched_Slip_Arr_Tm)- 2),0),2,0)
   ||'''':''''||
   RPAD(nvl(TRUNC(TO_NUMBER(substr(Sched_Slip_Arr_Tm,3,length(Sched_Slip_Arr_Tm) - 2)) * .60,0),0),2,0)      
   ||'''':00''''
   ,''''HH24:MI:SS'''')
   as Sched_Slip_Arr_Tm,
   Day,
   to_date(
   LPAD(nvl(substr(tn.sched_arr_tm,1,length(sched_arr_tm)- 2),0),2,0)
   ||'''':''''||
   RPAD(nvl(TRUNC(TO_NUMBER(substr(sched_arr_tm,3,length(sched_arr_tm) - 2)) * .60,0),0),2,0)      
   ||'''':00''''
   ,''''HH24:MI:SS'''') 
   as Sched_Arr_Tm,
   Drvr_Cd,
   Pup_Run_Flg,
   ''''B'''',
   ''''1P''''
FROM src_table tn
WHERE tn.dow = to_char(sysdate - 1, ''''D'''')
AND tn.sched_dep_tm between
       TO_NUMBER(SUBSTR(TO_CHAR(sysdate, ''''HH24:MI:SS''''), 1, 2) ||
                 TRUNC(SUBSTR(TO_CHAR(sysdate, ''''HH24:MI:SS''''), 4, 2) * 100/60,0)) 
  AND
       TO_NUMBER(TO_NUMBER(SUBSTR(TO_CHAR(sysdate, ''''HH24:MI:SS''''), 1, 2)) + 12 ||
                 TRUNC(SUBSTR(TO_CHAR(sysdate, '''' HH24 :MI :SS ''''), 4, 2) * 100 / 60, 0))';

  execute immediate query;


  dbms_output.put_line(' Data loaded successfully');

end;

执行此过程时出现错误“缺少右括号。我认为[']有一些问题。我想在查询中使用''来连接字符串。就像争取时间

其中时间=to_date(HH||':'||mi||':'||ss,''HH24:MI:SS')

请帮助我的人。

4

1 回答 1

2

您的动态 SQL 似乎逃脱了太多。即它看起来更正确:

   OneTimeLoad_query := 'INSERT INTO trgt_table
               (Originterminal,
                Destinationterminal,
                Scheduledepartdate,
                Scheduledeparttime,
                Runtypenbr,
                DOW,
                Sched_Slip_Arr_Tm,
                DAY,
                Sched_Arr_Tm,
                Drivercode,
                Puprunflag,
                Sched_Cd,
                Status_Cd)    
              SELECT Orgn_Term_Cd,
    Dest_Term_Cd,
    sysdate,
    Sched_Dep_Tm,
    Run_Nbr,
    DOW,
    to_date(
    LPAD(nvl(substr(tn.Sched_Slip_Arr_Tm,1,length(Sched_Slip_Arr_Tm)- 2),0),2,0)
    ||'':''||
    RPAD(nvl(TRUNC(TO_NUMBER(substr(Sched_Slip_Arr_Tm,3,length(Sched_Slip_Arr_Tm) - 2)) * .60,0),0),2,0)
    ||'':00''
    ,''HH24:MI:SS'')
    as Sched_Slip_Arr_Tm,
    Day,
    to_date(
    LPAD(nvl(substr(tn.sched_arr_tm,1,length(sched_arr_tm)- 2),0),2,0)
    ||'':''||
    RPAD(nvl(TRUNC(TO_NUMBER(substr(sched_arr_tm,3,length(sched_arr_tm) - 2)) * .60,0),0),2,0)
    ||'':00''
    ,''HH24:MI:SS'')
    as Sched_Arr_Tm,
    Drvr_Cd,
    Pup_Run_Flg,
    ''B'',
    ''1P''
 FROM src_table tn
 WHERE tn.dow = to_char(sysdate - 1, ''D'')
 AND tn.sched_dep_tm between
        TO_NUMBER(SUBSTR(TO_CHAR(sysdate, ''HH24:MI:SS''), 1, 2) ||
                  TRUNC(SUBSTR(TO_CHAR(sysdate, ''HH24:MI:SS''), 4, 2) * 100/60,0))
   AND
        TO_NUMBER(TO_NUMBER(SUBSTR(TO_CHAR(sysdate, ''HH24:MI:SS''), 1, 2)) + 12 ||
                  TRUNC(SUBSTR(TO_CHAR(sysdate, ''HH24:MI:SS ''), 4, 2) * 100 / 60, 0))';

   execute immediate OneTimeLoad_query;

尽管给出了您的示例,但我不明白为什么此处不使用静态 SQL,因为您没有动态构建 SQL。即你为什么不开火:

INSERT INTO trgt_table
              (Originterminal,
               Destinationterminal,
               Scheduledepartdate,
               Scheduledeparttime,
               Runtypenbr,
               DOW,
               Sched_Slip_Arr_Tm,
               DAY,
               Sched_Arr_Tm,
               Drivercode,
               Puprunflag,
               Sched_Cd,
               Status_Cd)
             SELECT Orgn_Term_Cd,
   Dest_Term_Cd,
   sysdate,
   Sched_Dep_Tm,
   Run_Nbr,
   DOW,
   to_date(
   LPAD(nvl(substr(tn.Sched_Slip_Arr_Tm,1,length(Sched_Slip_Arr_Tm)- 2),0),2,0)
   ||':'||
   RPAD(nvl(TRUNC(TO_NUMBER(substr(Sched_Slip_Arr_Tm,3,length(Sched_Slip_Arr_Tm) - 2)) * .60,0),0),2,0)
   ||':00'
   ,'HH24:MI:SS')
   as Sched_Slip_Arr_Tm,
   Day,
   to_date(
   LPAD(nvl(substr(tn.sched_arr_tm,1,length(sched_arr_tm)- 2),0),2,0)
   ||':'||
   RPAD(nvl(TRUNC(TO_NUMBER(substr(sched_arr_tm,3,length(sched_arr_tm) - 2)) * .60,0),0),2,0)
   ||':00'
   ,'HH24:MI:SS')
   as Sched_Arr_Tm,
   Drvr_Cd,
   Pup_Run_Flg,
   'B',
   '1P'
FROM src_table tn
WHERE tn.dow = to_char(sysdate - 1, 'D')
AND tn.sched_dep_tm between
       TO_NUMBER(SUBSTR(TO_CHAR(sysdate, 'HH24:MI:SS'), 1, 2) ||
                 TRUNC(SUBSTR(TO_CHAR(sysdate, 'HH24:MI:SS'), 4, 2) * 100/60,0))
  AND
       TO_NUMBER(TO_NUMBER(SUBSTR(TO_CHAR(sysdate, 'HH24:MI:SS'), 1, 2)) + 12 ||
                 TRUNC(SUBSTR(TO_CHAR(sysdate, 'HH24:MI:SS '), 4, 2) * 100 / 60, 0))
于 2013-02-01T08:06:34.630 回答