Pass datatable to refcursor of Oracle stored procedure

落爺英雄遲暮 提交于 2020-01-17 03:53:25

问题


I am using ODP.NET and Oracle 11g client. I am passing a datatable to a procedure with a refcursor as input.

dtSample is DataTable with four columns called Type, Name, Size and Description.

I got error

Invalid parameter binding, Parameter name: ParameterName

at the line

db.ExecuteNonQuery(oracleCommand);

Full code:

OracleCommand oracleCommand = new OracleCommand();
oracleCommand.CommandType = CommandType.StoredProcedure;
oracleCommand.CommandText = "PCK_ADMIN.PROC_INS";
oracleCommand.Parameters.Add("P_USERID", OracleDbType.Varchar2).Value = "abcd";
oracleCommand.Parameters.Add("P_ATTACH_LIST", OracleDbType.RefCursor).Value = dtSample;
oracleCommand.Parameters.Add("P_out", OracleDbType.Int32).Direction = ParameterDirection.Output;
oracleCommand.Parameters.Add("P_msg", OracleDbType.Varchar2, 50).Direction = ParameterDirection.Output;

db.ExecuteNonQuery(oracleCommand);

Stored procedure:

PROCEDURE PROC_INS(  P_USERID IN VARCHAR2,
                            P_ATTACH_LIST IN SYS_REFCURSOR,
                            P_out      OUT NUMBER,
                             P_msg     OUT VARCHAR2) AS

V_BRS_USERID         VARCHAR2(50);
V_ATTACHMENT_TYPE_ID BRS_USER_ATTACHMENT.ATTACHMENT_TYPE_ID%TYPE;
V_FILE_NAME          BRS_USER_ATTACHMENT.FILE_NAME%TYPE;
V_FILE_SIZE          BRS_USER_ATTACHMENT.FILE_SIZE%TYPE;
V_FILE_DESCR         BRS_USER_ATTACHMENT.FILE_DESCR%TYPE; 

BEGIN 
LOOP 
FETCH P_ATTACH_LIST INTO V_BRS_USERID, V_ATTACHMENT_TYPE_ID,V_FILE_NAME, V_FILE_SIZE, V_FILE_DESCR; 
EXIT WHEN P_ATTACH_LIST%NOTFOUND; 
    INSERT INTO USER_ATTACHMENT VALUES
            (SEQ_RER_EMP_REP_ID.NEXTVAL,
              V_BRS_USERID,
              V_ATTACHMENT_TYPE_ID,
              V_FILE_NAME,
              V_FILE_SIZE,
              V_FILE_DESCR,
              NULL,
              NULL,
              0,
              0,
              'A',
              P_USERID,
              SYSDATE,
              NULL,
              NULL
            );

END LOOP; 

CLOSE P_ATTACH_LIST;
EXCEPTION
  WHEN OTHERS THEN
  ROLLBACK; -- Transaction mgmt
  p_out := 2;
  p_msg := sqlerrm;

END PROC_REG_INS_ATTACH;            

回答1:


You can't. You'll need to pass in another ref cursor and you can only get that as the output of a stored procedure/function, or a query against a table in an anonymous block... perhaps you can create a wrapper stored procedure that uses different data types, such as associative arrays. http://www.oracle.com/technetwork/issue-archive/2006/06-jan/o16odpnet-087852.html



来源:https://stackoverflow.com/questions/27249670/pass-datatable-to-refcursor-of-oracle-stored-procedure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!