存储过程意外结果不合需要

时间:2011-12-07 07:22:13

标签: sql sql-server stored-procedures

ALTER PROCEDURE  [dbo].[USP_ViewRegForm]
    -- Add the parameters for the stored procedure here
    (   @RegFormID      Numeric=null,
        @FDate          Date=null,
        @TDate          Date=null,
        @viewPending        int=null
     )
AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;
    SET DATEFORMAT DMY;
    -- View statements for procedure here
    -- 
    If (@viewPending = 1 or @viewPending = 0 Or @viewPending is null)
    Begin

            SELECT 
            RegDate,
            iRegFormID    As [Registraion ID],
            CenterName  As [Center Name],
            OwnerName   As [Owner Name],
            MobileNo    As [Mobile],
            MailID      As [EMail ID],
            isVerified  As [Verified]
        FROM  TBL_iREGFORM
        WHERE (@FDate Is Null or RegDate <= @TDate ) And 
              (@TDate is Null or RegDate >= @FDate ) And
              ((RegDate between @FDate and @TDate) OR (RegDate=convert(varchar(20),GETDATE(),103)) )  And
              isVerified in (Case When  @viewPending =1 Then 0 Else 1 | 0 End) 
    End

问题在于:isVerified in (Case When @viewPending =1 Then 0 Else 1 | 0 End)

如果我没有传递@viewPenind值,那么它必须同时选择1和0.但是这只返回一个值..

为什么?

Previous question

1 个答案:

答案 0 :(得分:2)

将WHERE部分更改为isVerified = COALESCE(@viewPending, isVerified)

1=NULL评估为假。