查询正在返回"单行子查询返回多行"

时间:2018-05-09 09:15:32

标签: sql

我不确定为什么以下查询会给出"单行子查询返回多行"如果我遗失任何东西,请告诉我。

CASE
  WHEN s.servprov_gid = 'IFFCO.CAR-60041'
  THEN
    (SELECT E.EQUIPMENT_NUMBER
    FROM S_EQUIPMENT SE,
      EQUIPMENT E,
      SHIPMENT_S_EQUIPMENT_JOIN SSEJ,
      SHIPMENT S
    WHERE SSEJ.SHIPMENT_GID = 'IFFCO/LOGISTICS.L171203007'
    AND SE.S_EQUIPMENT_GID  = SSEJ.S_EQUIPMENT_GID
    AND E.EQUIPMENT_GID     = SE.EQUIPMENT_GID
    AND SSEJ.SHIPMENT_GID   = S.SHIPMENT_GID
    )
  ELSE
    (SELECT MIN(se.equipment_number)
    FROM shipment_s_equipment_join ssej,
      s_equipment se
    WHERE ssej.shipment_gid  = 'IFFCO/LOGISTICS.L171203007'
    AND ssej.s_equipment_gid = se.s_equipment_gid
    AND se.equipment_number IS NOT NULL
    )
END 

1 个答案:

答案 0 :(得分:0)

错误消息非常清楚地告知,在关键字"然后"之后从子查询返回多行。您可以添加" Top 1"在SQL Serve4的第一个SELECT关键字之后。 或者以"和rownum = 1"结束子查询结束对于Oracle。以下内容适用于SQL Server。

CASE
  WHEN s.servprov_gid = 'IFFCO.CAR-60041'
  THEN
    (SELECT TOP 1 E.EQUIPMENT_NUMBER
    FROM S_EQUIPMENT SE,
      EQUIPMENT E,
      SHIPMENT_S_EQUIPMENT_JOIN SSEJ,
      SHIPMENT S
    WHERE SSEJ.SHIPMENT_GID = 'IFFCO/LOGISTICS.L171203007'
    AND SE.S_EQUIPMENT_GID  = SSEJ.S_EQUIPMENT_GID
    AND E.EQUIPMENT_GID     = SE.EQUIPMENT_GID
    AND SSEJ.SHIPMENT_GID   = S.SHIPMENT_GID
    )
  ELSE
    (SELECT MIN(se.equipment_number)
    FROM shipment_s_equipment_join ssej,
      s_equipment se
    WHERE ssej.shipment_gid  = 'IFFCO/LOGISTICS.L171203007'
    AND ssej.s_equipment_gid = se.s_equipment_gid
    AND se.equipment_number IS NOT NULL
    )
END