2016-08-03 10 views
0

例:1,2,3のように2000個のエントリを持っている一つの列「ID」とテーブル「ID_NAME」がある... 2000年 私はID_NAMEどこIDからの問合せORACLEのSQLクエリの結果セットを格納する方法?

選択IDを持っていました<1001;

> Result : 1 2 3 4 . .1000 

私のPL SQLブロックは、次のようになります

SET SERVEROUTPUT ON; 
    declare 
    var1 number; 
    var2 number; 
    var3 number; 
    var4 number; 
    var5 number; 
    var6 number; 
    var7 number; 
    var8 number; 
    var9 number; 
    var10 number; 
    begin 
    with set1 as (select id from ID_NAME where id < 1001) 
    select count(*) into var1 from table1 where id in (select * from set1); 
    select count(*) into var2 from table2 where id in (select * from set1); 
    select count(*) into var3 from table3 where id in (select * from set1); 
    select count(*) into var4 from table4 where id in (select * from set1); 
    select count(*) into var5 from table5 where id in (select * from set1); 
    select count(*) into var6 from table6 where id in (select * from set1); 
    select count(*) into var7 from table7 where id in (select * from set1); 
    select count(*) into var8 from table8 where id in (select * from set1); 
    select count(*) into var9 from table9 where id in (select * from set1); 
    select count(*) into var10 from table10 where id in (select * from set1); 
DBMS_OUTPUT.PUT_LINE('var1,var2,var3,var4,var5,var6,var7,var8,var9,var10'); 
DBMS_OUTPUT.PUT_LINE(var1||','||var2||','||var3||','||var4||','||var5||','||var6||','||var7||','||var8||','||var9||','||var10); 
end; 

が、私は

PL/SQL取得しています。ORA-00942:表またはビューが

存在しません。

私のSQL開発者です。

私は数に何度も何度もそれを実行する必要がないように私のクエリの下からSET1を使用したい(*)サブ

with set1 as (select id from ID_NAME where id < 1001) 

答えて

0

を照会し、それが行くようにするには、CTEを構築しています次のようにします。CTEに参加する必要がある各selectステートメントごとに。

declare 
    var1 number; 
    var2 number; 
    var3 number; 
    var4 number; 
    var5 number; 
    var6 number; 
    var7 number; 
    var8 number; 
    var9 number; 
    var10 number; 
    begin 
    with set1 as (select emp_id from employee where emp_id < 1001) 
    select count(*) into var1 from employee 
    where emp_id in (select eno from emp_sal); 

    with set1 as (select emp_id from employee where emp_id < 1001) 
    select count(*) into var2 from employee 
    where emp_id in (select eno from emp_sal); 


    . 
    . 
    . 
    . 
    and so on 
-- select count(*) into var2 from table2 where id in (select * from set1); 
-- select count(*) into var3 from table3 where id in (select * from set1); 
-- select count(*) into var4 from table4 where id in (select * from set1); 
-- select count(*) into var5 from table5 where id in (select * from set1); 
-- select count(*) into var6 from table6 where id in (select * from set1); 
-- select count(*) into var7 from table7 where id in (select * from set1); 
-- select count(*) into var8 from table8 where id in (select * from set1); 
-- select count(*) into var9 from table9 where id in (select * from set1); 
-- select count(*) into var10 from table10 where id in (select * from set1); 
DBMS_OUTPUT.PUT_LINE('var1,var2,var3,var4,var5,var6,var7,var8,var9,var10'); 
DBMS_OUTPUT.PUT_LINE(var1||','||var2||','||var3||','||var4||','||var5||','||var6||','||var7||','||var8||','||var9||','||var10); 
end; 
0

SQLでは、with句またはCommon Table Expressionがクエリの一部です。プログラム変数は設定しません。

with xyz as (select blah from blahblah where something = somethingelse) 
select blah from xyz; 

あなたは他のクエリでxyzを参照することができません - それは、単一のクエリ内でだけ句です。

1
SET SERVEROUTPUT ON; 
    declare 
    var1 number; 
    var2 number; 
    var3 number; 
    var4 number; 
    var5 number; 
    var6 number; 
    var7 number; 
    var8 number; 
    var9 number; 
    var10 number; 
    begin 
    with set1 as (select id from ID_NAME where id < 1001) 
    select 
    (select count(*) from table1 where id in (select * from set1)), 
    (select count(*) from table2 where id in (select * from set1)), 
    .............. 
    (select count(*) from table9 where id in (select * from set1)), 
    (select count(*) from table10 where id in (select * from set1)) 
    into var1,var2,.....,var9,var10 
    from dual; 
DBMS_OUTPUT.PUT_LINE('var1,var2,var3,var4,var5,var6,var7,var8,var9,var10'); 
DBMS_OUTPUT.PUT_LINE(var1||','||var2||','||var3||','||var4||','||var5||','||var6||','||var7||','||var8||','||var9||','||var10); 
end; 
関連する問題