not in 的问题

lg_asus 2011-03-31 10:20:39
从java中传过来一个数组,我想从表中查出id不在这个数组中的记录
cursor cur is select from t.name from XXX t where t.id not in temp;
其中temp是一个varchar2,在对数组循环时把数组中的所有id拼接起来赋给temp.然后打开这个cur,fetch,结果not in temp并不起作用。

我是这样拼接的:

for i in 1..arr.count
loop
temp := temp||''''||arr(i)||''''||',';
end loop;
temp:=substr(temp,1,length(temp)-1);
temp:= '('||temp||')';


请问我的代码哪里错了,或者大家有什么好的idea ?
...全文
224 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
sunjj1212 2011-04-08
  • 打赏
  • 举报
回复
('||v_empnos||') 这么写是可以当数字用么?貌似字符串in的时候是找不到的,数字可以
lg_asus 2011-03-31
  • 打赏
  • 举报
回复
多谢,我试下
luoyoumou 2011-03-31
  • 打赏
  • 举报
回复
-- 要not in ,无非就是这样修改一下:
create or replace procedure emp_proc(v_empnos varchar2, o_cur out sys_refcursor)
is
begin
open o_cur for 'select t.ename from emp t where t.empno not in ('||v_empnos||')';
end;
/
luoyoumou 2011-03-31
  • 打赏
  • 举报
回复
-- 用存储过程,将你的temp变量的内容整理后,当作参数传入存储过程去调用,取其结果!
-- temp中的内容格式为: 1,2,3,4,...,n
-- 具体看我3楼!
luoyoumou 2011-03-31
  • 打赏
  • 举报
回复
-- 给个例子给你:

eygle@SZTYORA> select empno, ename from emp;

EMPNO ENAME
---------- --------------------
7499 ALLEN
7521 WARD
7654 MARTIN
7844 TURNER
7900 JAMES
7369 SMITH
7876 ADAMS

已选择7行。

eygle@SZTYORA> create or replace procedure emp_proc(v_empnos varchar2, o_cur out sys_refcursor)
2 is
3 begin
4 open o_cur for 'select t.ename from emp t where t.empno in ('||v_empnos||')';
5 end;
6 /

过程已创建。

eygle@SZTYORA>
eygle@SZTYORA> set serveroutput on;
eygle@SZTYORA> var c_cur refcursor;
eygle@SZTYORA> exec emp_proc('7369,7876',:c_cur);

PL/SQL 过程已成功完成。

eygle@SZTYORA> print c_cur;

ENAME
--------------------
SMITH
ADAMS
lg_asus 2011-03-31
  • 打赏
  • 举报
回复
这样写也不行
v := v|| arr(i) ||',';

我刚才试了把数组中的id拼接起来,然后把cursor改成 instr(temp,t.id) = 0 就可以了。


大家还有什么好的idea 吗?
lg_asus 2011-03-31
  • 打赏
  • 举报
回复
上面cursor写错了

cursor cur is select t.name from XXX t where t.id not in temp;
lg_asus 2011-03-31
  • 打赏
  • 举报
回复
原来是()的问题。。多谢

3,491

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 高级技术相关讨论专区
社区管理员
  • 高级技术社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧