IN 子句元素个数超过1000处理

一、解决方法一

/* Oracle Database 10g Release 10.2.0.1.0 */

CREATE OR REPLACE TYPE t_num_arr IS TABLE OF VARCHAR2(30);
/
CREATE OR REPLACE FUNCTION f_num_arr(str VARCHAR2) RETURN t_num_arr pipelined
IS
    l_start NUMBER := 1;
    l_end NUMBER;
BEGIN
    l_end := INSTR(str,',',l_start);
    WHILE l_end >0 loop
        pipe ROW(SUBSTR(str,l_start,l_end-l_start));
        l_start:=l_end+1;
        l_end := INSTR(str,',',l_start);
    END loop;
    pipe ROW(SUBSTR(str,l_start));
end;
/
-- build a test string that has 1001 fields
CREATE OR REPLACE FUNCTION f_test_str RETURN VARCHAR2
IS
l_str VARCHAR2(32767);
BEGIN
    FOR i IN 1 .. 1000 LOOP
        l_str := l_str || i || ',';
    END loop;
    l_str := l_str || '1001'
    RETURN l_str;
end;
/
SELECT dummy
FROM dual
WHERE dummy IN (SELECT column_value FROM TABLE(f_num_arr(f_test_str)));

 

二、解决方法二,使用子查询

例如:select * from tablename where id in (valueList)

改为:select * from tablename where id in (select id from tablename1)


posted on 2011-12-13 15:20  洞幺人生  阅读(287)  评论(0编辑  收藏  举报