Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 当参数为NULL时执行存储过程_Sql Server_Stored Procedures_Parameters - Fatal编程技术网

Sql server 当参数为NULL时执行存储过程

Sql server 当参数为NULL时执行存储过程,sql-server,stored-procedures,parameters,Sql Server,Stored Procedures,Parameters,我有一个包含6个参数的存储过程,即使其中一些参数为null,我也希望执行它。这就是我所拥有的: CREATE OR ALTER PROCEDURE [dbo].[report] (@StartDate DATETIME, @EndDate DATETIME, @Mode VARCHAR(50) = '', -- { SS105 | SS106 } @DEBUG BIT = 0, @Age FLOAT =

我有一个包含6个参数的存储过程,即使其中一些参数为null,我也希望执行它。这就是我所拥有的:

CREATE OR ALTER PROCEDURE [dbo].[report] 
    (@StartDate DATETIME,
     @EndDate   DATETIME,
     @Mode      VARCHAR(50) = '',     -- { SS105 | SS106 } 
     @DEBUG     BIT = 0,
     @Age       FLOAT = NULL,     -- { Greater than 59 or less )
     @EmpStatus VARCHAR(255) = NULL)  -- { 'Employed' OR 'Unemployed }
AS
BEGIN
    IF @Mode = 'SS106'
    BEGIN
        DECLARE @SS106 TABLE
        (   
                [ClientID]              INT 
           ,    [ClientName]            VARCHAR(255)
           ,    [VService]              VARCHAR(10)
           ,    [Age]                   FLOAT
           ,    [EmploymentStatus]      VARCHAR(255)
        );
        INSERT INTO @SS106
        (
                [ClientID]          
           ,    [ClientName]        
           ,    [VService]          
           ,    [Age]               
           ,    [EmploymentStatus]  
        )
        SELECT DISTINCT 
                [ClientID]              =       [c].[ClientID]
            ,   [ClientName]            =       [c].[ClientName]
            ,   [VService]              =       [c].[VService]
            ,   [Age]                   =       [c].[Age] 
            ,   [EmploymentStatus]      =       [c].[EmploymentStatus]
        FROM [#Table] AS [c] WITH (NOLOCK) 
        WHERE 
            ( @Age IS NULL OR [c].[Age] >= @Age ) 
        AND 
            ( @EmpStatus IS NULL OR ( @EmpStatus = 'Employed' AND [c].[EmploymentStatus] IN 
                                ('Employed Part Time (<35 hrs per week)', 'Employed Full Time (35+ hrs per week)') 
                                    )               
        OR          
              @EmpStatus IS NULL OR ( @EmpStatus = 'Unemployed' AND [c].[EmploymentStatus] NOT IN 
                                ('Employed Part Time (<35 hrs per week)', 'Employed Full Time (35+ hrs per week)')
                                    )
            )
        AND [c].[VService] IN ('SS106') 

            IF @DEBUG = 1
                BEGIN 
                        SELECT * FROM @SS106
                END
            RETURN 0
    END
我希望存储过程在最后两个参数为空时执行并显示表。当我将
@Age
@EmpStatus
设置为所需值时,它会工作。但是,当我将
@EmpStatus
保留为NULL,其余保留一个值时,它将失败。这可能是一个括号问题,但是否有一个有效的方法来做到这一点


我知道这是很多阅读,但我已经被困在这里两个小时了。这是我最后的办法。

您需要写下您的where条件,如下所示:

对于
null
0
作为
@Age
的参数值,它将在这两种情况下都有效

WHERE ( isnull(@Age, 0) = 0 OR ( isnull(@Age, 0) <> 0 and [c].[Age] >= @Age ))  
AND ( isnull(@EmpStatus, '') = '' OR ( isnull(@EmpStatus, '') = 'Employed' and [c].[EmploymentStatus] in ('Employed Part Time (<35 hrs per week)', 'Employed Full Time (35+ hrs per week)')))  
其中(isnull(@Age,0)=0或(isnull(@Age,0)0和[c].[Age]>=@Age))

和(isnull(@EmpStatus,)=''或(isnull(@EmpStatus,)='employeed'和[c].[EmploymentStatus]in('employeed Part Time)(你需要写下你的where条件,如下所示:

对于
null
0
作为
@Age
的参数值,它将在这两种情况下都有效

WHERE ( isnull(@Age, 0) = 0 OR ( isnull(@Age, 0) <> 0 and [c].[Age] >= @Age ))  
AND ( isnull(@EmpStatus, '') = '' OR ( isnull(@EmpStatus, '') = 'Employed' and [c].[EmploymentStatus] in ('Employed Part Time (<35 hrs per week)', 'Employed Full Time (35+ hrs per week)')))  
其中(isnull(@Age,0)=0或(isnull(@Age,0)0和[c].[Age]>=@Age))
和(isnull(@EmpStatus,)=''或(isnull(@EmpStatus,)='employeed'和[c].[EmploymentStatus]在('employeed Part Time')中(