2017-07-07 10 views
-1

工作我在PLSQL很新,我想數量的數組傳遞到IN()子句的值,但是的SQLDeveloper拋出以下錯誤消息:PLSQL腳本不傳遞多個值到IN()caluse

Error report - 
ORA-06550: line 11, column 60: 
PLS-00382: expression is of wrong type 
ORA-06550: line 11, column 53: 
PL/SQL: ORA-22905: cannot access rows from a non-nested table item 
ORA-06550: line 10, column 4: 
PL/SQL: SQL Statement ignored 
06550. 00000 - "line %s, column %s:\n%s" 
*Cause: Usually a PL/SQL compilation error. 
*Action: 

這是我的代碼:

DECLARE 
    TYPE sc IS TABLE OF transactionhistory.NBSUBCOMPANY%TYPE INDEX BY PLS_INTEGER; 
    arr sc; 
BEGIN 
    arr(0) := 000; 
    arr(1) := 111; 
    arr(2) := 222; 
    arr(3) := 333; 

    select count(th.CHCARDNUMBER) as transactions from transactionhistory th INNER JOIN cards ch on ch.NBATTMID=th.NBATTMID where th.dtdate>=to_date('01-oct-2016','dd-mon-yyyy') and th.dtdate<to_date('01-nov-2016','dd-mon-yyyy') 
    and ch.NBSUBCOMPANY IN (select column_value from table (arr)) 
    and ((th.CHTRANSTYPE in ('2940', '2916', '2941', '2942', '2943', '2944', '2945', '2902', '2917', '2925') and th.NBBASEAMT < 0) or (th.CHTRANSTYPE in ('2922', '2923', '2926', '2950', '2951', '2952', '2953', '2954', '2955') and th.NBBASEAMT > 0) or (th.CHTRANSTYPE in ('1101', '1102', '1104', '1105', '1106', '1107', '1109') and th.BASEII_STATUS = 'C') or th.CHTRANSTYPE not in ('2940', '2916', '2941', '2942', '2943', '2944', '2945', '2902', '2917', '2925', '2922', '2923', '2926', '2950', '2951', '2952', '2953', '2954', '2955', '1101', '1102', '1104', '1105', '1106', '1107', '1109')); 
END; 

請建議我我如何可以通過這個。

+1

的可能的複製[陣列中()子句預言PLSQL](https://stackoverflow.com/questions/15515772/array-in-in-clause-oracle -plsql) – XING

+0

@XING謝謝你的回覆,但事情是不同的,我已經訪問過URL,他們沒有將值傳遞給IN(),他們只是像一個簡單的迭代器一樣一個一個地打印值。但我想將整個數組傳入IN()。 –

回答

0

您需要有一個解決方法。

SQL不支持以您想要的方式使用本地集合類型。

採樣過程下,這將有助於解決

CREATE OR REPLACE type TEST_TYPE 
IS 
    TABLE OF NUMBER; 

您需要這個工作在數據庫中創建一個類型。可以在包內創建,如果有的話。

DECLARE 
    TEST_VAR TEST_TYPE := TEST_TYPE(); 
BEGIN 
    TEST_VAR.extend(1); 
    TEST_VAR(TEST_VAR.last) := 222; 
    TEST_VAR.extend(1); 
    TEST_VAR(TEST_VAR.last) := 333; 

    FOR rec IN 
    (SELECT column_value 
    FROM TABLE(TEST_VAR) 
    ) 
    LOOP 
     dbms_output.put_line(rec.column_value); 

    END LOOP; 

END; 

輸出

222 
333 

這樣,您就可以使用select column_value from table(test_var)IN()條款。

此外,您不一定需要遵循extend(i)部分。你可以簡單地做下面還有

TEST_VAR TEST_TYPE := TEST_TYPE(222,333); 

有一個讀 - local collection types not allowed in SQL statements

0

可以使用MEMBER OF條款。如下所示:

由於@Sudipta提到你不能像使用PLSQL塊中的decalred集合那樣使用,你需要在PLSQL block之外聲明它。

CREATE OR REPLACE TYPE sc IS TABLE OF NUMBER; 

然後

DECLARE 
    -- TYPE sc IS TABLE OF NUMBER INDEX BY PLS_INTEGER; 

    arr sc:=sc(); 

    num number; 

BEGIN 
    arr.extend(4); 
    arr(1) := 0; 
    arr(2) := 1; 
    arr(3) := 2; 
    arr(4) := 3; 

    Select count(1) 
    into num 
    from employee 
    -- You can use either commented one or uncommented one. your choice. 
    --where employee_id in (select column_value from table(arr)); 
    where employee_id member of arr; 

END;