子查询返回了1个以上的值错误

时间:2019-06-17 10:30:38

标签: sql-server

SELECT 
    CASE CR.claim_status 
       WHEN 'RC' 
          THEN (SELECT ins_clause_no 
                FROM tbl_rej_code_master RCM 
                WHERE CR.s_no IN (SELECT s_no 
                                  FROM claim_rejection_code_trans CRCT 
                                  WHERE CRCT.irrmp_sr_no = RCM.rej_srno)) 
    END 
FROM   
    tblclaimregistration CR 

SELECT 
    CASE CR.claim_status 
       WHEN 'RC' THEN (SELECT clause_no 
                       FROM claim_rejection_code_trans CRCT 
                       WHERE CR.s_no = CRCT.s_no) 
    END AS Clause_No 
FROM
    tblclaimregistration CR; 

运行此代码时出现错误

  

子查询返回了多个值。当   子查询遵循=,!=,<,<=,>,> =,或当子查询用作   表达式。

请帮助

1 个答案:

答案 0 :(得分:1)

您的case子查询语句返回多个值。

您必须仅返回一个值,因为此子查询是在CR表的每一行执行的。

您可以在每个子查询中使用“ TOP 1”。

例如:

SELECT 
CASE CR.claim_status 
   WHEN 'RC' 
      THEN (SELECT TOP 1 ins_clause_no 
            FROM tbl_rej_code_master RCM 
            WHERE CR.s_no IN (SELECT s_no 
                              FROM claim_rejection_code_trans CRCT 
                              WHERE CRCT.irrmp_sr_no = RCM.rej_srno ORDER BY <Your Column>)) 
END 
FROM   
tblclaimregistration CR 

SELECT 
CASE CR.claim_status 
   WHEN 'RC' THEN (SELECT TOP 1 clause_no 
                   FROM claim_rejection_code_trans CRCT 
                   WHERE CR.s_no = CRCT.s_no ORDER BY <Your Column>) 
END AS Clause_No 
FROM
tblclaimregistration CR;