暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Oracle where子句中singel varibale中的多个值

ASKTOM 2019-07-03
207

问题描述

嗨,汤姆,我对一个场景有疑问。
我有一个名为test2的表

create table test2(
  id number(10),country varchar2(100)
);

BEGIN
  insert into test2 values(1,'INDIA');
  insert into test2 values(2,'INDIA');
  insert into test2 values(3,'INDIA');
  insert into test2 values(4,'INDIA');
  insert into test2 values(5,'INDIA');
  insert into test2 values(6,'INDIA');
  insert into test2 (ID)values(7);
  insert into test2 values(8,'US');
  insert into test2 values(9,'US');
  COMMIT;
END;
/
复制

我写了一个程序。
create or replace procedure test3 (
  p_currency in varchar2 default null
) as
  v_currency varchar2 (100);
begin
  v_currency := upper (p_currency);
  select id
  into v_id
  from vinay_test2
  where country in
    case
      when upper (p_currency) in (
        'US',
        'INDIA'
      ) then
        upper (p_currency)
      when (upper (p_currency) is null)
           or (upper (p_currency) not in (
        'INDIA',
        'US'
      )) then
        in ('US','INDIA')
      else
        in ('US',india)
    end;

  dbms_output.put_line ('v_id' || v_id);
end;
/
复制


我的疑问是我如何在where子句中的单个变量中使用多个值,如上所示。

假设p_currrency是我的用户输入。如果输入的是印度或美国,则它将按照where子句显示所需的结果。

但是,如果输入为null/除印度和美国以外,那么我想要包含印度和美国记录的结果。

请让我知道如何实现这一目标。

如果输入是我们,那就是

专家解答

我不明白您查询的逻辑。示例预期输出将有所帮助!

这是我对你想做的事情的猜测:

-如果货币为null,则返回美国和印度行
-如果货币 = US或INDEX,则返回匹配国家/地区的行
-如果货币是其他货币,请返回美国和印度行

看起来像:

var curr varchar2(10);

select * from test2
where  (    
    country in ( 'US', 'INDIA' ) 
and ( :curr is null or :curr not in ( 'US', 'INDIA' ) )
) or (
  :curr = country
);

ID    COUNTRY   
    1 INDIA      
    2 INDIA      
    3 INDIA      
    4 INDIA      
    5 INDIA      
    6 INDIA      
    8 US         
    9 US 

exec :curr := 'UK';

select * from test2
where  (    
    country in ( 'US', 'INDIA' ) 
and ( :curr is null or :curr not in ( 'US', 'INDIA' ) )
) or (
  :curr = country
);

ID    COUNTRY   
    1 INDIA      
    2 INDIA      
    3 INDIA      
    4 INDIA      
    5 INDIA      
    6 INDIA      
    8 US         
    9 US 

exec :curr := 'INDIA';

select * from test2
where  (    
    country in ( 'US', 'INDIA' ) 
and ( :curr is null or :curr not in ( 'US', 'INDIA' ) )
) or (
  :curr = country
);

ID    COUNTRY   
    1 INDIA      
    2 INDIA      
    3 INDIA      
    4 INDIA      
    5 INDIA      
    6 INDIA  
复制

文章转载自ASKTOM,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论