动态sql,当变量个数不确定时。如果直接open vCursor for piSQL using 变量1,变量2 ,真是一个体力活。
发现居然可用 DBMS_SQL.TO_REFCURSOR()转换为 可返回的cursor;
create or replace procedure test_dbms_sql
(
curCursor out sys_refcursor,
piStatus in char,
piCompanyID in varchar2
)
is
V_SQL VARCHAR2(1000) ;
vCurID NUMBER;
vResult number;
begin
--poErrorCode :='0' ;
vCurID := DBMS_SQL.OPEN_CURSOR;
---略去sql拼接过程
V_SQL := 'SELECT * FROM t_product where status = :Status and Company_id = :CompanyID';
DBMS_SQL.PARSE(vCurId, V_SQL, DBMS_SQL.NATIVE);
if (piCompanyID is not null) then
DBMS_SQL. bi nd_variable(vCurId,':CompanyID',piCompanyID );
end if;
if (piStatus is not null) then
DBMS_SQL.bind_variable(vCurId,':Status',piStatus );
end if;
vResult := DBMS_SQL.EXECUTE(vCurID);
--转为可返回的cursor
curCursor := DBMS_SQL.TO_REFCURSOR(vCurID);
--poErrorCode := '1' ;
exception
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE(SQLErrm);
end test_dbms_sql;
发现居然可用 DBMS_SQL.TO_REFCURSOR()转换为 可返回的cursor;
create or replace procedure test_dbms_sql
(
curCursor out sys_refcursor,
piStatus in char,
piCompanyID in varchar2
)
is
V_SQL VARCHAR2(1000) ;
vCurID NUMBER;
vResult number;
begin
--poErrorCode :='0' ;
vCurID := DBMS_SQL.OPEN_CURSOR;
---略去sql拼接过程
V_SQL := 'SELECT * FROM t_product where status = :Status and Company_id = :CompanyID';
DBMS_SQL.PARSE(vCurId, V_SQL, DBMS_SQL.NATIVE);
if (piCompanyID is not null) then
DBMS_SQL. bi nd_variable(vCurId,':CompanyID',piCompanyID );
end if;
if (piStatus is not null) then
DBMS_SQL.bind_variable(vCurId,':Status',piStatus );
end if;
vResult := DBMS_SQL.EXECUTE(vCurID);
--转为可返回的cursor
curCursor := DBMS_SQL.TO_REFCURSOR(vCurID);
--poErrorCode := '1' ;
exception
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE(SQLErrm);
end test_dbms_sql;