SQL ignore part of WHERE if parameter is null

后端 未结 7 611
既然无缘
既然无缘 2020-12-15 15:59

I have a stored procedure that fetches info from a table based on 4 parameters.

I want to get values based on the parameters, but if a parameter is NULL then that pa

7条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-15 16:32

    CREATE PROCEDURE myProcedure
        @Param1 nvarchar(50),
        @Param2 nvarchar(50),
        @Param3 nvarchar(50),
        @Param4 nvarchar(50)
    AS
    BEGIN
        IF(@Param1 IS NULL)
            BEGIN
                SELECT Id, col1, col2, col3, col4 FROM myTable
            END
        ELSE
            BEGIN
                SELECT Id, col1, col2, col3, col4 FROM myTable WHERE col1 LIKE @Param1+'%' OR @Param1 is Null
            END
    END
    

    This should help

    regards

    Ashutosh Arya

提交回复
热议问题