在一个查询中运行equals和isull语句

时间:2019-06-17 20:30:46

标签: sql-server tsql

我有这个查询

 DECLARE @LastDesignKey INT = (SELECT TOP 1 
                                   MAX([DesignKey])
                               FROM [Design] AS [D]
                               INNER JOIN [DesignGroup] AS [DG] ON [D].[DesignGroupId] = [DG].[DesignGroupId]
                               INNER JOIN [DesignType] AS [DT] ON [D].[DesignTypeGuid] = [DT].[DesignTypeGuid]
                               WHERE [D].[ParentDesignKey] = @ParentDesignKey 
                                 AND [DT].[Abbreviation] = @DesignTypeName
                                 AND [DG].[ProjectKey] = @ProjectKey)

为了对其进行测试,我添加了静态变量:

 DECLARE @ParentDesignKey INT = NULL, 
         @DesignTypeName VARCHAR(266) = 'BD',
         @ProjectKey INT = 4395


DECLARE @LastDesignKey INT = (SELECT TOP 1 MAX([DesignKey])
                              FROM [Design] AS [D]
                              INNER JOIN [DesignGroup] AS [DG] ON [D].[DesignGroupId] = [DG].[DesignGroupId]
                              INNER JOIN [DesignType] AS [DT] ON [D].[DesignTypeGuid] = [DT].[DesignTypeGuid]
                              WHERE [D].[ParentDesignKey] = @ParentDesignKey
                                AND [DT].[Abbreviation] = @DesignTypeName
                                AND [DG].[ProjectKey] = @ProjectKey)

  SELECT @LastDesignKey

如您在我的where子句中所见,我有WHERE [D].[ParentDesignKey] = @ParentDesignKey,但有时@ParentDesignKey变量   可以为null,所以我的resut是错误的,因为要检查null您不能使用:

错误使用:

 WHERE [D].[ParentDesignKey] = NULL

正确使用

  WHERE [D].[ParentDesignKey] IS NULL

我可以在查询中做什么以支持IS NULL和=?问候

3 个答案:

答案 0 :(得分:0)

您可以使用OR ... IS NULL条件:

WHERE ([D].[ParentDesignKey] = @ParentDesignKey OR  @ParentDesignKey IS NULL)
  AND ([DT].[Abbreviation] = @DesignTypeName OR @DesignTypeName IS NULL)
  AND ([DG].[ProjectKey] = @ProjectKey OR @ProjectKey IS NULL)
OPTION(RECOMPILE) -- to force using actual values instead of parameters

答案 1 :(得分:0)

您可以添加显式逻辑:

WHERE ([D].[ParentDesignKey] = @ParentDesignKey OR [D].[ParentDesignKey] IS NULL AND @ParentDesignKey IS NULL) OR
      ([DT].[Abbreviation] = @DesignTypeName OR [DT].[Abbreviation] IS NULL AND @DesignTypeName IS NULL) OR
      ([DG].[ProjectKey] = @ProjectKey OR [DG].[ProjectKey] IS NULL AND @ProjectKey IS NULL)

请注意,如果您依赖性能索引,这会降低性能。如果是这种情况,您可能要使用非NULL的默认值。

答案 2 :(得分:0)

您可以添加或将其设置为可空变量

   DECLARE @ParentDesignKey INT = NULL, 
             @DesignTypeName VARCHAR(266) = 'BD',
             @ProjectKey INT = 4395


    DECLARE @LastDesignKey INT = (SELECT TOP 1 MAX([DesignKey])
                                  FROM [Design] AS [D]
                                  INNER JOIN [DesignGroup] AS [DG] ON [D].[DesignGroupId] = [DG].[DesignGroupId]
                                  INNER JOIN [DesignType] AS [DT] ON [D].[DesignTypeGuid] = [DT].[DesignTypeGuid]
                                  WHERE (      [D].[ParentDesignKey] = @ParentDesignKey 
                                          or ( [D].[ParentDesignKey] is null  and @ParentDesignKey is null )
                                         )
                                    AND [DT].[Abbreviation] = @DesignTypeName
                                    AND [DG].[ProjectKey] = @ProjectKey)

      SELECT @LastDesignKey