Results 1 to 4 of 4
  1. #1
    Join Date
    Aug 2010
    Posts
    6

    Unanswered: Check PL/SQL var in an IF against a list of values returned from a SELECT?

    In my PL/SQL code, I've a variable and I want to check if the value of this var is in a list of values. This list of values I want to get directly from a select statement. Can I do something like below:

    Code:
    IF (v_var1 in (select col1 from <table> where <condition>))
    THEN
       <do something>
    ELSE
       <do something else>
    END IF;

  2. #2
    Join Date
    Mar 2010
    Location
    Vienna, Austria
    Posts
    149
    Code:
    declare
      vIntResult     pls_integer := 0;
    begin
      select 1 
      into   vIntResult 
      from   <table> 
      where  v_var1 = <col1> 
      and    <other_conditions>;
      
      if vIntResult = 1
         then
            <do something>
         else
            <do something else>
      end if;
    "There is always an easy solution to every problem - neat, plausible, and wrong."
    -- H.L. Mencken

  3. #3
    Join Date
    Jun 2003
    Location
    Toronto, Canada
    Posts
    5,516
    Provided Answers: 1
    Quote Originally Posted by MxC View Post

    Code:
    IF (v_var1 in (select col1 from <table> where <condition>))
    THEN
       <do something>
    ELSE
       <do something else>
    END IF;
    Code:
    IF EXISTS (select 1 from <table> where <condition>  and col1 = v_var1 )
    THEN
       <do something>
    ELSE
       <do something else>
    END IF;
    ?

  4. #4
    Join Date
    Aug 2010
    Posts
    6
    My apologies if I couldn't thank all of you for the various solutions presented. They have helped me quite a bit.

    Thanks to all of you!

Posting Permissions

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