如何将字符串列表传递给Olracle中的存储过程

时间:2019-02-26 00:09:24

标签: oracle stored-procedures plsql

如何将字符串列表传递给存储过程?  这是我的存储过程:


CREATE OR REPLACE PROCEDURE sp_search (p_name     IN VARCHAR2,
                                       r_cursor      SYS_REFCURSOR)
AS
BEGIN
    OPEN cursor FOR SELECT name
                      FROM tableN
                     WHERE name IN (p_name);
END;

示例:

var c_ref refcursor; 
sp_search('Andy,Marty,miky',:c_ref);
print c_ref;

1 个答案:

答案 0 :(得分:1)

我认为应该这样做

CREATE OR REPLACE PROCEDURE sp_search (
    p_name     IN VARCHAR2,
    r_cursor   OUT  SYS_REFCURSOR)
AS
Declare
    v_sql  VARCHAR2(200); 
BEGIN

    v_sql := 'SELECT name FROM table WHERE name IN (' || p_name || ')';
    OPEN r_cursor FOR v_sql;

END;

仅确保在传递Andy,Marty,miky时添加',以使最终的SQL看起来像

SELECT name FROM table WHERE name IN ('Andy','Marty','miky')