存储过程中有一语句如下,是直接返回结果集的:
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的。
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的。
解决方案 »
- 本地访问服务器上的oracle
- ORACLE字符集
- 在VM下如何完全彻底删除ORACLE中的一个数据库
- oracle高手请进来看看这个问题
- 在企業管理器里,點procedure,在選擇 view/edit details....,只能跳出個Edit Procedure得框,裡面得東東一點都
- Oracle 9i简体中文企业版[3CD] 快来下载.
- 如何选择数据库?
- 数据库导入错误
- 请问使用oracle的触发器会不会降低数据库的性能?
- 急!!在线等!!oracle 存储过程加入对工资求最大值,最小值,平均值,总值
- 急求一巧妙的ORACLE查询语句
- linux下利用oracle oci 访问编译 问题?
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;
像目前只有三个变量的,如果用if..else就需要判断6次。
我想知道除此而外有没有什么其它方法可以解决这一问题?
这样索引会有问题的,还是用拼装吧只用if就够了,不需要else,用java也一样
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))
这样对索引好像没有影响的。
用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中也不太现实。。
继续求解决方案。。
我现在的程序就是这样写的,可是有问题。如果表中有一条记录的empno字段为空,当in_empno为空时,这条empno字段为空的记录就查不出来了。
我不明白楼上说的算法很差是什么意思,难道是我理解错了?这里禁止拼装SQL,所以我以为你是说把整个open cur for(..)全部放到if里呢
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;
where NVL(empno,'*') = NVL(in_empno,NVL(empno,'*'))
and NVL(empna,'*') = NVL(in_empnanme,NVL(empno,'*'))
and NVL(salary,'*') = NVL(in_salary,NVL(empno,'*')))
open cur for sql
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;