16

使用以下样式将多行插入表中时:

insert all

into ghazal_current (GhazalName,Rating) values('Ajab Apna Haal Hota Jo Visaal-e-Yaar Hota',5)
into ghazal_current (GhazalName,Rating) values('Apne Hothon Par Sajana Chahta Hun',4)
into ghazal_current (GhazalName,Rating) values('Shaam Se Aankh Mein Nami Si Hai',4)
into ghazal_current (GhazalName,Rating) values('Tumhe Yaad Ho Ke Na Yaad Ho',3)

select 1 from dual;

声明select 1 from dual是什么意思?这是为了什么?

4

3 回答 3

13

DUAL is a built-in table, useful because it is guaranteed to return only one row. This means DUAL may be used to get pseudo-columns such as user or sysdate, the results of calculations and the like. The owner of DUAL is SYS but it can be accessed by every user. DUAL is well-covered in the documentation. Find out more.

In your case, SELECT 1 FROM DUAL; will simply returns 1. You need it because the INSERT ALL syntax demands a SELECT clause but you are not querying the input values from a table.

于 2013-03-13T04:40:06.773 回答
3

简要重新介绍一排表

一些 SQL 数据库要求所有值都来自一个表或类似表的对象,而其他 SQL 数据库则允许查询从nihilo构造值:

-- MySQL, sqlite, PostgreSQL, HSQLdb, and many others permit
-- a "naked" select:
SELECT 1;

-- Others *require* a FROM target, like Oracle.
SELECT 1 FROM DUAL;

-- ...and Firebird/Interbase:
SELECT 1 FROM RDB$DATABASE;

-- ...and DB2:
SELECT 1 FROM SYSIBM.SYSDUMMY1;

这里 DUAL 的基数很重要。如果它有不止一行,那么您的结果集将有不止一行。例如,当你SELECT 1 FROM A_Table_With_Ten_Rows?

为什么在这里使用 DUAL

SQL 构造VALUES (<row-value-expression>)是一个行值构造函数。 VALUES (1, 2, 3)就像“创建”一行值一样SELECT 1, 2, 3

当然,Oracle 要求这些值来自某个地方。

作为演示,不要在 INSERT ALL 的末尾从 DUAL 中选择,而是尝试一个有N行的表,您会看到每一VALUES()行都插入了N次。

于 2013-03-13T13:18:03.793 回答
-1

有一些关于在查询中使用对偶的示例:

   select sysdate from dual  /--it returns date of system
    SELECT chr(223) FROM dual /--it returns character of Asciهi code
    select  my_sequence.nextval from dual; /-- It returns the next value of a sequence
    select to_char(sysdate,'yyyy/mm/dd','nls_calendar=persian')from dual 
    /--returns persian date of system
于 2013-03-13T05:06:48.623 回答