Oracle PL SQL - EXECUTE IMMEDIATE 的问题
Posted
技术标签:
【中文标题】Oracle PL SQL - EXECUTE IMMEDIATE 的问题【英文标题】:Oracle PL SQL - Issue with EXECUTE IMMEDIATE 【发布时间】:2016-03-18 11:21:36 【问题描述】: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''
,所有语句都运行良好;
【问题讨论】:
因为 :start_epochtime 没有任何赋值。您需要添加“USING”子句。 【参考方案1】:这里似乎没有理由使用EXECUTE IMMEDIATE
。您没有构建动态查询,也没有执行 DDL 语句。我建议用
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
祝你好运。
【讨论】:
我想你可以写 sorterGROUP BY m.CREATIONDATE
,因为其他的都是不变的。
嗨 Bob,当我按照你的建议运行 SQL 时出现以下错误> 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
OK,所以要么在 SELECT 语句中添加 INTO 子句,要么将 SELECT 变成游标并遍历游标。祝你好运。【参考方案2】:
如果你真的想在 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 日期文字;但是,我确实这样做了认为您将 -30
放在错误的位置,因为它应该是 start_epochtime
而不是 end_epochtime
。)
【讨论】:
以上是关于Oracle PL SQL - EXECUTE IMMEDIATE 的问题的主要内容,如果未能解决你的问题,请参考以下文章
PL/SQL里 execute immediate的用法 谁给解释下