Oracle WITH 子句不返回任何数据
我试图在 Oracle 中使用WITH 子句,但它没有返回任何数据。
这是我试图运行的查询...
with test as
(select count(*)
from my_table)
select *
from test;
当我运行此代码时,我得到了 my_table 中的记录计数
select count(*)
from my_table
,我在 Oracle 10g 上,所以查询应该可以工作
select * from v$version;
...
Oracle 数据库 10g 企业版版本 10.2.0.4.0 - 64bi
PL/SQL 版本 10.2.0.4.0 - 生产
核心 10.2.0.4.0 生产
适用于 Solaris 的 TNS:版本 10.2.0.4.0 - 生产
NLSRTL 版本 10.2.0.4.0 - 生产
可能是权限问题还是其他问题?
*编辑:*
我相信我的问题很清楚。使用WITH语句不会为我返回任何记录,即使WITH语句内的“select count(*) from my_table”语句工作正常,这会让我相信还有另一个我无法弄清楚的问题,因此这个问题:)
编辑2
好的,所以如果我尝试从SQL Server Management Studio的链接服务器执行查询,我会得到一些错误信息:
sg 7357,Level 16,State 2, 1号线 无法处理对象“测试为 (选择计数(*) 来自 v$ 版本) select * from test;"。链接服务器“MyServer”的 OLE DB 提供程序“MSDAORA”指示该对象没有列,或者当前用户对该对象没有权限。
I am trying to use a WITH clause in Oracle, but it is not returning any data.
This is the query I am trying to run...
with test as
(select count(*)
from my_table)
select *
from test;
When I run this code, I get back the count of the records in my_table
select count(*)
from my_table
I am on Oracle 10g so the query should work...
select * from v$version;
yields
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bi
PL/SQL Release 10.2.0.4.0 - Production
CORE 10.2.0.4.0 Production
TNS for Solaris: Version 10.2.0.4.0 - Production
NLSRTL Version 10.2.0.4.0 - Production
Could it a permissions issue or something?
*EDIT: *
I believe my question is clear. Using the WITH statement will not return any records for me, even though the "select count(*) from my_table" statement inside the WITH statement works correctly, which would lead me to believe that there is another issue that I am unable to figure out, hence this question :)
EDIT 2
OK, so if I try and execute the query from a linked server from SQL server management studio I get some error information back:
sg 7357, Level 16, State 2, Line 1
Cannot process the object "with test as
(select count(*)
from v$version)
select * from test;". The OLE DB provider "MSDAORA" for linked server "MyServer" indicates that either the object has no columns or the current user does not have permissions on that object.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
也许优化器正在具体化计数查询(愚蠢,我同意)。这是一次盲目的尝试,但你有这些特权吗?
授予您的用户查询重写;
授予您的用户创建物化视图;
Maybe the optimizer is materializing the count query (dumb, I agree). It's a shot in the dark but do you have these privileges?
grant query rewrite to youruser;
grant create materialized view to youruser;
尝试为聚合指定一个别名。
Try giving the aggregate an alias name.
以下内容对我来说效果很好(10gR2)
您使用什么客户端?
The following worked just fine for me (10gR2)
What client are you using?
这个问题令人困惑。您是说您正在还是没有从 my_table 获取计数?
您应该返回计数,因为这正是您在 with 子句中所要求的。
类似于这样写:
select *
from (从 my_table 中选择 count(*));
This question is confusing. Are you saying you are or are not getting back the count from my_table?
You should be getting back the count because that's exactly what you asked for in the with clause.
It's analogous to writing:
select *
from (select count(*) from my_table);
前几天,我公司的一些人遇到了这个问题 - 我们追踪到 PL/SQL 开发人员正在使用的 Oracle 客户端版本 [以及 OCI.dll] 版本。我们的一些开发 PC 上仍然安装了 Oracle 8(!)客户端以及更新的版本。
症状是,使用WITH子句编写的查询不仅不返回任何行,而且也不返回任何列!如果您手动设置应用程序以获取 Oracle 11 oci.dll,那么一切都会正常。
我认为发生的事情是 Oracle 8 早于WITH 子句(在 Oracle 9 中引入,并随后得到增强)。现在,大多数情况下您可以获得不同版本的 Oracle 客户端和服务器来相互通信。然而,因为客户端具有一定的“智能”,所以它应该半知道它正在向数据库提交什么样的操作,并且某种形式的 SQL 原始解析也是如此。由于它不将该命令识别为 SELECT,因此它将其视为某些未知命令(例如可能是 DDL 命令),并且不将其识别为返回结果集。如果为会话打开 SQL_TRACE,您可以看到 SQL 在服务器上得到解析和执行,但不会调用 FETCH。
最近,当我尝试在 Oracle 12 中使用允许内联函数定义的新WITH 语法时,我自己也遇到了类似的事情。如果您尝试使用基于 Oracle 11 胖客户端的应用程序(例如 PL/SQL Developer 或 SQL*Plus)的简单示例,则会出现错误。如果您使用 Oracle 12 客户端或不依赖客户端安装的瘦客户端应用程序,那么它就可以工作。
Some people at my company ran into this the other day - we traced it down to the Oracle client version [and thus the OCI.dll] version that was being picked up by PL/SQL developer. Some of our dev PCs had Oracle 8 (!) client installs still knocking around on them as well as more recent versions.
The symptom was that not only were queries written using a WITH clause returning no rows, they were returning no columns either! If you manually set the app to pick up the Oracle 11 oci.dll then it all worked.
I think what is going on is that Oracle 8 predates the WITH clause (introduced in Oracle 9, and enhanced subsequently). Now, mostly you can get different versions of the Oracle client and server to talk to one another. However because the client has a certain amount of 'intelligence', it is supposed to be semi-aware of what sort of operation it is submitting to the database, and so does some form of primitive parse of the SQL. As it doesn't recognize the command as a SELECT, it treats it as some unknown command [e.g. possibly a DDL command] and doesn't recognize it as returning a resultset. If you turn on SQL_TRACE for the session you can see the SQL gets PARSEd and EXECUTEd fine on the server, but that no calls to FETCH are made.
I had a similar thing myself recently when trying to use the new WITH syntax in Oracle 12 that allows an inline function definition. If you try simple examples using an Oracle 11 thick client-based application, such as PL/SQL developer or SQL*Plus, then you get an error. If you use an Oracle 12 client, or a thin-client application that doesn't rely a client-side install, then it works.