HiveBrain v1.2.0
Get Started
← Back to all entries
snippetMinor

How do I use the IF...ELSE condition in a WHERE clause?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
theconditionelsewherehowuseclause

Problem

I'm attempting to use the IF...ELSE construct in my WHERE clause to selectively apply conditions to my SELECT.

Should this work?

CREATE OR REPLACE package body If_Else_Pack IS

    PROCEDURE Moving
    (
        obj_A       IN varchar2,
        obj_B       IN varchar2,
        obj_C       IN varchar2,
        obj_D       IN varchar2,
        cur_Result  OUT T_CURSOR
    ) IS
    BEGIN
        OPEN cur_Result FOR
        SELECT 
            w.assetid,
            w.vehiclenumber,
            w.LatLong,
            w.CurrentSpeed,
            w.timeOfMovement,
            w.CurrentPlace,
            w.curTime,
            w.motion,
            w.fuelR,
            w.VehicleStart
        FROM waypoints1 w
        WHERE 
            IF ((obj_D= '0' OR obj_D IS NULL) AND (obj_C= '0' OR  obj_C IS NULL)) THEN
                WHERE w.customer_id =obj_A
                AND w.delegate_user_id = obj_B;
            ELSE IF ((obj_D= '0 'OR obj_D IS NULL) AND (obj_C<> '0' OR obj_C IS NOT NULL)) THEN
                WHERE w.customer_id = obj_A
                AND w.category_id = obj_C
                AND w.delegate_user_id = obj_B;
            ELSE IF ((obj_D<> '0' OR obj_Dis NOT NULL) AND(obj_C= '0' OR obj_C IS NULL)) THEN
                WHERE w.customer_id = obj_A
                AND w.fleet_id = obj_D
                AND w.delegate_user_id = obj_B;
    END MOVING;
END IF_ELSE_PACK;

Solution

I think the question is a little misleading & causes people to not think properly. Looks like the 3 conditions won't overlap, so all you need to do is OR the 3 statements together:

create or replace package body If_Else_Pack is

  Procedure Moving(obj_A IN varchar2,
                   obj_B  IN varchar2,
                   obj_C IN varchar2,
                   obj_D    IN varchar2,
                   cur_Result     OUT T_CURSOR) is

   begin

        open cur_Result for
        select w.assetid,
                 w.vehiclenumber,
                 w.LatLong,
                 w.CurrentSpeed,
                 w.timeOfMovement,
                 w.CurrentPlace,
                 w.curTime,
                 w.motion,
                 w.fuelR,
                 w.VehicleStart
        from waypoints1 w
        where 
        (((obj_D= '0' or obj_D is null) and (obj_C= '0' oR  obj_C is null)) 
                 and w.customer_id =obj_A
                 and w.delegate_user_id = obj_B)
        or
        (((obj_D= '0 'or obj_D is null) and (obj_C<> '0' or obj_C is not null)) 
           and w.customer_id = obj_A 
           and w.category_id = obj_C 
           and w.delegate_user_id = obj_B)
        or
        (((obj_D<> '0' or obj_D is not null) and (obj_C= '0' or obj_C is null))
           and w.customer_id = obj_A
           and w.fleet_id = obj_D
           and w.delegate_user_id = obj_B);
END MOVING;
END IF_ELSE_PACK;


The WHERE condition can be further simplified to:

WHERE 
              w.customer_id = obj_A
          AND 
              w.delegate_user_id = obj_B
          AND 
              ( (obj_D = '0' or obj_D IS NULL) AND (obj_C= '0' OR obj_C IS NULL) 
             OR 
                (obj_D = '0' or obj_D IS NULL) AND (w.category_id = obj_C) 
             OR 
                (w.fleet_id = obj_D) AND (obj_C= '0' OR obj_C IS NULL) 
              ) ;

Code Snippets

create or replace package body If_Else_Pack is

  Procedure Moving(obj_A IN varchar2,
                   obj_B  IN varchar2,
                   obj_C IN varchar2,
                   obj_D    IN varchar2,
                   cur_Result     OUT T_CURSOR) is

   begin

        open cur_Result for
        select w.assetid,
                 w.vehiclenumber,
                 w.LatLong,
                 w.CurrentSpeed,
                 w.timeOfMovement,
                 w.CurrentPlace,
                 w.curTime,
                 w.motion,
                 w.fuelR,
                 w.VehicleStart
        from waypoints1 w
        where 
        (((obj_D= '0' or obj_D is null) and (obj_C= '0' oR  obj_C is null)) 
                 and w.customer_id =obj_A
                 and w.delegate_user_id = obj_B)
        or
        (((obj_D= '0 'or obj_D is null) and (obj_C<> '0' or obj_C is not null)) 
           and w.customer_id = obj_A 
           and w.category_id = obj_C 
           and w.delegate_user_id = obj_B)
        or
        (((obj_D<> '0' or obj_D is not null) and (obj_C= '0' or obj_C is null))
           and w.customer_id = obj_A
           and w.fleet_id = obj_D
           and w.delegate_user_id = obj_B);
END MOVING;
END IF_ELSE_PACK;
WHERE 
              w.customer_id = obj_A
          AND 
              w.delegate_user_id = obj_B
          AND 
              ( (obj_D = '0' or obj_D IS NULL) AND (obj_C= '0' OR obj_C IS NULL) 
             OR 
                (obj_D = '0' or obj_D IS NULL) AND (w.category_id = obj_C) 
             OR 
                (w.fleet_id = obj_D) AND (obj_C= '0' OR obj_C IS NULL) 
              ) ;

Context

StackExchange Database Administrators Q#24251, answer score: 6

Revisions (0)

No revisions yet.