How to handle dynamic sql parameters

前端 未结 2 1071
囚心锁ツ
囚心锁ツ 2021-01-14 09:02

What is a good way to handle dynamic sql parameters?

I have a search form that takes in a whole bunch of different search parameters. If the parameters are empty an

2条回答
  •  太阳男子
    2021-01-14 09:28

    One of the things that can be done is check whether the parameter was passed to your stored procedure. You can do it like this:

    create procedure my_procedure (
      @param1 as int = null
      @param2 as int = null
    ) as 
    begin
    
       select field1, field2, fieldn
         from table
        where ((@param1 is null) or (field2 = @param1))
          and ((@param2 is null) or (field2 = @param2))
    end 
    

    I'd rather do this on sql procedure than in the application tho

提交回复
热议问题