如何在 Oracle Sql 中的存储过程中传递 where in 子句值
how to pass where in clause values in stored procedure in Oracle Sql
我有一个程序将参数作为逗号分隔格式的字符串,如“已保存,进行中”。以下程序在“已保存”等状态下运行良好。但不使用逗号分隔值
create or replace PROCEDURE get_Sample_request (
in_request_status IN VARCHAR2,
out_cursor OUT SYS_REFCURSOR
) AS
sql_qry VARCHAR2 (150);
emp_tot NUMBER(3);
BEGIN
OPEN out_cursor FOR SELECT * from user_requests where request_status in (in_request_status);
END get_Sample_request;
使用LIKE
:
create or replace PROCEDURE get_Sample_request (
in_request_status IN VARCHAR2,
out_cursor OUT SYS_REFCURSOR
) AS
sql_qry VARCHAR2 (150);
emp_tot NUMBER(3);
BEGIN
OPEN out_cursor FOR
SELECT *
FROM user_requests
WHERE ',' || in_request_status || ',' LIKE '%,' || request_status || ',%';
END get_Sample_request;
/
或者,传入一个集合:
create or replace PROCEDURE get_Sample_request (
in_request_status IN SYS.ODCIVARCHAR2LIST,
out_cursor OUT SYS_REFCURSOR
) AS
sql_qry VARCHAR2 (150);
emp_tot NUMBER(3);
BEGIN
OPEN out_cursor FOR
SELECT *
FROM user_requests
WHERE request_status IN (SELECT column_value FROM TABLE(in_request_status));
END get_Sample_request;
/
或者,拆分字符串(较慢):
create or replace PROCEDURE get_Sample_request (
in_request_status IN VARCHAR2,
out_cursor OUT SYS_REFCURSOR
) AS
sql_qry VARCHAR2 (150);
emp_tot NUMBER(3);
BEGIN
OPEN out_cursor FOR
SELECT *
FROM user_requests
WHERE request_status IN (SELECT REGEXP_SUBSTR(in_request_status, '[^,]+', 1, LEVEL)
FROM DUAL
CONNECT BY LEVEL <= REGEXP_COUNT(in_request_status, '[^,]+'));
END get_Sample_request;
/
我有一个程序将参数作为逗号分隔格式的字符串,如“已保存,进行中”。以下程序在“已保存”等状态下运行良好。但不使用逗号分隔值
create or replace PROCEDURE get_Sample_request (
in_request_status IN VARCHAR2,
out_cursor OUT SYS_REFCURSOR
) AS
sql_qry VARCHAR2 (150);
emp_tot NUMBER(3);
BEGIN
OPEN out_cursor FOR SELECT * from user_requests where request_status in (in_request_status);
END get_Sample_request;
使用LIKE
:
create or replace PROCEDURE get_Sample_request (
in_request_status IN VARCHAR2,
out_cursor OUT SYS_REFCURSOR
) AS
sql_qry VARCHAR2 (150);
emp_tot NUMBER(3);
BEGIN
OPEN out_cursor FOR
SELECT *
FROM user_requests
WHERE ',' || in_request_status || ',' LIKE '%,' || request_status || ',%';
END get_Sample_request;
/
或者,传入一个集合:
create or replace PROCEDURE get_Sample_request (
in_request_status IN SYS.ODCIVARCHAR2LIST,
out_cursor OUT SYS_REFCURSOR
) AS
sql_qry VARCHAR2 (150);
emp_tot NUMBER(3);
BEGIN
OPEN out_cursor FOR
SELECT *
FROM user_requests
WHERE request_status IN (SELECT column_value FROM TABLE(in_request_status));
END get_Sample_request;
/
或者,拆分字符串(较慢):
create or replace PROCEDURE get_Sample_request (
in_request_status IN VARCHAR2,
out_cursor OUT SYS_REFCURSOR
) AS
sql_qry VARCHAR2 (150);
emp_tot NUMBER(3);
BEGIN
OPEN out_cursor FOR
SELECT *
FROM user_requests
WHERE request_status IN (SELECT REGEXP_SUBSTR(in_request_status, '[^,]+', 1, LEVEL)
FROM DUAL
CONNECT BY LEVEL <= REGEXP_COUNT(in_request_status, '[^,]+'));
END get_Sample_request;
/