1

I want to know how to select columns from a table where another column has several possible values. So, for example, this code will return Column X from table 1 and Column B from Table 2 if Column G has the value given by the input.

variable input varchar2(60);
exec :input := 'c';

Select Table1.x, Table2.b
  FROM Table1, 
       Table2
 WHERE Table1.g = :input

What if instead, I wanted to return Column X from table1 and ColumnB from Table2 if ColumnG had value "k" or "c" or "d" without doing

WHERE Table1.g = "k" or Table1.g = "c" or Table1.g = "d"

How do i define the input such that I get this effect?

I use: oracle 10g, PL/SQL

Nick Krasnov
  • 26,886
  • 6
  • 61
  • 78
RebeccaK375
  • 871
  • 3
  • 17
  • 28

5 Answers5

1

As I understood from your comment to @SuperMan answer

Yes, is there any way to define a variable to be a placeholder for the ('c', 'd', 'k')?

you want to parametrize IN clause.

Obviously you cannot pass the :input variable in the IN clause defined like this exec :input := 'k,c,d'. I mean, technically you can, but the query wont produce the desired output because it will search for the entire 'k,c,d' string.

What you can do is, you can split the 'k,c,d'(or any other) string by comma and use the result in the IN clause.

Here is an example:

-- variable of this type will contain letters produced by 
-- splitting 'k,c,d' string
create or replace type T_vars as table of varchar2(5)

-- This table function returns collection of individual letters
create or replace function GetLetters(p_input in varchar2)
return t_vars
is
  l_res T_Vars;
begin
  select cast(
              collect(
                       cast(res as varchar2(5))
                      ) as T_Vars
             )
    into l_res
    from ( select regexp_substr(p_input, '[^,]+', 1, level) as res
             from dual
          connect by level <= regexp_count(p_input, '[^,]+' )
          );

  return l_res;
end;  

Here is how it works:

SQL> variable x varchar2(11);

SQL> exec :x := 'k,c,d';

PL/SQL procedure successfully completed

SQL> select col
  2    from table1
  3   where col in (select column_value from table(getletters(:x)))
  4  ;

Result:

 COL
 ---
 k
 c
 d
Nick Krasnov
  • 26,886
  • 6
  • 61
  • 78
0

Looking for this?

Select Table1.x, Table2.b
FROM Table1, Table2
WHERE Table1.g  in ('c', 'd', 'k')

Edit - reply on comment: It is not straight forward or take a look here.

Community
  • 1
  • 1
SuperMan
  • 178
  • 1
  • 1
  • 7
0

Since you are using pl/sql a FOR loop can be used to run the query for each value. Something like this:

FOR x in ('k','c','d')
loop
Select Table1.x, Table2.b into var1,var2
FROM Table1, Table2
WHERE Table1.g = x
dbms_output.put_line(var1,var2);
end loop;
Baljeet
  • 428
  • 2
  • 9
0

Its pretty straight forward -

Select Table1.X, Table2.B FROM TABLE1, TABLE2 WHERE TABLE1.g IN ('c', 'd', 'k')

Basically it will give you a Cartesian product of all row from table 1 where Table1.g = c or d or k and all rows from Table2 since you don't have any another join condition.

Aniket
  • 279
  • 3
  • 8
  • 21
-1

Can you try this?

where table1.g in ("k","c","d")
bummi
  • 27,123
  • 14
  • 62
  • 101
Sander
  • 390
  • 1
  • 4
  • 13
  • It works, I just wonder if I can define a variable which will be a placeholder of the stuff in parenthesis. Thank you – RebeccaK375 Aug 28 '13 at 11:54