DBAsupport.com Forums - Powered by vBulletin
Results 1 to 4 of 4

Thread: SQLPLUS if then

  1. #1
    Join Date
    Aug 2008
    Posts
    1

    SQLPLUS if then

    using Oracle 9i
    I am trying to create a report that will look at a user variable
    and append sql criteria onto the end of the sql statement based
    on the variable.
    Example:

    define x = '&1'

    If &x = 'A' Then I would like to append 'WHERE test_column > 5' to the end of the statement.
    If &x = 'B' Then append 'WHERE test_column > 500' to the end.
    and so forth.

    I am able to get the results through PL/SQL below but I am not sure how to pass that variable to sqlplus.
    DECLARE x varchar(25) := 'A';
    y varchar(30);
    BEGIN
    SELECT DECODE(x,
    'A', 'WHERE test_column > 5',
    'B', 'WHERE test_column > 500',
    'C', 'WHERE test_column < 5')
    INTO y
    FROM DUAL;
    select disc_Cd from disc y;
    END;
    /
    select disc_Cd from disc y

  2. #2
    Join Date
    May 2006
    Location
    Taipei
    Posts
    1
    Hi Mnymoen,

    Here you go!
    -----------------------------------------
    var x varchar2(30);

    DECLARE x varchar(25) := 'A';
    y varchar(30);
    BEGIN
    SELECT DECODE(x,
    'A', 'WHERE test_column > 5',
    'B', 'WHERE test_column > 500',
    'C', 'WHERE test_column < 5')
    INTO y
    FROM DUAL;
    :x:=y;
    END;
    /

    print x

    X
    --------------------------------
    WHERE test_column > 5

    -----------------------------------------
    Daniel

  3. #3
    Join Date
    Mar 2007
    Location
    Ft. Lauderdale, FL
    Posts
    3,555
    Quote Originally Posted by mnymoen
    I am able to get the results through PL/SQL below but I am not sure how to pass that variable to sqlplus.
    DECLARE x varchar(25) := 'A';
    y varchar(30);
    BEGIN
    SELECT DECODE(x,
    'A', 'WHERE test_column > 5',
    'B', 'WHERE test_column > 500',
    'C', 'WHERE test_column < 5')
    INTO y
    FROM DUAL;
    select disc_Cd from disc y;
    END;
    /
    select disc_Cd from disc y
    No. You are not.
    Last edited by PAVB; 08-22-2008 at 05:22 AM.
    Pablo (Paul) Berzukov

    Author of Understanding Database Administration available at amazon and other bookstores.

    Disclaimer: Advice is provided to the best of my knowledge but no implicit or explicit warranties are provided. Since the advisor explicitly encourages testing any and all suggestions on a test non-production environment advisor should not held liable or responsible for any actions taken based on the given advice.

  4. #4
    Join Date
    Jul 2002
    Location
    Lake Worth, FL
    Posts
    1,492

    Talking where what?

    Maybe this will work:
    Code:
    Def X = '&1'
    Col W New_Value Test
    Select Decode('&X','A',' > 5','B',' > 500','C',' < 5') W
      From Dual;
    
    Select Disc_Cd
      From Disc 
     Where Test_Column &&Test;
    "The person who says it cannot be done should not interrupt the person doing it." --Chinese Proverb

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Click Here to Expand Forum to Full Width