If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Oracle > Check PL/SQL var in an IF against a list of values returned from a SELECT?

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-24-10, 07:54
MxC MxC is offline
Registered User
 
Join Date: Aug 2010
Posts: 6
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;
Reply With Quote
  #2 (permalink)  
Old 08-24-10, 09:48
magicwand magicwand is offline
Registered User
 
Join Date: Mar 2010
Location: Vienna, Austria
Posts: 112
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;
__________________
If A is a success in life, then A = x + y + z.
Work is x; y is play; and z is keeping your mouth shut. After all the years, I'm still working on the correct value for z.
(Albert Einstein)
Reply With Quote
  #3 (permalink)  
Old 08-24-10, 10:22
n_i n_i is online now
:-)
 
Join Date: Jun 2003
Location: Toronto, Canada
Posts: 4,233
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;
?
Reply With Quote
  #4 (permalink)  
Old 08-30-10, 07:07
MxC MxC is offline
Registered User
 
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!
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On