-1

我想在 SQL Server 2008where的语句部分添加一个案例。select

SELECT * 
FROM CR_LOAN_DTL 
WHERE 
    LOAN_ID = 1
    AND REFERENCE_NO = CASE 
                          WHEN @i_loan_id = 0 THEN >=1 
                          CASE WHEN  @i_loan_id = 0 THEN REFERENCE_NO  >=1 and REFERENCE_NO  <=9999999999999 
                               ELSE  REFERENCE_NO = @i_loan_id 
                          END 

它给了我一个错误:

">" 附近的语法不正确

我怎样才能解决这个问题?

4

1 回答 1

2

您的CASE陈述不能返回>=1. 它需要返回一个值,而不是需要解析的逻辑语句。

一个选项可能是执行以下操作:

WHERE LOAN_ID=1 AND   
   ((@i_loan_id <> 0 AND REFERENCE_NO = @i_loan_id)
OR (@i_loan_id = 0 AND REFERENCE_NO BETWEEN 1 AND 9999999999999))
于 2012-12-10T07:57:18.827 回答