简体   繁体   中英

How to pass a TABLE OBJECT out parameter from C# ASP.NET Core to Oracle Stored Procedure

I'm trying to pass an Oracle Table Object out parameter from a C# API on ASP.NET Core 2.2 to a Oracle Stored Procedure.

I have an Oracle stored procedure named GET_CARD_LIST

CREATE OR REPLACE TYPE TABLE_OBJECT IS TABLE OF VARCHAR2(16)

PROCEDURE GET_CARD_LIST(USER_ID IN number , CARD_LIST OUT TABLE_OBJECT) AS

BEGIN
    -- No real code, just an example query result
    SELECT CARD_NUMBER INTO CARD_LIST FROM CUSTOM_CARDS;
END GET_CARD_LIST;

And this is my C# code, how pass a out parameter to match with the out parameter in the Stored Procedure?

OracleConnection connection = _oracleDbContext.GetConnection();

using (OracleCommand command = new OracleCommand())
{
    command.Connection = connection;                
    command.CommandType = CommandType.StoredProcedure;
    command.CommandText = "PRUN_APP_SV_PKG.OBTIENE_ID_TH_P";
    command.Parameters.Add("EMAIL", OracleDbType.Varchar2, ParameterDirection.Input).Value = email;

    // Here is my problem, how pass out parameter?            
    command.Parameters.Add("CARD_LIST", OracleDbType.???, ParameterDirection.Output);                

    try
    {
        command.ExecuteNonQuery();
    }
    catch (System.Exception ex)
    {
        _logger.LogCritical("Oracle - ExecuteNonQuery. Exception: {ex}", ex);
    }
    finally
    {                        
        command.Dispose();
        if (connection.State == ConnectionState.Open)
            connection.Close();
    }
}

Thank you very much in advance!

Return a cursor rather than a nested table.

CREATE OR REPLACE FUNCTION GET_CARD_LIST(USER_ID IN number) RETURN SYS_REFCURSOR AS
   CARD_LIST SYS_REFCURSOR;
BEGIN
    OPEN CARD_LIST AS
    SELECT CARD_NUMBER FROM CUSTOM_CARDS;

    RETURN CARD_LIST;
END GET_CARD_LIST;

Sorry, if a procedure has only one single return values then I prefer a FUNCTION rather than PROCEDURE.

If you have to use existing structures in your code then it would be

CREATE OR REPLACE FUNCTION GET_CARD_LIST(USER_ID IN number) RETURN SYS_REFCURSOR AS
   CARD_LIST TABLE_OBJECT;
   res SYS_REFCURSOR;
BEGIN
    SELECT CARD_NUMBER BULK COLLECT INTO CARD_LIST FROM CUSTOM_CARDS;

    OPEN res FOR
    SELECT * FROM TABLE(CARD_LIST);    
    RETURN res;
END GET_CARD_LIST;

In C# the parameter would be

command.Parameters.Add("res", OracleDbType.RefCursor, ParameterDirection.ReturnValue);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM