存储过程中有一语句如下,是直接返回结果集的:
open cur for(select *from emp 
                    where empno = in_empno 
                      and empna =in_empnanme
                      and salary = in_salary)in_empno、in_empnanme、in_salary都是是前台传入的变量,如果都为空,则查询所有语句,否则查询指定变量的记录。(这是一个很常见的按输入条件进行查询)
问题:当输入变量为空的时候,如何查询所有记录?我试过用 where empno=decode(in_empno,null,empno,in_empno)这种写法,可是,如果表中有empno字段为空时,这条记录就查不出来了。
我的目的是:当in_empno为空时(输入变量存在多个,其要求都是和这个变量一样的),就查询所有empno的记录,包括empno为空的也查出来,要求在一条SQL语句中实现。谢谢!数据库是oracle 10g的。

解决方案 »

  1.   

    例子:declare
    strsql1 varchar2(1000);
    strsql2 varchar2(1000);
    strsql varchar2(2000);
    begin
    strsql1:='';
    strsql2:='select * from emp where ';
    if  in_empno is not null then
    strsql1:=' empno = in_empno and ';
    end if;
    if in_empnanme is not null then
    strsql1:=strsql1||' empna =in_empnanme and ';
    end if;
    if in_salary is not null then
    strsql1:=strsql1||' salary = in_salary ';
    end if;
    strsql:=strsql2||strsql1;
      

  2.   

    加if..else不是理想的结果,因为前台输入条件很多,那得要多少个if..else啊?
    像目前只有三个变量的,如果用if..else就需要判断6次。
      

  3.   

    这个方法也有问题的,动态SQL不宜在后台使用。如果非得要用语句拼装,那我就干脆写java中好了。
    我想知道除此而外有没有什么其它方法可以解决这一问题?
      

  4.   

    empno=decode(in_empno,null,empno,in_empno)
    这样索引会有问题的,还是用拼装吧只用if就够了,不需要else,用java也一样
      

  5.   

    open cur for(select *from emp 
                        where empno = decode(in_empno,null,empno,in_empno)
                          and empna =decode(in_empnanme ,null,empnanme,in_empnanme )
                          and salary = decode(in_salary,null,salary,in_salary)) 
      

  6.   


    这样对索引好像没有影响的。
    用if判断次数太多,比如说我上面的三个变量如果用if的话:
    if in_empno is not null then
      ..where empno=in_empno;
    end if;
    if in_empno is not null and in_empname is not null then
    ..where empno=in_empno
        and empname=in_empname
    end if;
    ......(略)
    如上,三个变量要判断六次。
    后台禁止使用动态SQL,也就是说语句拼装是不行的了。但因其中算法还有很多,所以放在java中也不太现实。。
    继续求解决方案。。

      

  7.   


    我现在的程序就是这样写的,可是有问题。如果表中有一条记录的empno字段为空,当in_empno为空时,这条empno字段为空的记录就查不出来了。
      

  8.   


    我不明白楼上说的算法很差是什么意思,难道是我理解错了?这里禁止拼装SQL,所以我以为你是说把整个open cur for(..)全部放到if里呢
      

  9.   

    CREATE OR REPLACE PROCEDURE proc is 
    v_count number := 0;
    v_empno varchar := '';
    v_empna varchar := '';
    v_salary varchar := '';cursor v_cursor is
    select empno, empna, salary from emp ;begin
    open v_cursor ;
       loop
       fetch v_cursor into v_empno, v_empna, v_salary;
         select count(1) into v_count from emp where empno = v_empno and empna = v_empna and salary = v_salary;
         if v_count >= 1 then
    ---- do something
         end if;   end loop;
       
       close v_cursor;
    end;
      

  10.   

    open cur for(select *from emp 
                        where NVL(empno,'*') = NVL(in_empno,NVL(empno,'*'))
                          and NVL(empna,'*') = NVL(in_empnanme,NVL(empno,'*')) 
                          and NVL(salary,'*') = NVL(in_salary,NVL(empno,'*'))) 
      

  11.   

    其实楼主只要用动态游标就可以了
    open cur for sql
      

  12.   

    where (empno=decode(in_empno,null,empno,in_empno) or empno is null)
      

  13.   

    where (empno=in_empno or in_empno is null) and (empna=in_empnanme or in_empnanme is null) and (salary = in_salary or in_salary is null)
      

  14.   

    CREATE OR REPLACE PROCEDURE PRO(V_DEPTNO NUMBER,V_EMPNO NUMBER)
    IS
    TYPE EMP_CUR IS REF CURSOR RETURN EMP%ROWTYPE;
    CV_EMP EMP_CUR;
    V_EMP EMP%ROWTYPE;
    BEGIN
         IF V_DEPTNO IS NULL THEN
         OPEN CV_EMP FOR SELECT * FROM EMP;
         ELSE
         OPEN CV_EMP FOR SELECT * FROM EMP WHERE DEPTNO=V_DEPTNO AND EMPNO=V_EMPNO;
         END IF;
         FETCH CV_EMP INTO V_EMP;
         WHILE CV_EMP%FOUND LOOP
         DBMS_OUTPUT.PUT_LINE(V_EMP.ENAME);
         FETCH CV_EMP INTO V_EMP;
         END LOOP;
         CLOSE CV_EMP;
    END;