条件空选择

时间:2018-07-26 04:26:30

标签: sql-server

使用NULL检查有条件地查询值的正确语法是什么?

DECLARE @TargetDialogHandle UNIQUEIDENTIFIER
DECLARE @EventMessage XML
DECLARE @EventMessageTypeName sysname

WAITFOR(
    RECEIVE @TargetDialogHandle = conversation_handle,
    @EventMessage = CONVERT(XML, message_body),
    @EventMessageTypeName = message_type_name FROM dbo.EventNotificationQueue ), TIMEOUT 1000

SELECT 
    @EventMessage.value('(/EVENT_INSTANCE/TSQLCommand/CommandText)[1]', 'nvarchar(max)') AS TSQLCommand, 
    CASE @EventMessage.value('(/EVENT_INSTANCE/TargetObjectName)[1]', 'varchar(128)') 
       WHEN NULL @EventMessage.value('(/EVENT_INSTANCE/ObjectName)[1]', 'varchar(128)') 
       ELSE @EventMessage.value('(/EVENT_INSTANCE/TargetObjectName)[1]', 'varchar(128)') 
    END AS TableName

这会引发错误:

  

第15级州立1行9的消息102
  “ @EventMessage”附近的语法不正确。

以下声明:

DECLARE @TargetDialogHandle UNIQUEIDENTIFIER
DECLARE @EventMessage XML
DECLARE @EventMessageTypeName sysname

WAITFOR(
    RECEIVE @TargetDialogHandle = conversation_handle,
    @EventMessage = CONVERT(XML, message_body),
    @EventMessageTypeName = message_type_name FROM dbo.EventNotificationQueue ), TIMEOUT 1000

SELECT 
    @EventMessage.value('(/EVENT_INSTANCE/TSQLCommand/CommandText)[1]', 'nvarchar(max)') AS TSQLCommand, 
    IIF( @EventMessage.value('(/EVENT_INSTANCE/TargetObjectName)[1]', 'varchar(128)') IS NULL, @EventMessage.value('(/EVENT_INSTANCE/ObjectName)[1]', 'varchar(128)'), @EventMessage.value('(/EVENT_INSTANCE/TargetObjectName)[1]', 'varchar(128)')) AS TableName

返回

  

第156条消息,第15级,状态1,第9行
  关键字“ IS”附近的语法不正确。

2 个答案:

答案 0 :(得分:2)

CASE表达式有两种样式:

CASE (column name or SQL variable)
   WHEN (value 1) 
      THEN (return value 1)
   WHEN (value 2) 
      THEN (return value 2)       
   ELSE (return else value)
END

这将检查CASE之后的列或变量名是否等于给定值之一,并在每种情况下返回某些值。

但是,如果您无法检查是否相等-使用NULL,您永远不会! -您需要使用其他样式:

CASE 
   WHEN (expression 1) 
      THEN (return value 1)
   WHEN (expression 2) 
      THEN (return value 2)       
   ELSE (return else value)
END

因此,您在这里检查公式化表达式(可以使用 other 而不是相等检查(例如,检查> =,或使用BETWEEN进行范围检查等)。

对于您来说,由于要检查NULL,因此必须使用方法2:

SELECT 
    @EventMessage.value('(/EVENT_INSTANCE/TSQLCommand/CommandText)[1]', 'nvarchar(max)') AS TSQLCommand, 
    CASE 
       -- check if your value IS NULL
       WHEN @EventMessage.value(.....) IS NULL 
          THEN @EventMessage.value('(/EVENT_INSTANCE/ObjectName)[1]', 'varchar(128)') 
       ELSE @EventMessage.value('(/EVENT_INSTANCE/TargetObjectName)[1]', 'varchar(128)') 
    END AS TableName

答案 1 :(得分:0)

您不能只使用COALESCE吗?

DECLARE @TargetDialogHandle UNIQUEIDENTIFIER
DECLARE @EventMessage XML
DECLARE @EventMessageTypeName sysname

WAITFOR(
    RECEIVE @TargetDialogHandle = conversation_handle,
    @EventMessage = CONVERT(XML, message_body),
    @EventMessageTypeName = message_type_name FROM dbo.EventNotificationQueue ), TIMEOUT 1000

SELECT 
    @EventMessage.value('(/EVENT_INSTANCE/TSQLCommand/CommandText)[1]', 'nvarchar(max)') AS TSQLCommand, 
    COALESCE(@EventMessage.value('(/EVENT_INSTANCE/TargetObjectName)[1]', 'varchar(128)'), @EventMessage.value('(/EVENT_INSTANCE/ObjectName)[1]', 'varchar(128)')) AS TableName