Sql server 在一个查询中运行equals和isnull语句

Sql server 在一个查询中运行equals和isnull语句,sql-server,tsql,Sql Server,Tsql,我有这个疑问 DECLARE @LastDesignKey INT = (SELECT TOP 1 MAX([DesignKey]) FROM [Design] AS [D] INNER JOIN [DesignGroup] AS [DG] ON [D].[DesignGroupId] = [DG].

我有这个疑问

 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,因此我的结果是错误的,因为要检查null,您不能使用:

错误使用:

 WHERE [D].[ParentDesignKey] = NULL
正确使用

  WHERE [D].[ParentDesignKey] 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

您可以添加显式逻辑:

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)

请注意,如果您依赖索引来获得性能,这将破坏性能。如果是这种情况,您可能希望使用非空的默认值。

您可以将或添加到可空变量中

   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
其中([D].[ParentDesignKey]=@ParentDesignKey或@ParentDesignKey为空)