示例:有一张表" ID_NAME"一列" ID"它有2000个条目,如1,2,3 .. 2000。 我有查询
从ID_NAME中选择ID,其中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,这样我就不必在count(*)子查询中再次运行它
with set1 as (select id from ID_NAME where id < 1001)
答案 0 :(得分: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;
答案 1 :(得分:0)
您正在构建一个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;
答案 2 :(得分:0)
在SQL中,with
子句或公用表表达式是查询的一部分。它没有设置程序变量。
with xyz as (select blah from blahblah where something = somethingelse)
select blah from xyz;
您无法在其他查询中引用xyz
- 它只是单个查询中的一个子句。