为什么我的Case语句在‘=’附近抛出错误、不正确的语法?

wljmcqd8  于 2022-10-03  发布在  其他
关注(0)|答案(2)|浏览(251)

我的Case语句引发此错误的原因:

  • 消息102,级别15,状态1,行2‘=’附近的语法不正确。*

代码:

Create PROCEDURE [dbo].[Chart_SelectSmsComplaintsValidity]

 @FromDate datetime,
 @ToDate datetime

AS
BEGIN

    Select COUNT(ReceivedMessages.ID) As ReceivedMessageID, 
    CASE ReceivedMessages.IsComplaint WHEN ReceivedMessages.IsComplaint=0 
    THEN 'Incomplete'
    WHEN ReceivedMessages.IsComplaint=1 THEN 'Complete'
    WHEN ReceivedMessages.IsComplaint=-3 THEN 'Inappropriate' END from ReceivedMessages 

    where ReceivedMessages.ReceivedDateTime >= @FromDate AND ReceivedMessages.ReceivedDateTime <= @ToDate
    Group By ReceivedMessages.IsComplaint

END
txu3uszq

txu3uszq1#

您组合了两种不同形式的CASE语句。您可以这样简化它,因为您在每种情况下都比较相同的表达式:

CASE ReceivedMessages.IsComplaint
  WHEN 0  THEN 'Incomplete'
  WHEN 1  THEN 'Complete'
  WHEN -3 THEN 'Inappropriate' 
END from ReceivedMessages

另一种选择是:

CASE
  WHEN ReceivedMessages.IsComplaint=0  THEN 'Incomplete'
  WHEN ReceivedMessages.IsComplaint=1  THEN 'Complete'
  WHEN ReceivedMessages.IsComplaint=-3 THEN 'Inappropriate' 
END
6l7fqoea

6l7fqoea2#

错误在此行中

CASE ReceivedMessages.IsComplaint WHEN ReceivedMessages.IsComplaint=0

应该是这样的

CASE WHEN ReceivedMessages.IsComplaint=0 THEN 'Incomplete'
WHEN ReceivedMessages.IsComplaint=1 THEN 'Complete'
WHEN ReceivedMessages.IsComplaint=-3 THEN 'Inappropriate' END

相关问题