0
我試圖做(在PL/SQL)IN和IF在PL/SQL運營商
if ((var1, var2) in (select number1, number2 from.......)) then
....
end if;
爲什麼這個不行? 如何做纔是正確的事情?
我試圖做(在PL/SQL)IN和IF在PL/SQL運營商
if ((var1, var2) in (select number1, number2 from.......)) then
....
end if;
爲什麼這個不行? 如何做纔是正確的事情?
答案是,在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;
試試這個:
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;