user_id
の値が '101,102,103'であるusers
テーブルに行がありません。
文字列を別々の値のセットに分割する場合は、not in
式で文字列を使用する以外にも、少し以上の作業を行う必要があります。
いくつかの可能なアプローチ:
declare
p_csvlist varchar2(100) := '2002, 7369, 7499, 7902, 7934';
v_count integer;
begin
select count(*) into v_count
from emp e
where e.empno in
(select extractvalue(xt.column_value,'e')
from table(xmlsequence
(extract
(xmltype('<coll><e>' || replace(p_csvlist,',','</e><e>') || '</e></coll>')
, '/coll/*'))) xt);
dbms_output.put_line(v_count || ' rows');
end;
またはこの
declare
p_csvlist varchar2(100) := '2002, 7369, 7499, 7902, 7934';
v_count integer;
begin
select count(*) into v_count
from emp e
where e.empno in
(select regexp_substr(p_csvlist, '[^,]+',1,rownum)
from dual
connect by rownum <= length(p_csvlist) - length(replace(p_csvlist,',')));
dbms_output.put_line(v_count || ' rows');
end;
または本(数値のみで動作します):
declare
p_csvlist varchar2(100) := '2002, 7369, 7499, 7902, 7934';
v_count integer;
begin
select count(*) into v_count
from emp e
where e.empno in
(select to_number(xt.column_value)
from xmltable(p_csvlist) xt);
dbms_output.put_line(v_count || ' rows');
end;
例としては、私のよくある質問の記事からです: www.williamrobertson.net/documents/comma-separated.html
'によって提供さsys.DBMS_DEBUG_VC2COLL --collectionデータ型を使用することができます正しく働く。それは働くと思われるように働く。あなたは 'in'節に値のリストを渡していません。ちょうど1つの値を渡しています - ' 101,102,103 '' –