IF ... ELSE条件总是返回ELSE条件

问题描述 投票:0回答:1

我有一个像这样的简单查询:

@DesignKey [INT] = NULL
AS
        BEGIN
            -- SET NOCOUNT ON added to prevent extra result sets from
            -- interfering with SELECT statements.
            SET NOCOUNT ON;

            -- Insert statements for procedure here
            IF(@DesignKey != NULL)
            BEGIN
                 SELECT
                     [P].[LegacyKey] AS [Job No]
                   , [TT].[Name] AS [Task]
                   , CONCAT([E].[FirstName] , ' ' , [E].[MaidenName]) AS [Technician]
                    FROM [Task] AS [T]
                        LEFT JOIN [TaskAssignation] AS [TA] ON [T].[TaskAssignationId] = [TA].[TaskAssignationId]
                        LEFT JOIN [Employee] AS [E] ON [TA].[EmpKey] = [E].[EmpKey]
                        INNER JOIN [TaskType] AS [TT] ON [T].[TaskTypeId] = [TT].[TaskTypeId]
                        INNER JOIN [Design] AS [D] ON [T].[DesignKey] = [D].[DesignKey]
                        INNER JOIN [ProjectDesign] AS [PD] ON [D].[DesignKey] = [PD].[DesignKey]
                        INNER JOIN [Project] AS [P] ON [PD].[ProjectKey] = [P].[ProjectKey]
                    WHERE [E].[EmpKey] IS NOT NULL
                         AND [T].[DesignKey] = @DesignKey
            END
               ELSE
            BEGIN
                             SELECT
                    [P].[LegacyKey] AS [Job No]
                   , [TT].[Name] AS [Task]
                   , CONCAT([E].[FirstName] , ' ' , [E].[MaidenName]) AS [Technician]
                    FROM [Task] AS [T]
                        LEFT JOIN [TaskAssignation] AS [TA] ON [T].[TaskAssignationId] = [TA].[TaskAssignationId]
                        LEFT JOIN [Employee] AS [E] ON [TA].[EmpKey] = [E].[EmpKey]
                        INNER JOIN [TaskType] AS [TT] ON [T].[TaskTypeId] = [TT].[TaskTypeId]
                        INNER JOIN [Design] AS [D] ON [T].[DesignKey] = [D].[DesignKey]
                        INNER JOIN [ProjectDesign] AS [PD] ON [D].[DesignKey] = [PD].[DesignKey]
                        INNER JOIN [Project] AS [P] ON [PD].[ProjectKey] = [P].[ProjectKey]
                    WHERE [E].[EmpKey] IS NOT NULL

            END
        END

正如您所看到的,它将执行不同的选择,具体取决于@DesignKey是否为null,因此我执行查询为:

exec usp_Get_EmpoyeeByDesign 

并且它正确运行它现在执行第二次选择如果我发送@DesignKey为:

exec usp_Get_EmpoyeeByDesign @DesignKey = 2837

它加载相同的结果,它不关心我的if条件我测试它在where子句中发送designKey的参数为:

WHERE [E].[EmpKey] IS NOT NULL
                         AND [T].[DesignKey] = 2837

它会引发欲望的结果。所以总的来说查询不会验证@DesignKey是否来了。有什么问题?问候

sql-server tsql
1个回答
6
投票

!= NULL替换IS NOT NULL

任何NULL操作都返回NULL,而不是True。

将NULL视为“我不知道”。

你有两筐苹果。其中一个有7个苹果。另一个篮子被覆盖,所以你不知道有多少苹果。两个篮子的苹果数量是否相同?他们有不同数量的苹果吗?

declare @a int, @b int
set @a = 7    -- 7 apples
set @b = NULL -- I don't know how many apples

if @a = @b 
  print 'Equal' -- doesn't print

if @a <> @b 
  print 'Not equal' -- doesn't print

因为你不知道第二桶中有多少苹果,你不知道苹果的数量是否相等,你也不知道它们是否相等。

© www.soinside.com 2019 - 2024. All rights reserved.