我从前台传到后台几个数据:标题 title,名字 name,开始时间 starttime,结束时间endtime
然后我想写个数据库查询,查标题,名字,时间。  时间是开始时间和结束时间范围内的,标题,名字开始和结束时间都有可能为空这个该怎么写啊

解决方案 »

  1.   


    create or replace procedure p_test
    (
        in_title                varchar2,
        in_name                 varchar2,
        in_starttime            varchar2,
        in_endtime              varchar2
        out_cursor    out       sys_refcursor
    )
    as
        str_sql                 varchar2(1000);
    begin
        str_sql := 'select * from table_name where 1 = 1 ';
        if in_title is not null then
            str_sql := str_sql || ' and title = ''' || in_title || '''';
        end if;
        
        if in_name is not null then
            str_sql := str_sql || ' and name = ''' || in_name || '''';
        end if;
        
        if in_starttime is not null then
            str_sql := str_sql || ' and time >= ''' || in_starttime || '''';
        end if;
        
        if in_endtime is not null then
            str_sql := str_sql || ' and time <= ''' || in_endtime || '''';
        end if;
        
        open out_cursor for str_sql;
    end p_test;
      

  2.   

    select title,name,
    case when start_date is null or end_date is null then NULL else end_date-start_date end as period 
    end from tbl;没哟编译过,不知道有没有问题
      

  3.   


    with tb1 as(
    select 'title1' title,'name1' name,sysdate time from dual
    )
    select * from tb1 where title=nvl('title1',title) and name=nvl('name1',name) and time<=nvl(sysdate,time) and time>=nvl(to_date(null),time);
    select * from tb1 where title=nvl(传入的title,title) and name=nvl(传入的name,name) and time<=nvl(传入的starttime,time) and time>=nvl(传入的endtime,time);
      

  4.   


    with tb1 as(
    select 'title1' title,'name1' name,sysdate time from dual
    )
    select * from tb1
    where title=nvl(传入的title,title) and 
          name=nvl(传入的name,name) and 
          time<=nvl(传入的starttime,time) and 
          time>=nvl(传入的endtime,time);
    --test--
    select * from tb1 
    where title=nvl('title1',title) and 
          name=nvl('name1',name) and 
          time<=nvl(sysdate,time) and 
          time>=nvl(to_date(null),time);
      

  5.   

    +1
    不过为什么LZ不在后台重新组织句SQL去执行呢,这样写法很怪