回 帖 发 新 帖 刷新版面

主题:ORA-01427: 单行子查询返回多个行

已创建表depositor 和 account2,分别有属性account2_number、customer_name以及account2_name、branch_name、balance。
在oracle中输入以下语句,目的是找出在Brooklyn的所有支行都有帐户的客户

select distinct D.customer_name
from depositor D
where (select branch_name
       from depositor T,account2 R
       where T.account2_number = R.account2_number
       and 
       D.customer_name = T.customer_name)
       =all
       (select branch_name
       from branch
       where branch_city = 'Brooklyn')
返回
Error report:
SQL Error: ORA-01427: 单行子查询返回多个行
01427. 00000 -  "single-row subquery returns more than one row"
请问为什么?
本人是新手,刚学数据库没几天。写这个的另一目的是为了看看=all的用法。因书上只是一笔带过,是否在这里出错了?

回复列表 (共1个回复)

沙发

错误出在:
(select branch_name
       from depositor T,account2 R
       where T.account2_number = R.account2_number
       and 
       D.customer_name = T.customer_name)
对应 D.customer_name 上式返回多行结果,“= ALL” 语法的左边不允许多值(Set)。以下是 ALL 的语法供你参考: ( ALL 在 Oracle 和 MS-SQL 中的用法相同)

Compares a scalar value with a single-column set of values. 
Syntax

scalar_expression { = | <> | != | > | >= | !> | < | <= | !< } ALL (subquery)

Arguments

scalar_expression

Is any valid Microsoft?SQL Server?expression.

{ = | <> | != | > | >= | !> | < | <= | !< }

Is a comparison operator.

subquery

Is a subquery that returns a result set of one column. The data type of the returned column must be the same data type as the data type of scalar_expression.
Is a restricted SELECT statement (the ORDER BY clause, the COMPUTE clause, and the INTO keyword are not allowed).

Return Types

Boolean

Result Value

Returns TRUE when the comparison specified is TRUE for all pairs (scalar_expression, x) where x is a value in the single-column set; otherwise returns FALSE.

我来回复

您尚未登录,请登录后再回复。点此登录或注册