简体   繁体   中英

Subquery returned more than 1 value. This is not permitted when the subquery follows>= or when the subquery is used as an expression

Subquery returned more than 1 value error. How to solve this?

SELECT top 1 address
FROM tblAdr A
WHERE A.pkey=
  (SELECT b.pkey FROM tblMachine b WHERE b.ADDRESS_PKEY IS NULL
  )

The subquery (the quantity in parentheses) is returning more than one pkey value, which isn't allowed because a single scalar value is required. If you are content with checking whether a pkey in tblAdr matches any of the values in the subquery, then you can use WHERE A.pkey IN (...) as follows:

SELECT TOP 1 address
FROM tblAdr A
WHERE A.pkey IN (SELECT b.pkey FROM tblMachine b WHERE b.ADDRESS_PKEY IS NULL)

Change the = to an IN

select top 1 address from tblAdr A where A.pkey in (select b.pkey from tblMachine b where b.ADDRESS_PKEY is null)

This should fix the error, however the logic of your query is likely flawed. You are after one row only ( select top 1 ), however you don't define what row should be chosen first (no order by clause).

Use IN instead of = as your subquery returns multiple matching rows.

SELECT top 1 address
FROM tblAdr A
WHERE A.pkey IN
  (SELECT b.pkey FROM tblMachine b WHERE b.ADDRESS_PKEY IS NULL
  );

Also, always use top queries with order by.

此子查询“从tblMachine b中选择b.pkey,其中b.ADDRESS_PKEY为null”返回多个值,您无法将其与“ =”运算符进行比较,但您需要此try in子句

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM