Oracle PL\SQL Null Input Parameter WHERE condition

前端 未结 2 728
花落未央
花落未央 2021-02-10 13:02

As of now I am using IF ELSE to handle this condition

IF INPUT_PARAM IS NOT NULL

    SELECT ... FROM SOMETABLE WHERE COLUMN = INPUT_PARAM
ELSE
    SELECT ... FR         


        
2条回答
  •  野的像风
    2021-02-10 13:25

    One method would be to use a variant of

     WHERE column = nvl(var, column)
    

    There are two pitfalls here however:

    1. if the column is nullable, this clause will filter null values whereas in your question you would not filter the null values in the second case. You could modify this clause to take nulls into account but it turns ugly:

          WHERE nvl(column, impossible_value) = nvl(var, impossible_value)
      

      Of course if somehow the impossible_value is ever inserted you will run into some other kind of (fun) problems.

    2. The optimizer doesn't understand correctly this type of clause. It will sometimes produce a plan with a UNION ALL but if there are more than a couple of nvl, you will get full scan even if perfectly valid indexes are present.

    This is why when there are lots of parameters (several search fields in a big form for example), I like to use dynamic SQL:

    DECLARE
       l_query VARCHAR2(32767) := 'SELECT ... JOIN ... WHERE 1 = 1';
    BEGIN
       IF param1 IS NOT NULL THEN
          l_query := l_query || ' AND column1 = :p1';
       ELSE 
          l_query := l_query || ' AND :p1 IS NULL';
       END IF;
       /* repeat for each parameter */
       ...
       /* open the cursor dynamically */
       OPEN your_ref_cursor FOR l_query USING param1 /*,param2...*/; 
    END;
    

    You can also use EXECUTE IMMEDIATE l_query INTO l_result USING param1;

提交回复
热议问题