日期:2014-05-17  浏览次数:20727 次

oracle初学者提问。。急。。。。
有以下语句
select * from table_a where to_date(check_date,'yyyyMM') in ('201208') 
这样可以查询出审核日期在2012年8月数据。

但我想将201208改成动态参数
做了如下操作
declare m_check_date date;
begin
m_check_date := to_date('201208','yyyyMM');
select * from table_a where to_date(check_date,'yyyyMM') in (check_date) ;--(真实语句中用到该变量的地方很多。。)
end;

在pl/sql 里面执行报错。。 

要求:不写成存储过程、试图或函数,希望在pl/sql里面直接执行输出。。。
(本人以前用sql server ,才转到oracle)  



------解决方案--------------------
这个地方不可以直接select 的

此处用游标取吧,定义cur ,然后cur for select ...
最后用dbms_out.put_line(cur.列)输出
------解决方案--------------------
把它做成子查询吧。

select * from table_a,(select to_date('201208','yyyyMM') m_date from dual) b where to_date(check_date,'yyyyMM') in b.m_date ;
------解决方案--------------------
declare 
m_check_date date;
xxx table_a%rowtype;
begin
m_check_date := to_date('201208','yyyyMM');
select * into xxx from table_a where to_date(check_date,'yyyyMM') in (check_date);
end;

这样可以输出一行,想都出来,用游标加个for循环。
ddl需要用动态sql的方式,不能直接在plsql中使用