3

我正在尝试在查询中使用 WITH 子句,但不断收到消息

ORA-00942: 表或视图不存在

我试图在这里创建一个简单的查询作为示例:

WITH
test AS
(
SELECT COUNT(Customer_ID) FROM Customer
)
SELECT * FROM test;

但即使这样也不起作用,它只是给出了信息:

从测试中选择*;2 3 4 5 6 SQL>
SELECT * FROM test
* 第 1 行出现错误:
ORA-00942:表或视图不存在

我以前从未使用过 WITH 子句,这里有什么简单的东西吗?我正在使用 Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod 任何建议将不胜感激。谢谢。

4

5 回答 5

5

我相信您的脚本中 WITH 子句和 SELECT 之间有一个空行:

SQL> WITH
  2  test AS
  3  (
  4  SELECT COUNT(Customer_ID) FROM Customer
  5  )
  6  
SQL> select * from test;
select * from test
              *
ERROR at line 1:
ORA-00942: table or view does not exist

这与您将错误报告为“第 1 行”和 SQL“select * from test”的事实一致,而该 SQL 应位于“第 6 行”。

于 2009-04-29T13:55:37.453 回答
2

您的示例有效 - 刚刚尝试过(SQL*Plus 日志如下):

SQL> create table customer
  2  (customer_id number);
Table created.
SQL> with 
  2  test as 
  3  (select count(customer_id)
  4  from customer
  5  )
  6  select * from test;
COUNT(CUSTOMER_ID)
------------------
         0

您确定您对客户表具有特权还是不需要模式限定符(如果它在不同的模式中)?

于 2009-04-29T11:25:32.150 回答
1

您是否尝试过添加到脚本的顶部

SET SQLBLANKLINES ON;
于 2021-03-23T22:13:53.047 回答
0

看看这个例子

编辑

一个非常基本的示例:

create table emp (emp_id number, dept_id number);
insert into emp values (1,20);
insert into emp values (2,20);
insert into emp values (3,20);
insert into emp values (4,30);

with
emp_counter  as (select count(distinct emp_id) from emp),
dept_counter as (select count(distinct dept_id) from emp)
select * from emp_counter, dept_counter;

COUNT(DISTINCTEMP_ID) COUNT(DISTINCTDEPT_ID)
--------------------- ----------------------
                    4                      2
于 2009-04-29T09:32:51.093 回答
0

您收到的错误字面意思是视图表在您当前的架构中不存在,并且您的架构没有可见的同义词。例如,如果我以 greg 身份登录,并且表在 bob 中,那么我应该将表引用为 bob.test。

SELECT * FROM bob.test

至于 WITH 语法,我不熟悉,但其他答案都很好。

于 2009-04-29T14:01:00.170 回答