0

我正在尝试做(在 PL/SQL 中)

if ((var1, var2) in ( select number1, number2 from.......) ) then
....
end if;

为什么这不起作用?怎么做才是正确的?

4

2 回答 2

2

试试这个:

declare
  l_exist number(1);
  var1 ...
  var2 ...
begin
  --obtain var1 & var2 
  -- ...
  select 
    case 
      when exists(select1 from ...
                  where number1 = var1 and number2 = var2) then 1
      else 0
    end into l_exist
  from dual;

  if l_exist = 1 
  then
    -- do what you want here
  end if;
end;
于 2013-05-28T16:10:26.537 回答
2

答案是在 PL/SQL 中,您必须将 ( select) 某些内容读入变量中,然后在IF语句中使用它。您可以通过多种方式执行此操作,但您不能在IF子句中执行此操作。Chorel 的回答在这方面很有创意,他的回答值得称赞。

你也可以这样做

declare
  l_count number;
  var1 number := 1; -- or whatever the number
  var2 number := 2;
begin
  select count(*) 
    into l_count
    from mytable
   where number1 = var1 and number2 = var2;
  if l_count > 0 then
     ... the row exists
  end if;
end;

但以下不起作用

if (select count(*) from mytable where (number1,number2) in (1,2)) > 0 then -- WRONG
    ...
end if;

如果你想使用in你必须求助于表的功能dual

   where (number1, number2) in (select var1, var2 from dual);

像这样

declare
  l_count number;
  var1 number := 1; -- or whatever the number
  var2 number := 2;
begin
  select count(*) 
    into l_count
    from mytable
   where (number1, number2) in (select var1, var2 from dual);
  if l_count > 0 then
     ...
  end if;
end;
于 2013-05-28T20:45:03.137 回答