在ID字段

时间:2015-12-30 12:33:17

标签: sql oracle plsql oracle11g ora-01427

我在下面的查询中有一个问题,主要是 SELECT 的值 ENTITY_ID 无法检索,因为我正在使用LIKE,我得到的结果不止一个。 我怎么能克服这个? 如果我放置一个内部SELECT,如:

    (
            SELECT
                SEC_NN_SERVER_T.SRV_ID
            FROM
                SEC_NN_SERVER_T
            WHERE
                UPPER(SEC_NN_SERVER_T.SRV_NAME) LIKE UPPER('%gen%') )       AS ENTITY_ID,

我得到同样的错误:

“ORA-01427:单行子查询返回多行”。

他是完整的查询:

SELECT DISTINCT
SEC_USER.USR_ID         AS USR_ID,
SEC_USER.USR_FIRST_NAME AS USR_FIRST_NAME,
SEC_USER.USR_LAST_NAME  AS USR_LAST_NAME,
SEC_USER_PRIV.ROLE_ID   AS SYSTEM_ROLE_ID,
21                      AS ENTITY_TYP_CODE,
(
    SELECT
        SEC_NN_SERVER_T.SRV_ID
    FROM
        SEC_NN_SERVER_T
    WHERE
        UPPER(SEC_NN_SERVER_T.SRV_NAME) LIKE UPPER('%gen%') ) AS ENTITY_ID
FROM
    SEC_USER_PRIV,
    SEC_USER
WHERE
    SEC_USER.USR_ID = SEC_USER_PRIV.USR_ID
AND ((
            SEC_USER_PRIV.ENTITY_TYP_CODE = 21
        AND SEC_USER_PRIV.ENTITY_ID IN (
            (
                SELECT
                    SERVER_T.SRV_ID
                FROM
                    SERVER_T
                WHERE
                    UPPER(SERVER_T.SRV_NAME) LIKE UPPER('%gen%') ))))

请提前协助谢谢。

4 个答案:

答案 0 :(得分:1)

您不理解错误消息的哪一部分?您的子查询返回多行。您可以使用rownum或聚合:

来修复错误
(SELECT MAX(SEC_NN_SERVER_T.SRV_ID)
 FROM SEC_NN_SERVER_T
 WHERE UPPER(SEC_NN_SERVER_T.SRV_NAME) LIKE UPPER('%gen%')
) as ENTITY_ID

或者也许:

(SELECT LISTAGG(SEC_NN_SERVER_T.SRV_ID, ', ') WITHIN GROUP (ORDER BY SEC_NN_SERVER_T.SRV_ID)
 FROM SEC_NN_SERVER_T
 WHERE UPPER(SEC_NN_SERVER_T.SRV_NAME) LIKE UPPER('%gen%')
) as ENTITY_IDS

然而,这可能不是你真正想要的。您需要检查您正在做的事情的逻辑。

答案 1 :(得分:1)

通过从select中的where子句重复子查询,可以销毁SEC_USER_PRIV.ENTITY_ID与子查询之间的耦合。 SELECT部分​​中的子查询应始终只返回一个值,因为select构造要返回的行。我希望通过简单地包含SEC_USER_PRIV.ENTITY_ID而不是子查询来解决您的问题。

答案 2 :(得分:0)

您可以尝试以下查询

 public class ServiceNotFoundException : HttpResponseException
    {
        public ServiceNotFoundException() : base(HttpStatusCode.NotFound)
        {

        }
    }

答案 3 :(得分:0)

查看上述答案的评论,并猜测不清楚你想做什么,你可能想调查游标。以下是一个通用示例。

Declare

  cursor c_test is 
  SELECT SEC_NN_SERVER_T.SRV_ID
  FROM   SEC_NN_SERVER_T
  WHERE  UPPER(SEC_NN_SERVER_T.SRV_NAME) LIKE UPPER('%gen%') ) AS ENTITY_ID

begin

  for v_row in c_test loop

     your logic for each ID here.

  end loop;

end;