SQL 在 Oracle 中不使用 FROM 子句进行选择
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1881853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Select without a FROM clause in Oracle
提问by RRUZ
in SQL Serveris possible to execute a SELECT, without reference to a table; something like:
在SQL Server中可以执行 SELECT,而无需参考表;就像是:
Select 1.2 +3, 'my dummy string'
As Oracledoes not allow a SELECT without a FROM, I use the dual table for this type of operation; something like:
由于Oracle不允许没有 FROM 的 SELECT,因此我使用双表进行此类操作;就像是:
Select 1,2+3, 'my dummy string' FROM DUAL
There is a better way of doing this type of query? it is good practice to use the dual table?
有没有更好的方法来做这种类型的查询?使用双表是个好习惯吗?
回答by Quassnoi
No, in Oracle
there is no SELECT
without FROM
.
不,Oracle
有没有SELECT
没有FROM
。
Using the dual
table is a good practice.
使用dual
表格是一个很好的做法。
dual
is an in-memory table. If you don't select DUMMY
from it, it uses a special access path (FAST DUAL
) which requires no I/O
.
dual
是一个内存表。如果您不从中选择DUMMY
,它将使用一个特殊的访问路径 ( FAST DUAL
),它不需要I/O
.
Once upon a time, dual
had two records (hence the name) and was intended to serve as a dummy recordset to duplicate records being joined with.
曾几何时,dual
有两条记录(因此得名),旨在用作虚拟记录集以复制被加入的记录。
Now it has but one record, but you can still generate an arbitrary number of rows with it:
现在它只有一条记录,但您仍然可以用它生成任意数量的行:
SELECT level
FROM dual
CONNECT BY
level <= 100
MySQL
also supports dual
(as well as the fromless syntax).
MySQL
还支持dual
(以及 fromless 语法)。
回答by Vincent Malgrat
don't forget that most of the times you don't actually need to use SELECT.
不要忘记,大多数时候您实际上并不需要使用 SELECT。
Instead of:
代替:
SELECT sysdate INTO l_date FROM dual;
SELECT CASE WHEN i = j THEN 0 ELSE 1 END INTO l_foo FROM dual;
...
you can use
您可以使用
l_date := sysdate;
l_foo := CASE WHEN i = j THEN 0 ELSE 1 END;
...
回答by dcp
it is good practice to use the dual table
使用双表是个好习惯
Yes, the dual table is usually used for this exact purpose. It's pretty standard in Oracle when you have no table to select from.
是的,双表通常用于这个目的。当您没有可供选择的表时,这在 Oracle 中是非常标准的。
回答by tmeisenh
I think you gotta use dual. it is used when you need to run SQL that does not have a table name. I can't say I use it much other than in SQL scripts to echo out the date something is ran or something stupid like that.
我认为你必须使用双重。当您需要运行没有表名的 SQL 时使用它。我不能说我除了在 SQL 脚本中使用它来回显某些东西的运行日期或诸如此类的愚蠢东西之外,我不能说我用它太多了。
回答by sleske
Yes, the dual table is the usual way to do this in Oracle. As a matter of fact, it was introduced just for this.
是的,双表是在 Oracle 中执行此操作的常用方法。事实上,它就是为此而引入的。
The main advantage of DUAL is that the optimizer in Oracle knows it is special, so queries using it can be faster than if you used a single-row table you made yourself. Other than that, there's nothing special about it.
DUAL 的主要优点是 Oracle 中的优化器知道它的特殊性,因此使用它的查询比使用自己制作的单行表要快。除此之外,没有什么特别之处。
回答by DCookie
Actually, SQL Server's implementation is non-standard. The SQL-92 Standard(Section 7.9) requires a FROM clause in a SELECT statement. DUAL is Oracle's way of providing a table to select from to get a scalar row.
实际上,SQL Server 的实现是非标准的。在SQL-92标准(第7.9节)需要在SELECT语句中FROM子句。DUAL 是 Oracle 提供的表以从中进行选择以获取标量行的方式。