我对Oracle非常不熟悉,我只是想让这个工作。我看了这里和这里,但不知道如何使它全部工作。这是我需要的。我需要一个过程,它将接受一个逗号分隔的字符串作为参数,我可以在查询的"in子句"中使用,并返回游标结果。有没有办法在一个包罗万象的过程中做到这一点?如果没有,我需要采取哪些不同的步骤?再次,我为我对Oracle缺乏了解而道歉,这只是试图让某些东西快速工作。
感谢过程如下(p_Scope将是逗号分隔的输入):
create or replace PROCEDURE CU_SELECTION_ID
(
p_Scope IN varchar2,
p_ResultSet OUT SYS_REFCURSOR
)
is
BEGIN
OPEN p_ResultSet FOR
select
b.addr1,
b.addr2,
b.city,
b.country_code,
a.customer_no,
b.des1,
a.entity,
b.main_phone_no,
b.phone_area_code,
b.status,
b.wb_site_url,
b.zip
from
ar_customer a,
ct_addr b
where b.main_loc_flag = 'Y' and
a.customer_no = b.customer_no and
a.entity = b.cust_entity and
b.stk_loc_no = '3' and
b.customer_no in (p_Scope);
END;
我相信有一个"更好的方法",但我不确定它是什么现在…
这应该对你有用:
替换:
b.customer_no in (p_Scope);
instr(p_Scope, ','||b.customer_no||',' ) > 0
如果b.customer_no出现在列表中,将搜索p_Scope并返回> 0的值。
确保列表中的第一个和最后一个字符是逗号(',')
(另外,作为Oracle的新来者,我发现Tech Republic是一个非常有用的快速资源。)
假设声明
create or replace type cvarchar2 as table of varchar2(4000);
查询select * from some_table t where some_column in
('FOO','BAR')
给出与
相同的结果select * from some_table t where some_column in
(select column_value from table(cvarchar2('FOO','BAR')))
可以使用第二个,将PLSQL集合传递给表函数。我从Java中这样做,其中...table(?)
完美地工作。与传统的in子句相比,Explain plan似乎还不错。
基于分隔符的文本搜索的解决方案可能是性能杀手。
为了记录,这里有另一种难看的方法。
PROCEDURE getreport (
p_affiliates IN varchar2,
p_StartDate IN date,
p_EndDate IN date,
p_ReturnValue OUT sys_refcursor
) IS
BEGIN
DECLARE
sql_stmt VARCHAR2(4000);
BEGIN
sql_stmt := 'SELECT
FIRSTNAME,
LASTNAME,
ADDRESSLINE,
SUITE,
CITY,
STATE,
ZIP
FROM
ORDERHEADER head
INNER JOIN ORDERDETAIL detail
on head.ORDERTRACKINGLOGID = detail.ORDERTRACKINGLOGID
INNER JOIN ORDERTRACKINGDETAIL trackdetail
on detail.ORDERDETAILID = trackdetail.ORDERDETAILID
AND head.ORDERHEADERID = trackdetail.ORDERHEADERID
INNER JOIN AFFILIATE aff
on trackdetail.AFFILIATEID = aff.AFFILIATEID
WHERE
aff.AFFILIATEID IN
(
select
AFFILIATEID
from
AFFILIATE
where
AFFILIATEID IN (' || p_affiliates || ')
)
AND
head.CALENDAR_DATE >= TO_DATE( :p_StartDate )
AND
head.CALENDAR_DATE <= TO_DATE( :p_EndDate )
ORDER BY AFFILIATEID,
AFFILIATENAME
';
OPEN p_ReturnValue for sql_stmt USING p_StartDate, p_EndDate;
END;
END getreport;
你可以这样使用:
SELECT * FROM MATABLE
WHERE MT_ID
IN (SELECT REGEXP_SUBSTR(MYPARAM,'[^,]+', 1, LEVEL)
FROM DUAL
CONNECT BY REGEXP_SUBSTR(MYPARAM, '[^,]+', 1, LEVEL) IS NOT NULL))
MYPARAM——"368134、181956"