用于搜索日期的存储过程中的条件不起作用

时间:2013-11-07 08:59:09

标签: sql sql-server

我创建了一个存储过程,按状态,发货名称和到期日过滤许可证申请记录。代码已经运行以过滤状态和发货名称,EXPIRY DATE除外。

该功能应根据文本框中使用ajax日历的输入过滤到期日期。

我认为过滤有效期的条件有问题。

有什么建议吗?

USE [PhilfishPhilcatch]
GO
/****** Object:  StoredProcedure [dbo].[rectesting]    Script Date: 11/07/2013 08:53:48 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[rectesting]


@VesselName Varchar (30) = NULL,
@Status Varchar (10) = NULL,
@DateReleased date = NULL,
@ExpiryDate date = NULL,
@DateIssued date = NULL,
@Cfvgl_Upload varchar = NULL


AS
    BEGIN
        IF (@Status = 1)
            begin
            SELECT a.v_id, a.ship_name, b.status, b.cfvgl_validity_start , b.cfvgl_validity_end, b.cfvgl_path, b.cfvgl_date_released 
            FROM vessel_details as a INNER JOIN
            cfvgl as b ON a.v_id = b.v_id 
            WHERE
            a.ship_name LIKE ISNULL(@VesselName+'%', a.ship_name) AND
            (b.status = 4 OR b.status = 7)  
            end 

        IF (@Status <> NULL OR @Status <> '')
        begin
            SELECT a.v_id, a.ship_name, b.status, b.cfvgl_validity_start , b.cfvgl_validity_end, b.cfvgl_path, b.cfvgl_date_released 
            FROM vessel_details as a INNER JOIN
            cfvgl as b ON a.v_id = b.v_id 
            WHERE
            a.ship_name LIKE ISNULL(@VesselName+'%', a.ship_name) AND
            b.status = @Status  
            end

        IF (@ExpiryDate <> NULL OR @ExpiryDate <> '')
        begin   
            SELECT a.v_id, a.ship_name, b.status, b.cfvgl_validity_start , b.cfvgl_validity_end, b.cfvgl_path, b.cfvgl_date_released 
            FROM vessel_details as a INNER JOIN
            cfvgl as b ON a.v_id = b.v_id 
            WHERE
            b.cfvgl_validity_end = ISNULL (@ExpiryDate, b.cfvgl_validity_end)
        end
    END 

4 个答案:

答案 0 :(得分:2)

@Status <> NULL 

应该是

@Status IS NOT NULL 

答案 1 :(得分:0)

如果要检查值是否为NULL,则无法显式比较NULL。您需要将语句写为:

IF (@EXPIRY_DATE IS NOT NULL OR ...)

答案 2 :(得分:0)

建议:

IF ( NULLIF( @ExpiryDate, '' ) IS NOT NULL )
...

答案 3 :(得分:0)

空值无法与(=&gt;&lt;)运算符进行比较。 对此案例使用“IS”运算符