【问题标题】:How to store a result set of a ORACLE sql query?如何存储 ORACLE sql 查询的结果集?
【发布时间】:2016-08-03 11:28:43
【问题描述】:

示例:有一个表“ID_NAME”,其中有一列“ID”,其中包含 2000 个条目,例如 1,2,3.. 2000。 我有一个问题

从 ID_NAME 中选择 id,其中 id

> 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,这样我就不必在 count(*) 子查询中一次又一次地运行它

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

【问题讨论】:

    标签: oracle plsql oracle11g


    【解决方案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;
    

    【讨论】:

      【解决方案2】:

      您正在构建一个 CTE,所以它是这样的:对于每个 select 语句,您需要加入 CTE。

         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;
      

      【讨论】:

        【解决方案3】:

        在 SQL 中,with 子句或公用表表达式是查询的一部分。它没有设置程序变量。

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

        您不能在其他查询中引用xyz - 它只是单个查询中的一个子句。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-05-29
          • 2023-02-23
          • 2012-06-12
          • 1970-01-01
          • 1970-01-01
          • 2020-03-27
          • 1970-01-01
          • 2022-10-13
          相关资源
          最近更新 更多