我想创建一种存储过程,使它接受表名作为参数并在过程中动态传递它。例如:我想将表名“ DATA.WORK_CPG”作为参数传递给存储过程。
这是实际步骤
create or replace PROCEDURE Proc_Sample
(
ApplicationID IN varchar2,
CType IN varchar2,
WBName IN varchar2,
WBList OUT SYS_REFCURSOR
)AS
BEGIN
OPEN WBList
FOR
SELECT
WO.RECORDNUMBER AS RECORDNUMBER,
FROM DATA.ASSIGN_WB WB
INNER JOIN DATA.WORK_CPG WO ON WO.PZINSKEY = WB.PXREFOBJECTKEY
INNER JOIN COMMON.REF_APPLICATION RA ON RA.APPLICATIONNAME = WB.PXAPPLICATION
WHERE RA.APPLICATIONID = ApplicationID AND WB.ASSIGNEDID IN ( select regexp_substr(WBName,'[^,]+', 1, level) from dual
connect by regexp_substr(WBName, '[^,]+', 1, level) is not null );
END Proc_Sample;
我试图通过将表名作为参数传递给存储过程来按如下所示进行转换
create or replace PROCEDURE Proc_Sample
(
TableName in varchar2,
ApplicationID IN varchar2,
CaseType IN varchar2,
WBName IN varchar2,
WBList OUT SYS_REFCURSOR
)AS
stmt varchar2(10000);
BEGIN
--OPEN WBList
--FOR
stmt := ' SELECT
WO.RECORDNUMBER AS RECORDNUMBER,
FROM DATA.PC_ASSIGN_WB WB
INNER JOIN ' || TableName || ' WO ON WO.PZINSKEY = WB.PXREFOBJECTKEY
INNER JOIN COMMON.REF_APPLICATION RA ON RA.APPLICATIONNAME = WB.PXAPPLICATION
WHERE RA.APPLICATIONID = ApplicationID AND WB.ASSIGNEDID IN (select regexp_substr(''' || WBName || ''',''[^,]+'', 1, level) from dual
connect by regexp_substr(''' || WBName || ''', ''[^,]+'', 1, level) is not null)';
--execute immediate stmt;
open WBList for stmt;
END Proc_Sample;
这是正确的做法吗? 注-过程已成功编译。 错误: 从客户端应用程序执行此操作时,出现以下错误 ORA-01745:无效的主机/绑定变量名称
答案 0 :(得分:0)
我更喜欢使用绑定变量:
SQL> CREATE OR REPLACE PROCEDURE Proc_Sample(ApplicationID varchar2,
--CType varchar2,
WBName varchar2,
RecNum OUT DATA.WORK_CPG.RECORDNUMBER%type ) AS
WBList sys_refcursor;
stmt varchar2(10000);
BEGIN
stmt := 'SELECT WO.RECORDNUMBER AS RECORDNUMBER
FROM DATA.ASSIGN_WB WB
INNER JOIN DATA.WORK_CPG WO
ON WO.PZINSKEY = WB.PXREFOBJECTKEY
INNER JOIN COMMON.REF_APPLICATION RA
ON RA.APPLICATIONNAME = WB.PXAPPLICATION
WHERE RA.APPLICATIONID = :AppID
AND WB.ASSIGNEDID IN
(SELECT regexp_substr(:WBN, ''[^,]+'', 1, level)
FROM dual
CONNECT BY regexp_substr(:WBN, ''[^,]+'', 1, level) is not null)';
OPEN WBList FOR stmt USING ApplicationID, WBName, WBName;
FETCH WBList INTO RecNum;
CLOSE WBList;
END Proc_Sample;
/
每次调用时,查询的第一个返回值将在RecNum中消失:
SQL> SET SERVEROUTPUT ON
SQL> Declare
rcn DATA.WORK_CPG.RECORDNUMBER%type;
Begin
Proc_Sample(118,'abc',rcn);
Dbms_Output.Put_Line(rcn);
End;
/
如果您的目的只是打印所有返回的记录,那么 替换
FETCH WBList INTO RecNum;
使用
LOOP
FETCH WBList INTO RecNum;
EXIT WHEN WBList%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(WBList.RECORDNUMBER);
END LOOP;
在此过程中。