变量数组中的PL / SQL脚本子查询

时间:2015-12-02 00:22:44

标签: sql oracle plsql oracle11g

嘿伙计们,我试图做的是提取一个与主要帐户ID相关的帐户ID列表,而不加入表格,因为它的巨大然后使用该列表从数据表中提取相关信息我得到一个错误说它期待其他东西=第3行

数据表不包含主帐户ID account_lookup表在同一行IE

中具有帐户_id和主帐户ID
row ID | primary_account_id | account_id
 1     |          1         |     1
 2     |          1         |     2


declare
   acc_id number(10,0) := 5500704;
   p_acc_id number(10,0) := (select Primary_account_id from lookup_tbl where account_id = acc_id); 
   type array_t is table of number(10,0);
   array array_t := (select account_id from lookup_tbl where primary_account_id = p_acc_id);  

end;

select account_id, profit
from data_tbl where account_id in array_t

2 个答案:

答案 0 :(得分:1)

Hey i have modified the code snippet to elimintae the error you are facing. Please try and let me know for any issues.

DROP TYPE p_acc_tab;


-- Creating permament SQL object to eliminate the error occured
CREATE OR REPLACE TYPE 
p_acc_tab 
IS TABLE OF NUMBER; 


DECLARE          
acc_id NUMBER := 5500704;          
p_acc_id p_acc_tab;        
type array_tab        
IS          
TABLE OF NUMBER;          
array_t array_tab;        
BEGIN          
-- First bulk collect as you don want to use join as gigantic data present          
SELECT Primary_account_id           
BULK COLLECT          
INTO p_acc_id          
FROM lookup_tbl          
WHERE account_id = acc_id;                    

--Second bulk collect with EXISTS condition to check for the account_id for respective primary account_id          
SELECT account_id           
BULK COLLECT          
INTO array_t          
FROM lookup_tbl t_tab          
WHERE EXISTS            
(SELECT 1            
FROM TABLE(p_acc_id) tab            
WHERE tab.column_value = t_tab.Primary_account_id            
);        
END;

答案 1 :(得分:0)

未经测试但请尝试以下内容。

declare
   acc_id number(10,0) := 5500704;
   p_acc_id number(10,0); 

   type array_t is  table of number(10,0);
   my_array array_t;  

   cursor c_blah is
   select account_id, profit
    from data_tbl where account_id in array_t;

begin

  select primary_account_id 
  into p_acc_id
  from lookup_tbl 
  where account_id = acc_id;  

  select account_id 
  bulk collect into my_array
  from lookup_tbl 
  where primary_account_id = p_acc_id;

  for v_row in c_blah loop

     dbms_output.put_line(' account_id = ' || v_row.account_id || ' : ' || v_row.profit);      

  end loop;


end;