Oracle PL SQL - EXECUTE IMMEDIATE问题

时间:2016-03-18 11:21:36

标签: sql oracle plsql cursor execute-immediate

DECLARE
  start_date VARCHAR2(12);
  end_date VARCHAR2(12);
  start_epochtime VARCHAR2(15);
  end_epochtime VARCHAR2(15);
  v_sql VARCHAR2(1024);
BEGIN
  SELECT to_char(current_date,'YYYY-MM-DD') into start_date from dual;
  SELECT to_char(current_date - 30,'YYYY-MM-DD') into end_date from dual;
  dbms_output.put_line(start_date);
  dbms_output.put_line(end_date);
  /* Below section will convert date to epochtime with hard code date value     */
  SELECT CAST((TO_DATE('2016-01-01','YYYY-MM-DD') - TO_DATE('1970-01-    01','YYYY-MM-DD') ) * 24 * 60 * 60 * 1000 AS VARCHAR(15)) into start_epochtime FROM DUAL;
  SELECT CAST((TO_DATE('2016-01-01','YYYY-MM-DD') - TO_DATE('1970-01-01','YYYY-MM-DD') - 30) * 24 * 60 * 60 * 1000 AS VARCHAR(15)) into end_epochtime FROM DUAL;
  dbms_output.put_line(start_epochtime);
  dbms_output.put_line(end_epochtime);
  /* Below section will convert date to epochtime with a variable */
  SELECT CAST((TO_DATE(start_date,'YYYY-MM-DD') - TO_DATE('1970-01-01','YYYY-MM-DD') ) * 24 * 60 * 60 * 1000 AS VARCHAR(15)) into start_epochtime FROM DUAL;
  SELECT CAST((TO_DATE(end_date,'YYYY-MM-DD') - TO_DATE('1970-01-01','YYYY-MM-DD') - 30) * 24 * 60 * 60 * 1000 AS VARCHAR(15)) into end_epochtime FROM DUAL;
  dbms_output.put_line(start_epochtime);
  dbms_output.put_line(end_epochtime);
  EXECUTE IMMEDIATE q'[select to_char((TO_DATE('1970-01-01','yyyy-mm-dd') + (m.CREATIONDATE/1000/24/60/60)),'YYYY-MM-DD'),count(1) from jivemessage_us m where m.CREATIONDATE >= :start_epochtime and m.CREATIONDATE <= :end_epochtime group by to_char((TO_DATE('1970-01-01','yyyy-mm-dd') + (m.CREATIONDATE/1000/24/60/60)),'YYYY-MM-DD') order by 1]';
END;
/

运行此pl sql时出现此错误ORA-01008: not all variables bound。并且,除EXECUTE IMMEDIATE q'';

之外,所有语句都正常运行

2 个答案:

答案 0 :(得分:2)

这里似乎没有理由使用EXECUTE IMMEDIATE。您不是在构建动态查询,也不是在执行DDL语句。我建议用{/ 1>替换EXECUTE IMMEDIATE

select to_char((TO_DATE('1970-01-01','yyyy-mm-dd') + 
                  (m.CREATIONDATE/1000/24/60/60)),'YYYY-MM-DD'),
       count(1)
  from jivemessage_us m
  where m.CREATIONDATE >= start_epochtime and
        m.CREATIONDATE <= end_epochtime
  group by to_char((TO_DATE('1970-01-01','yyyy-mm-dd') +
                      (m.CREATIONDATE/1000/24/60/60)),'YYYY-MM-DD')
  order by 1

祝你好运。

答案 1 :(得分:1)

如果您真的想在PL / SQL中完成所有操作,那么您可以这样做:

VARIABLE cur REFCURSOR;

DECLARE
  start_date      VARCHAR2(12);
  end_date        VARCHAR2(12);
  start_epochtime VARCHAR2(15);
  end_epochtime   VARCHAR2(15);
  v_sql           VARCHAR2(1024);
BEGIN
  start_date := TO_CHAR(current_date,      'YYYY-MM-DD');
  end_date   := TO_CHAR(current_date - 30, 'YYYY-MM-DD');
  dbms_output.put_line(start_date);
  dbms_output.put_line(end_date);

  /* Below section will convert date to epochtime with hard code date value     */
  start_epochtime := ( DATE '2016-01-01' - DATE '1970-01-01' ) * 24 * 60 * 60 * 1000;
  end_epochtime   := ( DATE '2016-01-01' - DATE '1970-01-01' - 30 ) * 24 * 60 * 60 * 1000;
  dbms_output.put_line(start_epochtime);
  dbms_output.put_line(end_epochtime);

  /* Below section will convert date to epochtime with a variable */
  start_epochtime := ( CURRENT_DATE      - DATE '1970-01-01' ) * 24 * 60 * 60 * 1000;
  end_epochtime   := ( CURRENT_DATE - 30 - DATE '1970-01-01' ) * 24 * 60 * 60 * 1000;
  dbms_output.put_line(start_epochtime);
  dbms_output.put_line(end_epochtime);

  OPEN :cur FOR
  select   to_char(DATE '1970-01-01' + CREATIONDATE/1000/24/60/60,'YYYY-MM-DD'),
           count(1)
  from     jivemessage_us
  where    CREATIONDATE BETWEEN start_epochtime and end_epochtime
  group by CREATIONDATE
  order by 1;
END;
/

PRINT cur;

但在SQL中执行它会更简单:

select   to_char(DATE '1970-01-01' + CREATIONDATE/1000/24/60/60,'YYYY-MM-DD'),
         count(1)
from     jivemessage_us
where    CREATIONDATE BETWEEN ( CURRENT_DATE      - DATE '1970-01-01' )*24*60*60*1000
                    AND     ( CURRENT_DATE - 30 - DATE '1970-01-01' )*24*60*60*1000
group by CREATIONDATE
order by 1;

(注意:我已经按原样离开了你的逻辑,但是从连续上下文切换到PL / SQL到SQL只是为了尽可能地使用PL / SQL和ANSI Date文字;但是,我做了认为-30位于start_epochtime而不是end_epochtime的错误位置。