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

游标中带时间条件的查询语句怎么写
open eng for 'select * from mytable where mytable.apldate>('2012-05-01','yyyy-mm-dd')';
loop
.....
end loop;
如果我这样写是会报错的,因为2012-05-01和yyyy-mm-dd是用单引号引起来的,而外围又是单引号,如果没有时间
条件,比如:
open eng for 'select * from mytable';
loop
.....
end loop;
这样就没有问题,但是如果要加上时间条件,该怎么写呢,我试了好几种方式,但是都不行。
请教各位要怎么写才规范呢。。。。
------最佳解决方案--------------------

--字符串内,两个单引号代表select语句里的一个单引号
open eng for 'select * from mytable where mytable.apldate>(''2012-05-01'',''yyyy-mm-dd'')';

------其他解决方案--------------------
open eng for 'select * from mytable where mytable.apldate>(''2012-05-01'',''yyyy-mm-dd'')';
------其他解决方案--------------------
加两个单引号的方式,我试过,没有用
------其他解决方案--------------------
引用:
加两个单引号的方式,我试过,没有用

报什么错?
------其他解决方案--------------------

declare
type emp_cur_type is ref cursor return emp%rowtype;  --定义强游标类型
emp_cur emp_cur_type;
emp_record emp%rowtype;
begin
  open emp_cur for select * from emp where hiredate>to_date('1980-01-01','yyyy-mm-dd') ;
  loop
   fetch emp_cur into emp_record;
   exit when emp_cur%notfound;
   dbms_output.put_line(emp_record.ename);
  end loop;
  close emp_cur;
end;

SMITH
ALLEN
WARD
JONES
MARTIN
BLAKE
CLARK
SCOTT
KING
TURNER
ADAMS
JAMES
FORD
MILLER

------其他解决方案--------------------
原来是可以把外围的引号都不要了,没错,这样就可以执行了,谢谢大哥,
小弟马上去结贴